I currently have a setup to save to a MySQL database using PHP:
index.php
- includes form for user to complete
validate.js
- a Javascript validation file to check users input before submission to the DB
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:
complete.php
- the final page.
I am aware that with the implementation of AJAX I can remove the page re-directs to make the user experience cleaner.
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?
Code for the save.php
file:
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?