【问题标题】:Bizarre AJAX behavior with php + mysql. Part working / part not?php + mysql 的奇怪 AJAX 行为。部分工作/部分不工作?
【发布时间】:2011-11-18 22:14:36
【问题描述】:

我正在尝试将一些 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);
        }
        });
    }); 

仪表板.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
        }
    ?>

进程注册.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' . "\r\n";

        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% 的时间内工作并且仍然感觉迟缓,但是当我将其留空并提交时,我总是得到返回错误代码并且页面没有' t 刷新。

非常感谢您对此事的任何帮助, 谢谢, 诺提莫斯勋爵

【问题讨论】:

    标签: php javascript jquery mysql ajax


    【解决方案1】:

    你绑定了表单的提交事件,但是并没有停止表单的正常提交。 添加一个return false;在 $("#form-submit").click(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 请求,但大多数时候您在它发生之前就离开了页面。

    【讨论】:

    • Bingo,5 次中有 1 次有效,因为 AJAX 在浏览器完成表单提交操作之前完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多