Jump to content

PHP and Java Script communication


mjsulliv

Recommended Posts

In teaching myself java script and PHP I've got to the point where I'm able to do / hack what I want but I'm not sure it's the "right" way to do things. I have an application that inserts and deletes records from an sql db. The table of records is displayed from a PHP page (e.g. showTable.php); I add a button to the table for each record in the table. When clicked the button calls a js function, passing in a record number. The function asks for confirmation and if approved uses:

window.location='showTable.php?recdNum_fld='+id;

The page then gets reloaded and PHP code in the page checks if the $_GET has a recdNum_fld set. If it does it performs the delete in the db.What I'd like to know is this the "proper / standard" way of having js and php interact?

Link to comment
Share on other sites

There's nothing really wrong with changing the location property. If PHP is going to send a new document, it is more customary to use a form. The form can be sent directly when the user clicks the submit button. If you need to validate something, JavaScript captures the form's submit event (there are several ways to do this), and if the data passes the test, JavaScript calls the form's submit() method.If PHP will update the database and return the user to the same page, then AJAX is a more friendly solution, since it operates in the background. The HTML works the same way, and you still capture the submit event, but you send the data to your server using different techniques that operate in the background.

Link to comment
Share on other sites

I've begun looking at AJAX but I'm not ready to start developing with it, but I know it's coming.

... If you need to validate something, JavaScript captures the form's submit event (there are several ways to do this), and if the data passes the test, JavaScript calls the form's submit() method.
Do you have and example or can you point me to the several ways?
Link to comment
Share on other sites

The easiset (and ugliest) is to add some inline code to your form tag:<form action="some.php" method="get" onsubmit="return myvalidate(this)">and that will send a reference to your form to a javascript function called myvalidate (the function that you have written; call it what you want). Obviously, you can pass any data you like to the function. If your form has a hidden element with the name 'recdNum_fld', your function can set the value of that element to the correct id, and the browser will construct the query string the way you want it to. Your function should have a return value. If it returns true, the form will be sent. If it returns false, the form will not be sent.

Link to comment
Share on other sites

I have a follow up question if I may. What is the "statefulness" of PHP and js variables as call between them progress? In other words, if I set a PHP variable, the page is sent to the browser, the browser then makes a call back to the server -- either to the same page or a different one -- will the variable still be set? ditto for the reverse.

Link to comment
Share on other sites

When a PHP script terminates, the memory allocated to it is deallocated. Variables and their values are destroyed. You can use sessions to save temporary data. PHP creates a unique id for the session and stores it in a cookie on the user's browser. After it reads the cookie, it looks for data associated with the id. The data persists for 20 minutes or so every time PHP calls the session_start function. This is usually long enough to maintain data values between page loads.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...