user1339752
user1339752

Reputation:

How to store the value returned by javascript function into a php variable?

The output generated by the program below is Inner textjain. Fine, but I want $m php variable to be assigned the value returned from JavaScript function a().

<html>
<head></head>
<body>
<?php
$name='abhi';
?>
<div id="a">

</div>
<script type="text/javascript">
function a(){
var a="jain";

<?php echo $GLOBALS['name'] ; ?>=a;
document.getElementById('a').innerHTML="Inner text"+<?php echo $name; ?>;
return <?php echo $GLOBALS['name'];?>;
}

</script>


<?php echo $n='<script type="text/javascript">'   , 'a();'   , '</script>';
echo $n;?>

</body>
</html>

Upvotes: 1

Views: 1496

Answers (3)

outlyer
outlyer

Reputation: 3943

PHP runs on the server, Javascript runs on the browser, and they don't run concurrently, so you'll have to send your Javascript variable to the PHP script (and re-run the PHP script), which involves reloading the page.

You can e.g. a GET request (by loading script.php?yourvariable=value).

Upvotes: 1

joy
joy

Reputation: 122

you can't. PHP runs at the server first and then javascript will run at client side.

Upvotes: 0

Erik
Erik

Reputation: 3636

You can't. Once the page is delivered to the browser, which is where Javascript runs, your PHP server has already shut the request down. If you somehow really want to send something calculated in Javascript back to PHP, you'll have to make an additional request to the browser from Javascript (using AJAX) but even then the variable will be available in a new request, not the old one.

Upvotes: 1

Related Questions