[英]PHP Save to MySQL convert to using AJAX
我目前有一個設置要使用PHP保存到MySQL數據庫:
index.php
包括供用戶填寫的表格
validate.js
一個Java驗證文件,用於在提交給數據庫之前檢查用戶輸入
save.php
如果已驗證用戶響應,並將其重定向到此文件,則將其成功保存到數據庫后,將其重定向到:
complete.php
最后一頁。
我知道,借助AJAX的實現,我可以刪除頁面重定向,以使用戶體驗更加清晰。
但是,在進行重新編碼之前-我可以將現有的save.php
用作新的AJAX工作方式的一部分,還是必須對代碼進行更改?
save.php
文件的代碼:
include_once("db.inc.php");
$rguid = $_POST["r"];
$ip=substr($_SERVER['REMOTE_ADDR'], 0, 50);
$browser=substr($_SERVER['HTTP_USER_AGENT'], 0, 255);
$q1 = $_POST["q1"];
$q1a = $_POST["q1a"];
$q2 = $_POST["q2"];
$q2a = $_POST["q2a"];
$q3 = $_POST["q3"];
$q3a = $_POST["q3a"];
$q4 = $_POST["q4"];
$q4a = $_POST["q4a"];
$q5 = $_POST["q5"];
$q5a = $_POST["q5a"];
$q6 = $_POST["q6"];
$q6a = $_POST["q6a"];
$q7 = $_POST["q7"];
$q7a = $_POST["q7a"];
$q8 = $_POST["q8"];
$q8a = $_POST["q8a"];
$q9 = $_POST["q9"];
$q9a = $_POST["q9a"];
$q10 = $_POST["q10"];
$q10a = $_POST["q10a"];
$respondent_id = decode_respondent_guid($rguid);
$rcount=respondent_status($respondent_id);
if ($rcount==0) {
$proc = mysqli_prepare($link, "INSERT INTO tresults (respondent_id, ip, browser, q1, q1a, q2, q2a, q3, q3a, q4, q4a, q5, q5a, q6, q6a, q7, q7a, q8, q8a, q9, q9a, q10, q10a) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
mysqli_stmt_bind_param($proc, "issisisisisisisisisisis", $respondent_id, $ip, $browser, $q1, $q1a, $q2, $q2a, $q3, $q3a, $q4, $q4a, $q5, $q5a, $q6, $q6a, $q7, $q7a, $q8, $q8a, $q9, $q9a, $q10, $q10a);
mysqli_stmt_execute($proc);
$mysql_error = mysqli_error($link);
if ($mysql_error!="") {
printf("Unexpected database error: %s\n", $mysql_error);
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
exit();
} else
{
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
update_completion_status($respondent_id, 'Started');
header("Location: complete.php?r=".$rguid);
}
} else {
$proc = mysqli_prepare($link, "UPDATE tresults SET ip = ?, browser = ?, q1 = ?, q1a = ?, q2 = ?, q2a = ?, q3 = ?, q3a = ?, q4 = ?, q4a = ?, q5 = ?, q5a = ?, q6 = ?, q6a = ?, q7 = ?, q7a = ?, q8 = ?, q8a = ?, q9 = ?, q9a = ?, q10 = ?, q10a = ? WHERE respondent_id = ?;");
mysqli_stmt_bind_param($proc, "ssisisisisisisisisisisi", $ip, $browser, $q1, $q1a, $q2, $q2a, $q3, $q3a, $q4, $q4a, $q5, $q5a, $q6, $q6a, $q7, $q7a, $q8, $q8a, $q9, $q9a, $q10, $q10a, $respondent_id);
mysqli_stmt_execute($proc);
$mysql_error = mysqli_error($link);
if ($mysql_error!="") {
printf("Unexpected database error: %s\n", $mysql_error);
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
exit();
} else
{
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
update_completion_status($respondent_id, 'Started');
header("Location: complete.php?r=".$rguid);
}
}
我需要對index.php
文件進行哪些更改才能使用AJAX?
您只需將其發布到該文件
jQuery.ajax({
url: 'save.php',
data: $('form').serialize(),
type: "POST",
success: function(data) {
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.