...

View Full Version : Confused about Javascript and PHP interaction



lld
03-04-2012, 03:54 AM
Hi, I have a problem like this:

I have a page that lets the user to add record into the database. After the user clicks on the 'submit' button, I would like to perform a check for duplicate record in my database. (I suppose this is to be done in PHP?).

Then if an existing record is found in database, I will ask the user if he wants to replace the existing record using a confirmation popup. (This uses javascript?)

I know how to execute a javascript function in PHP by using the echo command. However, I am confused how I can retrieve the return value of the confirmation from javascript so I can do an add or replace action in PHP accordingly.

Thanks a lot for the help.

Fou-Lu
03-04-2012, 03:57 AM
Use AJAX.
There is no way for PHP to interact directly with JS, nor JS with PHP (so no, PHP cannot execute a javascript function as this is processed by the client). One is a client language, one is a server language. Javascript must invoke a PHP script the same way your browser does. To PHP, there is no difference between ajax or your browser accessing. All it does is accept the input (post, get, etc), process and provide output.

webdev1958
03-04-2012, 04:41 AM
I have a page that lets the user to add record into the database. After the user clicks on the 'submit' button, I would like to perform a check for duplicate record in my database. (I suppose this is to be done in PHP?).

Then if an existing record is found in database, I will ask the user if he wants to replace the existing record using a confirmation popup. (This uses javascript?)


I would approach this functionality with something like this:

1) User fills out all inputs in a form

2) When the user clicks the Submit button, use javascript to validate all the inputs.

3) If they are all valid, send an ajax request to the server to query the database for a duplicate record. The ajax function returns a value of true or false depending on whether thre is a duplicate record.

4) If there is no duplicate record, use javascript to submit the form by returning true to the onsubmit event handler.

5) If a duplicate record exists, display a prompt asking the user to confirm if they want to replace the existing record with their new data inputed in the form.

6) If the user confirms they want to replace the old record with the new record, use javascript to submit the form.

7) If the user chooses to not replace the old record with the new data, then simply return false to the onsubmit event handler which will abort the form submission.

Whether the user submits a new record or chooses to overwrite an existing record, all the <form> data must be validated on the server because the js validation can be easily bypassed and a malicious user could send bogus data to the server side php script without having used your form at all.

lld
03-04-2012, 05:17 AM
I would approach this functionality with something like this:

1) User fills out all inputs in a form

2) When the user clicks the Submit button, use javascript to validate all the inputs.

3) If they are all valid, send an ajax request to the server to query the database for a duplicate record. The ajax function returns a value of true or false depending on whether thre is a duplicate record.

4) If there is no duplicate record, use javascript to submit the form by returning true to the onsubmit event handler.

5) If a duplicate record exists, display a prompt asking the user to confirm if they want to replace the existing record with their new data inputed in the form.

6) If the user confirms they want to replace the old record with the new record, use javascript to submit the form.

7) If the user chooses to not replace the old record with the new data, then simply return false to the onsubmit event handler which will abort the form submission.

Whether the user submits a new record or chooses to overwrite an existing record, all the <form> data must be validated on the server because the js validation can be easily bypassed and a malicious user could send bogus data to the server side php script without having used your form at all.

Hi webdev1958,

Thanks a million for the illustration! I think I have an idea how to write it now. I will check if my AJAX code works.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum