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;
?>
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>
Pass Javascript variable to PHP
You can't pass variable values from the current page Javascript to the current page PHP code ... PHP code runs at the server and it doesn't know anything about what is going on on the client side.
You need to pass variables to PHP code from html-form using another mechanism, such as submitting form on 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 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'];
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
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
Replace Woocommerce_Add_Order_Item_Meta Hook in Woocommerce 3.4
Get Woocommerce Product Categories from Wordpress
PHP Errors Not Being Displayed in the Browser [Ubuntu 10.10]
Request Headers Bag Is Missing Authorization Header in Symfony 2
Post to a Facebook User's Wall with Curl PHP
PHP "Pretty Print" HTML (Not Tidy)
Notice: Undefined Offset: 0 In
Difference Between PHP Echo and Return in Terms of a Jquery Ajax Call
Bind_Param Number of Variables Doesn't Match Number of Parameters in Prepared Statement
Adding Subscribers to a List Using Mailchimp's API V3
How to Store an Array into MySQL
Passing PHP Objects to JavaScript
How to Check That an Object Is Empty in PHP
In PHP, How to Extract Multiple E-Mail Addresses from a Block of Text and Put Them into an Array