php + mysql的奇怪AJAX行为.一半在工作/一半不在



我试图在我的表单中添加一些AJAX,但是我对一个问题感到非常困惑,我已经有一段时间了。

我有两个PHP页面,dashboard.php, process-register.php.

用户可以登录到他们的仪表板并将产品的许可证发送到他们选择的电子邮件地址。这在dashboard.php页面上。一旦提交,process-register将被调用,并将新用户插入数据库中,并将其登录详细信息通过电子邮件发送给他们。

奇怪的是,当我留空电子邮件字段并单击提交时,它总是返回process-register.php页面上指定的错误,然而,当我输入电子邮件地址并单击提交时,它只是刷新页面而不做任何事情。奇怪的是,在奇怪的情况下,它确实有效,并创建了一个新用户。我想说,这只是每隔1/5次。

JavaScript如下:

  $("#form-submit").click(function() { 
        $.ajax({
        cache: true,
        type: 'POST',
        url: 'process-register.php',
        data: $("#form-register").serialize(),
        success: function(response) {
            $("#output-div").html(response);
        }
        });
    }); 

Dashboard.php

<?php
    include("config.php");
       if(empty($_SESSION['email']))
        {
        echo "Please login...redirecting";
        echo '<meta HTTP-EQUIV="REFRESH" content="2; url=http://example.com">';
        }
        else
        {
        ?>
    <?php include_once 'header.php'; ?>
    <div id ="terms-content-wrapper">
        <div id="content" class="wrap">
        <div id="register">
                        <form id="form-register" class="testform" method="POST" form action="" >
                    <h4>Send License</h4>
                    <?php 
        $qry = mysql_query("SELECT codes_remaining FROM users WHERE email= '".$_SESSION['email']."'");
        while($row = mysql_fetch_array($qry)) {
            if ($row['codes_remaining'] ==1 ) 
            {
            echo "You have ".$row['codes_remaining'].' code remaining';
            }
            else {
        echo "You have ".$row['codes_remaining'].' codes remaining';
        }
        }
    ?>
                    <p>
                    Enter the e-mail address of the person you would like to send a license to.
                    </p>
                    <p>
                        <label for="email">Email Address:</label>
                        <input id="email" class="required email" value="" type="text" name="email">
                    </p>
                    <br>
                        <p class="buttons">
                            <input name="submit" type="submit" id="form-submit"/>
                    </form>
                </div>
            <div id="output-div"></div>
        </div>
    </div>
    <p></p> <!--content-wrapper div -->    
    <?php include_once 'footer.php'; ?> 
    <?php
        }
    ?>

process-register.php

<?php
    include("config.php");
    $username = mysql_real_escape_string( stripslashes($_POST['email']));
    //password emailed to user
    function createPassword($length) {
    $chars = "234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $i = 0;
    $password = "";
    while ($i <= $length) {
        $password .= $chars{mt_rand(0,strlen($chars))};
        $i++;
    }
    return $password;
}
$password = createPassword(8);
function genRandomString() {
    $length = 20;
    $characters = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "";    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}
$validation = genRandomString();
    if(($username != "") && ($password != ""))
    {
    //all fields have something in
    $sql = mysql_query("SELECT codes_remaining FROM users WHERE email= '".$_SESSION['email']."'");
   while($row = mysql_fetch_array($sql)) 
    {
    if($row['codes_remaining'] > 0)
    {
    //create a new user
    mysql_query("INSERT INTO users (email, password, verification_code) VALUES('". mysql_escape_string($username) ."', '".md5($password)."', '". mysql_escape_string($validation) ."') ") or die(mysql_error()); 
    //email them the details.
        $to      = $username;
        $subject = 'You lucky devil';
        $message = '
        Some kind fellow / lady has bought you something
        Login to: http://example.com/login
        Your account information
        -------------------------
        Email: '.$username.'
        Password: '.$password.'
        -------------------------
        If you have any problems, please contact us and we will sort it out ASAP.
        Thanks again;
        $headers = 'From:support@example.com' . "rn";
        mail($to, $subject, $message, $headers);
        mysql_query("UPDATE users SET codes_remaining = codes_remaining-1 WHERE email= '".$_SESSION['email']."'");
        echo "You have create a new user";
            }
        else
            {
            echo "Error: You do not have any licenses left. Please purchase some more.";
            }
        }
    }
    else
    {
    echo "Error: Please fill in all the fields.";
    }
?>

正如我所说的,当我输入一个电子邮件时,它只工作了大约20%的时间,仍然感觉迟钝,但是当我留空并提交时,我总是得到错误代码返回并且页面不刷新。

我真的很感激在这件事上的任何帮助。谢谢,Lordsnoutimus

绑定表单的提交事件,但不停止表单的正常提交。添加一个返回false;在$("#form-submit")的末尾,点击(function() {

或者event.preventDefault(),如下所示:

  $("#form-submit").click(function(e) { 
        e.preventDefault();
        $.ajax({
        cache: true,
        type: 'POST',
        url: 'process-register.php',
        data: $("#form-register").serialize(),
        success: function(response) {
            $("#output-div").html(response);
        }
        });
        // return false; would do the same.
    }); 

在您的情况下,发生的事情是,当您单击按钮时,表单像往常一样提交(非ajax方式),如果过程足够慢,一些绑定的代码将有时间运行…所以有时提交过程会慢到足以让AJAX请求准备好&

相关内容

  • 没有找到相关文章

最新更新