Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

so I have this in my HTML file

 <script type="text/javascript" src="load.js"> </script>    
    <script type="text/javascript" src="load2.php"> </script>

Saw somewhere you could call an external php file using the js script tag and whatever js in the php file would be rendered first. But my problem is, I can't assign no JS variable from load.js to a php variable in load2.php but, however, I could alert using echo "alert(js variable);"; So that tells me it finds the variable and I could use it, but I can't assign it to a php variable :/ any help please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
143 views
Welcome To Ask or Share your Answers For Others

1 Answer

When working between JavaScript and php you must remember one is client side (JavaScript) and the other server side (php). Therefore php can insert values into javascript before it is sent to the client and then rendered (processed). Although the reverse cannot happen as the "scope" of JavaScript is on the clients computer.

The way around this is to use AJAX requests from the client back to the server sending back the data / variable values you need.

JQuery provides an easy to use library with AJAX functionality.

Please see http://api.jquery.com/jQuery.ajax/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...