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

    simple complicated if/else ajax problem

    hello, i wrote a script for registering users but there's an "error". with AJAX, when i enter a new e-mail address, it skips echo-ing "You have been registered" and goes straight to "This e-mail has already been registered" but the INSERT query did execute. help! i'm an AJAX noob so please bear with me. tried to put exit(); to the if statement but it didn't help.

    PHP Code:
    if ($row[0] == 0)
        {
            
    //if email not in db, register user
            
    $query2 "INSERT INTO users (username, userPwd, userAccess) VALUES ('$email', '$userPwd', '$userAccess')";
            echo 
    $query2;
            
    $result2 mysql_query($query2);
            echo 
    "You have been registered";
        }
        else
        {
            
    //e-mail already registered
            
    echo "This e-mail has already been registered";
        }

    Last edited by keymistress; 05-14-2007 at 01:17 PM.

  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What do you have before that?

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Provide the full code...

    Regards

  • #4
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    html form
    Code:
    <html>
    
    <head>
    
    <script src="userHint.js"></script> 
    
    </head>
    
    
    
    <body>
    
    
    
    <form> 
    
    Email: <input type="text" id="email" onkeyup="showHint(this.value)">
    
    </form>
    
    <p><div id="userHint"></div></p>
    
    
    
    </body>
    
    </html>
    javascript
    Code:
    var xmlHttp
    
    function showHint(email)
    {
    if (email.length==0)
      { 
      document.getElementById("userHint").innerHTML="";
      return;
      }
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      } 
    var url="getHint.php";
    url=url+"?email="+email;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    } 
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4)
    { 
    document.getElementById("userHint").innerHTML=xmlHttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
    }
    same php but add $email=$_GET["email"]; and open and close php.

  • #5
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your php is as above but with '$email=$_GET["email"]' before it?!? So where does $row[0] come from then? It seems $row[0] is not defined, so $row[0] will never be equal to 0 (or anything else for that matter).

  • #6
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $email
    =$_GET["email"];

        
    $query "SELECT COUNT(*) FROM users WHERE username = '$email'";
        
    $result mysql_query($query);
        
    $row mysql_fetch_row($result);

        
    //check if e-mail is already registered
        
    if ($row[0] == 0)
        {
            
    //register user
            //check student or staff
            /*if (eregi("rp.sg$", $email))
            {
                $userAccess = 'Staff';
            }
            else
            {
                $userAccess = 'Student';
            }

            $query2 = "INSERT INTO users (username, userPwd, userAccess) VALUES ('$email', '$userPwd', '$userAccess')";
            echo $query2;
            $result2 = mysql_query($query2);*/

            
    echo "An e-mail with your username and password has been sent to the address you have entered.";
        }
        else
        {
            
    //e-mail already registered
            
    echo "This e-mail has already been registered. Forgot password?";
        }

        
    mysql_close($con);
    }
    else
    {
        echo (
    "E-mail is invalid. Please enter an RP e-mail address ending with 'rp.edu.sg' or 'rp.sg'.");
    }
    ?>


  •  

    Posting Permissions

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