can javascript or jquery variable can be read by php code?
Example
<script>
var num = 3;
</script>
<php?
$a = 20;
$b = num*$a;
?>
anyone?
can javascript or jquery variable can be read by php code?
Example
<script>
var num = 3;
</script>
<php?
$a = 20;
$b = num*$a;
?>
anyone?
Short answer - No it can't. But there is an alternative known as AJAX where you can send a javascript variable to a php
page and make it accessible to that page. Consider the following example:
$.ajax({
url:'some-php-page.php',
type:'post',
data:{num:3},
success:function(data_returned)
{
// do anything you want with the data returned back
alert(data_returned); // will alert 60
}
});
some-php-page.php
$num = $_POST['num']; // the key used in the "data" attribute of the ajax call
$a = 20;
$b = $num*$a;
echo $b;
Of course you'll need to include jQuery library in order to use the $.ajax({}). This can be done through plain javascript too, but I prefer to use jQuery as it is much simpler.