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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using JS to provide a form variable and then using PHP to verify in MySQL on submit

    I have this code in the head of my webpage to verify if the form has been submitted properly *before* i run the php to add a record to the DB:

    Code:
    <script> 
    function validateForm() { 
    
    var x=document.forms["register"]["email"].value; 
    
    if (x==null || x=="") {   
    alert("Please fill in email.");   
    return false;   
    }
    
    }
    </script>

    When the user clicks 'Submit', if the 'email' field is blank, a little message pops up to say fill in the email.

    OK, so that is the easy part.

    but what i want to do is check to see if that email already exists *before* posting.

    i know in javascript, in the above example, it will read the form field and know what is in it. what i want to do is somehow grab that variable and then query the DB to see if that email exists

    for example

    Code:
    <script> 
    function validateForm() { 
    
    var x=document.forms["register"]["email"].value; 
    </script>
    
    <?
    $sql="SELECT * FROM users WHERE email=>>var x above<<";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    
    ?>
    
    <script> 
    
    function ???() { 
    
    var y = $count;
    
    if (y>0) {   
    alert("Email in use.");   
    return false;   
    }
    
    </script>

    Obviously that code would not work, but I hope you get the idea. I want to use the check form contents script to grab the email addy, and then I want to check the users table to see if that addy exists, and if it does, *before* the form posts, I want to alert the user to use a different email.

    This way the user doesnt lose all the form contents. The submission is prevented and all they have to do is enter another email addy.

    if i check the users table after posting (easy), if the email is there, the form data gets wiped out and they have to start all over again. That is what I am trying to avoid.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    There are several examples on different ways you can do it at http://javascriptexample.net/ajax.php
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Server-side scripts are evaluated first before the server spits out the HTML, CSS and Javascript to the browser. So what you're trying to do will not work.

    The ways client-side can communicate to the server-side are to submit the form, pass querystring to a URL and send an AJAX request. In your case, you can send an AJAX request to check the email address existence when the email field loses its focus.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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