0

I am trying to create a registration form that checks to see if the user is already in the system by email using AJAX. I cannot seem to get my php file to run though and have tried a variety of items from the forums. Can anyone help with my code

the script is as follows.

<script>
function showUser() {
  var useremail = $('#useremail').val();
  var userpassword = $('#userpassword').val();
  var userzip = $('#userzipcode').val();
  var url = "/assets/php/userlogin/userdupcheck.php?e="+useremail+"&p="+userpassword+"&z="+userzip;

xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
}
</script>

The html form is

<div id="registration" title="Register at TownCaddy.com">
<form name="register" id="register" onsubmit="showUser()">
<table>
    <tr>
        <td><input id="useremail" name="useremail" type="text" placeholder="Your Email" size="20" maxlength="45" /></td>
    </tr>
    <tr>
        <td><input id="userpassword" name="userpassword" type="password" placeholder="Password" size="20" maxlength="32" /></td>
    </tr>
    <tr>
        <td><input id="userzipcode" name="userzipcode" type="text" placeholder="Zip" size="5" maxlength="5" /></td>
    </tr>
    <tr>
        <td colspan="4"><input name="submit" type="submit" value="Register" /></td>
    </tr>
</table>
</form>
</div>

and the PHP file userdupcheck.php code is below

<?php
$e = $_GET['e'];
$p = $_GET['p'];
$z = $_GET['z'];
$query_dupUserCheck = "SELECT tblUser.userKey FROM tblUser WHERE tblUser.useremail = '".$e."'";
$sqlsearch = mysql_query($query_dupUserCheck);
$resultcount = mysql_num_rows($sqlsearch);

if ($resultcount > 0) {
    print '<script type="text/javascript">'; 
    print 'alert("The email address '. $e.' is already registered")'; 
    print '</script>';
} else {
    require_once('register.php');
}

?>
6
  • What does Firebug show you? Commented Oct 25, 2013 at 22:28
  • It's generally not a good idea to be sending a password in a GET request. You should use post so the password isn't exposed in the url Commented Oct 25, 2013 at 23:01
  • Just a thought (not certain about this), but you aren't defining an 'action' attribute for your form tag. Since this is required, the behaviour could well be undefined if it is not present (such as not firing an onSubmit event, since without an action it has no meaning). w3.org/TR/html401/interact/forms.html#h-17.3 Commented Oct 25, 2013 at 23:06
  • @Gus - HTML 5 makes it optional, and the data is being submitted with XHR, which bypasses the form submission process entirely anyway. Commented Oct 25, 2013 at 23:49
  • Mr. Martin I tried the post but did not get any results either Commented Oct 26, 2013 at 1:15

1 Answer 1

1

Your error is

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

Why? I don't know.. I moved to jQuery before figuring this out long time ago. Try it this way

xmlHttp.open("GET", "test.php?e=test", false);
xmlHttp.send();

Also, why not save your self the time and move this over to using jQuery? Anyways.. I really don't know anything about x-www-for-urlencoded because I really only used it once.. but at least this should get you going. I have tested this my self a few min ago and it is working... here is my debug code..

<?php



$e = empty ($_GET['e'])?null:mysql_real_escape_string ($_GET['e']);
$p = empty ($_GET['p'])?null:mysql_real_escape_string ($_GET['p']);
//$z = $_GET['z'];


$name = "xlordt";
$rescount = null;

if (isSet ($_GET ['e']))
{

    echo $_GET ['e'];;
    exit; //exit else, we will get the page instead of results
}
//if ()
if ($rescount > 0) {

    print '<script type="text/javascript">'; 
    print 'alert("The email address '. $q.' is already registered")'; 
    print '</script>';

} else {

    //require_once('register.php');
}


?>

<!DOCTYPE html>
 <html>
  <head>
   <title>Nothing</title>
   <metha charset="utf-8" />
   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
   <script>
        function showUser() {
            var useremail = $('#useremail').val();
            var userpassword = $('#userpassword').val();
            var userzip = $('#userzipcode').val();
            var url = "test2.php";

            if(window.XMLHttpRequest) {     // for Forefox, IE7+, Opera, Safari, ...
                 xmlHttp = new XMLHttpRequest();
            }

            xmlHttp.open("GET", "test.php?e=test", false);
            xmlHttp.send();
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        }
   </script>
  </head>

  <body>
   <div id="registration" title="Register at TownCaddy.com">
    <form name="register" id="register" onsubmit="return showUser();">
    <table>
        <tr>
            <td><input id="useremail" name="useremail" type="text" placeholder="Your Email" size="20" maxlength="45" /></td>
        </tr>
        <tr>
            <td><input id="userpassword" name="userpassword" type="password" placeholder="Password" size="20" maxlength="32" /></td>
        </tr>
        <tr>
            <td><input id="userzipcode" name="userzipcode" type="text" placeholder="Zip" size="5" maxlength="5" /></td>
        </tr>
        <tr>
            <td colspan="4"><input name="submit" type="submit" value="Register" /></td>
        </tr>
    </table>
    </form>
  </div>    
  </body></html>
Sign up to request clarification or add additional context in comments.

6 Comments

I am still not able to get this to work. I copied and pasted the code you have into a new file and tested. Could there be something missing on my local machine or server?
How are you testing this? are you using firebug or similar to check the results?
I ran through firebug but did not see any errors. What should I be looking for?
Make sure "Persist" is on in the "Console" section and try again.
got it thank you. Let me play with it more to see if I can get it to execute my code now and get back to you tomorrow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.