简体   繁体   English

PHP保存到MySQL转换为使用AJAX

[英]PHP Save to MySQL convert to using AJAX

I currently have a setup to save to a MySQL database using PHP: 我目前有一个设置要使用PHP保存到MySQL数据库:

index.php - includes form for user to complete index.php包括供用户填写的表格

validate.js - a Javascript validation file to check users input before submission to the DB validate.js一个Java验证文件,用于在提交给数据库之前检查用户输入

save.php - if the users responses have been validated that are redirected to this file, which upon successful saving to the DB, they are then re-directed to: save.php如果已验证用户响应,并将其重定向到此文件,则将其成功保存到数据库后,将其重定向到:

complete.php - the final page. complete.php最后一页。

I am aware that with the implementation of AJAX I can remove the page re-directs to make the user experience cleaner. 我知道,借助AJAX的实现,我可以删除页面重定向,以使用户体验更加清晰。

However, before looking at re-coding - can I use the existing save.php as part of the new AJAX way of working or do I have make to make changes to the code? 但是,在进行重新编码之前-我可以将现有的save.php用作新的AJAX工作方式的一部分,还是必须对代码进行更改?

Code for the save.php file: 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);
}
}

What changes would I need to make to my index.php file to utilise AJAX? 我需要对index.php文件进行哪些更改才能使用AJAX?

you would just post to that file 您只需将其发布到该文件

jQuery.ajax({
    url: 'save.php',
    data: $('form').serialize(),
    type: "POST",
    success: function(data) {

    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM