【问题标题】:Using jQuery's method .ajax() to send a form in PHP使用 jQuery 的方法 .ajax() 在 PHP 中发送表单
【发布时间】:2012-04-13 14:41:44
【问题描述】:

我目前正在制作一个允许用户更改密码的表单。而且我对 jQuery ajax 不是很好。 ajax 似乎没有将数据发送到changepassword.php。当我不使用 ajax 时,PHP 文件工作正常。你们能帮帮我吗?

我根据一些 cmets 更新了我的文件。当我尝试提交表单时,它返回Doesn't Work。所以我猜php正在响应,但值没有进入数据库。

非常感谢大家的帮助。

以下是表单和ajax(更新)

        <form id="form_id" method="POST">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" required />
            <label for="old_password">Old Password</label>
            <input type="password" name="old_password" id="old_password" required />
            <label for="new_password">New Password</label>
            <input type="password" name="new_password" id="new_password" required />
            <button class="submit">
                Submit
            </button>
            <p class="result"></p>
        </form>
        <script>
            $.ajaxSetup({
                cache : false
            });
            $(".submit").click(function(e) {
                $.ajax({
                    url : "changepassword.php",
                    type : "POST",
                    data : $("#form_id").serialize(),
                    success : function(data) {
                        if(data == 1) {
                            $(".result").html("Works");
                        } else {
                            $(".result").html("Doesn't Work");
                        }
                    }
                });
                return false;
            });

        </script>

这是changepassword.php(更新)

    <?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
include_once "../classes/Connect.php";
$connection = new Connect();
$username = $_POST['username'];
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
$result = $connection -> change_password($username, $old_password, $new_password);
if ($result) {
    echo 1;
}
    ?>

这是来自connect.php类文件的方法change_password()

    function change_password($username, $old_password, $new_password){
    $query = "UPDATE Account SET password = ? WHERE username = ? AND password = ?";
    if ($stmt = $this -> conn -> prepare($query)){
        $stmt -> bind_param('sss', md5($new_password), $username, md5($old_password));
        if ($stmt -> execute()){
            return true;
        }
    }
}

【问题讨论】:

  • 你在萤火虫上有什么错误吗?您是否收到“有效”的警报?
  • echo $username 或者你的 php 文件中的任何内容然后 alert(data);而是查看它是否正确传递给 php。
  • 我没有显示警报“Works”。我试图在 php 文件中回显 $username,但它不会传回 ajax。而且我不太清楚如何使用萤火虫
  • 只是为了好玩,还要在脚本顶部添加$.ajaxSetup ({cache:false})
  • 在 Firefox 中:打开工具->Web 开发人员->Firebug->XHR 选项卡。然后单击您的按钮并检查/单击控制台内容。

标签: php jquery html ajax forms


【解决方案1】:

您对服务器的请求是否成功?因为我不确定,但是如果整个请求都失败了,那么你就没有成功,jquery ajax(可能)会寻找错误函数,但你没有提供它——所以什么都没有发生。

关于设置您的 ajax 数据属性,您可能可以执行 data: $('#form_id').serialize() 并且 thid=s 将为请求创建正确的字符串 - var1=value1&var2=value2 但代码更少。

如果在session_start(); 插入以下代码后你的 php 文件有问题,你会提示你出了什么问题:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);

祝你好运。 伊夫林

【讨论】:

  • 我做到了。我还给表格一个ID。但它仍然无法正常工作。无论如何,谢谢。
  • 在您的情况下,“不起作用”意味着请求已成功发送到脚本,但 mysql 请求或逻辑中的某些内容出错。如果可以用php.net/manual/en/function.mysql-affected-rows.php查看查询结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 2011-04-19
  • 2017-07-21
  • 2016-07-08
相关资源
最近更新 更多