0

我正在尝试创建一个注册表单,以使用 AJAX 通过电子邮件检查用户是否已经在系统中。我似乎无法让我的 php 文件运行,但我尝试了论坛中的各种项目。任何人都可以帮助我的代码

脚本如下。

<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>

html表单是

<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>

PHP文件userdupcheck.php代码如下

<?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');
}

?>
4

1 回答 1

1

你的错误是

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

为什么?我不知道.. 我很久以前就开始使用 jQuery 了。试试这个方法

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

另外,为什么不节省自己的时间并将其转移到使用 jQuery 上呢?无论如何..我真的对x-www-for-urlencoded一无所知,因为我真的只用过一次..但至少这应该能让你继续前进。几分钟前我已经自我测试过了,它正在工作......这是我的调试代码......

<?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>
于 2013-10-25T23:46:36.213 回答