Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get PHP and JavaScript to communicate?

PHP and JavaScript

How can I get PHP and JavaScript to communicate?

by  rhowes  Posted    (Edited  )
This is a common question and one I had to resolve myself recently. The important thing to remember is PHP is server side and JavaScript is client side.

But usually this is all anyone says when you ask. What they don't tell you is what IS possible.

Generally if you want to pass "instructions" or variables (e.g. flags) from JavaScript to PHP, then do the following:

1. Write your JavaScript to perform some action, e.g. when you click a button.
2. Write the "result" to a hidden form text field.
3. Reload the page and "read" the hidden text field value in PHP.

The reverse is also true . During PHP processing, if you want to pass on some "message" to JavaScript then set a hidden form field using PHP. On the client side you can "read" the message when the user performs some Javascript triggering action, or you can "read" it immediately the page loads with the JavaScript OnLoad event.

Again, because of the nature of the two technologies (server and client side) you can only communicate petween them by "reloading" a page (usually by redirecting to itself).

Hope this helps.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top