我目前正在做一个表单,客户将不得不做调查表单,我将有一个AJAX"保存"按钮,让我保存数据到数据库中,当客户没有设法完成表单本身,然后当客户再次登录,他们做了一半的表单将再次弹出,并要求他们继续完成调查表单。
是否有可能在哪里AJAX/javascript/jQuery可以与php代码在它的工作(因为插入查询)?
不是很确定与AJAX和所有所以感谢帮助!
这是"保存"按钮。
<input type="button" onClick="save();" value="Save">
这是一个插入查询,它将被插入到数据库中。
<?php
include("dbFunctions.php");
$idQuery = "SELECT id,question,input FROM db_ExtApp1.scFormLayout WHERE surveyID ='$lastID'";
$idResult = sqlsrv_query($conn, $idQuery);
while ($row = sqlsrv_fetch_array($idResult)) {
$fcID = $row['id'];
$question = $row['question'];
$surveyTitle = $_SESSION['surveyTitle'];
$input = $row['input'];
if (isset($_POST['qns' . $fcID])) {
$answer = implode(",", $_POST['qns' . $fcID]);
$newAns = str_replace($singleQuote,$changeQuote,$answer);
} else {
$newAns = '';
}
if (isset($_POST['others'.$fcID])) {
$others = $_POST['others' . $fcID];
$newOthers = str_replace($singleQuote,$changeQuote,$others);
}else {
$newOthers = 'N.A.';
}
$connectionInfo['ConnectionPooling']=0; // this creates a new connection on the next line...
$newconn = sqlsrv_connect($serverName, $connectionInfo);
if ($input != 'Normal text line, no input required*') {
$query = "INSERT INTO db_ExtApp1.scFormResult(surveyID, answer, others, title, question, name)
VALUES ('$lastID','$newAns','$newOthers', '$surveyTitle','$question', '$name')";
$status = sqlsrv_query($newconn, $query);
} }
if ($status === false) {
die(print_r(sqlsrv_errors(), true));
}
sqlsrv_close($conn);
可以使用jquery $.ajax()将数据从客户端发送到PHP。(如)
$.ajax({
url : 'path/to/php/page',
data : { id : '1', name : 'name' },
dataType : 'JSON',
type : 'POST',
cache: false,
success : function(succ) {
alert(succ);
},
error : function(err) {
alert(err);
}
});
然后在PHP页面中,使用$_POST[]捕获数据并将其插入数据库。
$id = $_POST['id'];
$name = $_POST['name'];