Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2012, 03:54 AM   PM User | #1
lld
New to the CF scene

 
Join Date: Mar 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
lld is an unknown quantity at this point
Confused about Javascript and PHP interaction

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.
lld is offline   Reply With Quote
Old 03-04-2012, 03:57 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 03-04-2012, 04:41 AM   PM User | #3
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
Quote:
Originally Posted by lld View Post
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.

Last edited by webdev1958; 03-04-2012 at 04:47 AM..
webdev1958 is offline   Reply With Quote
Old 03-04-2012, 05:17 AM   PM User | #4
lld
New to the CF scene

 
Join Date: Mar 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
lld is an unknown quantity at this point
Quote:
Originally Posted by webdev1958 View Post
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.
lld is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:40 AM.


Advertisement
Log in to turn off these ads.