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>
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
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 can i pass js variable to php variable?
<script>
var jsvar = "Name";
</script>
<?php
$phpVar = "<script>document.write(jsvar);</script>";
echo $phpVar;
?>
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.
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>
Passing javascript variables to php?
PHP and javascript don't work like that.
PHP is a server-side language. While javascript is a clientside language.
There are still ways of passing data from the client-window to your server, via ajax, query parameters, cookies... But none will work in the same page.
Give us a clearer image on what you are trying to achieve and we will gladly help.
UPDATE
JS
<script type="text/javascript">
document.cookie = 'name=Khez' ;
</script>
PHP
<?php
var_dump($_COOKIE['name']);
?>
Related Topics
Convert Var_Dump of Array Back to Array Variable
The Openssl Extension Is Required For Ssl/Tls Protection
Execute Raw SQL Using Doctrine 2
Curl_Exec() Always Returns False
Send Post Request Using Volley and Receive in PHP
Get the Files Inside a Directory
Verify Receipt For in App Purchase
How to Install Composer PHP Packages Without Composer
Curl Error: Recv Failure: Connection Reset by Peer - PHP Curl
Byethost Server Passing HTML Values "Checking Your Browser" With Json String
Converting HTML to Pdf Using PHP
How to Remove HTML Special Chars
How to Make a PHP Form That Submits to Self