Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    lld
    lld is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.

  • #4
    lld
    lld is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •