【发布时间】: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