简体   繁体   中英

PHP Get Value from JavaScript Variable

I have an Index.php file, which contain a simple js,php language for getting a value from javascript, but I can't do that. can you tell me about how to do it?

Here is My index.php Wrong Syntax File:

<html>
<body>
<script>
   var foo = "bar";
</script>
<?php
   $fromJS = "<script>var jsfoo = foo;</script>";
   echo $fromJS;
?>
</body>
</html>

If I'm right you want to set a variable in JavaScript and read its value in PHP. If that's right, it is not possible that way. The reason is really simple: PHP will be executed inside the HTTP server on the server side, while JavaScript is executed in the browser on the client side.

To pass the value of a JavaScript variable to PHP you have to initiate a request to your PHP script and add the value to the PSOT or GET request.

A very simple example:

The JavaScript part

var foo = 'bar';
window.location.href = '/path/to/script.php?foo=' + foo;

And the PHP part

<?php
echo htmlentities($_REQUEST['foo']);

I hope that helps.

You can't do this, at least not the way you have here .

PHP is a server-side language, and thus is executed as the page is rendered to the response, which is then passed back to the client (the browser).

JavaScript is a client-side language, and isn't executed until it is read by the browser. Therefore, it is not possible to pass the client-side data (the JavaScript) to the server (PHP) as you have it here .

The only way you can achieve this is to have another page which POSTs or GETs data to a PHP via JavaScript (using a form submit or an XMLHTTPRequest, etc), and then process the posted data from within PHP that way.

Here's a really simple example which performs a GET against the PHP page:

On the client side (JavaScript):

window.location.href = "http://yoursite.com/page.php?foo=bar";

On the server side (PHP):

<?php 
echo htmlspecialchars($_REQUEST['foo']);
?>

You can create a hidden iframe and a submit a form within the iframe with the variable you want to send to your script. Also all DOM manipulations can be done from the iframe.

<html>
<body>
<iframe id="ifrm" src="iframe.php" width="1" height="1" style="display: none;"></iframe>
<script>
    var foo = "bar";
// I assume you are using jQuery
    jQuery('ifrm').contents().find('#inpt').val(foo);
    jQuery('ifrm').contents().find('form').submit();
</script>
</body>
</html>

iframe.php

<html>
<!-- include jQuery or whatever -->
<body>
<?php
if(isset($_POST['inpt'])) {
// Do your manipulations here

    echo '<script type="text/javscript">';
// retrieve iframes parent (your webpage)
    echo 'let parent = window.parent.document.body';
// do anything you want
    echo 'parent.style.backgroundColor = \'#ff0000\'';
    echo '</script>';
}
?>
<form action="#" method="post" enctype="multipart/form-data">
<input id="inpt" name="inpt" type="hidden" value="" />
</form>
</body>
</html>

Should work like this, but keep in mind - I haven't tested the above code. But you should get the idea.

Have fun with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM