How do I pass JavaScript variables to PHP?
You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.
You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.
<DOCTYPE html>
<html>
<head>
<title>My Test Form</title>
</head>
<body>
<form method="POST">
<p>Please, choose the salary id to proceed result:</p>
<p>
<label for="salarieids">SalarieID:</label>
<?php
$query = "SELECT * FROM salarie";
$result = mysql_query($query);
if ($result) :
?>
<select id="salarieids" name="salarieid">
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one)
}
?>
</select>
<?php endif ?>
</p>
<p>
<input type="submit" value="Sumbit my choice"/>
</p>
</form>
<?php if isset($_POST['salaried']) : ?>
<?php
$query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid'];
$result = mysql_query($query);
if ($result) :
?>
<table>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others
echo '</tr>';
}
?>
</table>
<?php endif?>
<?php endif ?>
</body>
</html>
how can i pass js variable to php variable?
<script>
var jsvar = "Name";
</script>
<?php
$phpVar = "<script>document.write(jsvar);</script>";
echo $phpVar;
?>
Pass Javascript Variable Value in PHP
Try below to send the JS data to PHP via AJAX,
$.ajax({
type: 'POST',
url: 'yourphppage.php',
data: {
'totalprice' : $("#total-price").val(),
},
success: function(response){
}
})
In yourphppage.php,
echo $_POST['totalprice'];
How to pass JavaScript variable into a PHP function parameter?
In a very simple form/example you simply need to send an http request ( here done using the Fetch api ) to your PHP script and intercept that request. The following sends a POST request with a single parameter sid
which is then passed to the Twilio method as per your requirement.
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['sid'] ) ){
$sid=$_POST['sid'];
exit( $twilio->check_disputed( $sid ) );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Your page</title>
</head>
<body>
<h1>Do lots of things...</h1>
<script>
/*
do whatever it is you do to create your variable sid
- here it is explicitly declared rather than derived.
*/
const sid='CA519c6aefde211131f2f44370d67607d4';
let fd=new FormData();
fd.set('sid',sid);
fetch( location.href, { method:'post', body:fd } )
.then(r=>r.text())
.then(data=>{
alert(data)
})
</script>
</body>
</html>
Send JavaScript variable to PHP variable
As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name']
after your javascript was executed.
Pass javascript variable to php code?
You can't directly do that, since JavaScript runs on the client-side and PHP gets executed on the server-side.
You would need to execute the JavaScript first and then send the result to the server via a FORM or AJAX call.
Here's what that might look like:
PHP
$parameter = "this is a php variable";
echo "var myval = foo(" . parameter . ");";
JavaScript
var myval = foo("this is a php variable"); // generated by PHP
$.ajax({
type: 'POST',
url: 'yourphpfile.php',
data: {'variable': myval},
});
Receiving PHP (yourphpfile.php)
$myval = $_POST['variable'];
// do something
Related Topics
Allowed Memory Size of 33554432 Bytes Exhausted (Tried to Allocate 43148176 Bytes) in PHP
Why Would $_Files Be Empty When Uploading Files to PHP
Finding the Number of Days Between Two Dates
How to Make a Request Using Http Basic Authentication With PHP Curl
How to Call a JavaScript Function from PHP
Sort an Array by Keys Based on Another Array
Allow PHP Sessions to Carry Over to Subdomains
How to Encrypt and Decrypt a PHP String
PHP - Concatenate or Directly Insert Variables in String
Sending Email With PHP from an Smtp Server
How to Reindex an Array in PHP But With Indexes Starting from 1
Startswith() and Endswith() Functions in PHP
Fatal Error: Maximum Execution Time of 30 Seconds Exceeded
How to Create a Pdo Parameterized Query With a Like Statement