无法让我的PHP脚本从AJAX执行

时间:2013-10-25 22:24:27

标签: php ajax

我正在尝试创建一个注册表单,通过电子邮件使用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');
}

?>

1 个答案:

答案 0 :(得分: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>