Call php from javascript

This is a Frequently Asked Question for beginners getting to grips with the whole clientside versus serverside programming topic.

  1. Javascript can only run in the browser.
  2. PHP can only run on the webserver.

PHP forgets everything it knows once it has served a page up. It even forgets session variables as these are sent to the browser and then retrieved when the next page is requested.

Javascript can call pages from the webserver but it can’t access the session variables for the webserver scripts directly.

So, to access the webserver scripts you need a page to be created…

  1. By clicking on a hyperlink
  2. By submitting a form
  3. By setting the href of a target frame (_self, _top, _blank, or a specified frame name)

I have an example of using a hidden iframe to retrieve information from the database. It’s a nice marriage of PHP and Javascript – working to give a useful interface for the end user.

Don’t forget, always store anything to do with your intellectual property or business rules in server side scripts – and in a secure location if possible.

Article: Using PHP and Javascript together


Recent Comments


Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.