dpjj4763 2013-10-25 22:24
浏览 30
已采纳

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

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

?>
  • 写回答

1条回答 默认 最新

  • du9826 2013-10-25 23:46
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么