我有一个应用程序(editsessionadmin.php)
,用户在相关文本输入中显示他们的评估名称、日期和时间。现在当用户提交时,它会显示一个确认,当用户确认时,然后通过使用ajax,它会导航到updatedatetime.php
它将在数据库中更新评估的时间和日期,并在顶部显示成功或错误消息editsessionadmin.php
页。
但我有一个小问题。
问题:使用 div 标签,我可以在脚本更新后检索错误或成功消息,并使用此 jquery 代码updatedatetime.php
将其显示在脚本中。问题是,当用户提交表单时,它会显示消息,然后在提交表单后消息消失。我希望消息显示在页面顶部而不是消失。为什么会消失?editsessionadmin.php
$("#targetdiv").html(data)
editsessionadmin.php
下面是editsessionadmin.php的代码
<script>
function submitform() {
$.ajax({
type: "POST",
url: "/updatedatetime.php",
data: $('#updateForm').serialize(),
success: function(html){
$("#targetdiv").html(html);
}
});
}
function showConfirm(){
var examInput = document.getElementById('newAssessment').value;
var dateInput = document.getElementById('newDate').value;
var timeInput = document.getElementById('newTime').value;
if (editvalidation()) {
var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + examInput + "\n" + "Date: " + dateInput + "\n" + "Time: " + timeInput);
if (confirmMsg==true)
{
submitform();
}
}
}
$('body').on('click', '#updateSubmit', showConfirm);
</script>
<h1>EDIT AN ASSESSMENT'S DATE/START TIME</h1>
<p>You can edit assessment's Date and Start time on this page. Only active assessments can be editted.</p>
<div id="targetdiv"></div>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
<table>
<tr>
<th>Course: INFO101</th>
<th>Module: CHI2513</th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" /></p>
</form>
....
<?php
$editsession = "<form id='updateForm'>
<p><strong>New Assessment's Date/Start Time:</strong></p>
<table>
<tr>
<th>Assessment:</th>
<td><input type='text' id='newAssessment' name='Assessmentnew' readonly='readonly' value='' /> </td>
</tr>
<tr>
<th>Date:</th>
<td><input type='text' id='newDate' name='Datenew' readonly='readonly' value='' /> </td>
</tr>
<tr>
<th>Start Time:</th>
<td><input type='text' id='newTime' name='Timenew' readonly='readonly' value=''/><span class='timepicker_button_trigger'><img src='Images/clock.gif' alt='Choose Time' /></span> </td>
</tr>
</table>
<div id='datetimeAlert'></div>
<button id='updateSubmit'>Update Date/Start Time</button>
</form>
";
echo $editsession;
}
?>
以下是 updatedatetime.php 的代码:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
echo 'sumbit successful';
$sessionname = (isset($_POST['Assessmentnew'])) ? $_POST['Assessmentnew'] : '';
$sessiondate = (isset($_POST['Datenew'])) ? $_POST['Datenew'] : '';
$sessiontime = (isset($_POST['Timenew'])) ? $_POST['Timenew'] : '';
$formatdate = date("Y-m-d",strtotime($sessiondate));
$formattime = date("H:i:s",strtotime($sessiontime));
$updatesql = "UPDATE Session SET SessionDate = ?, SessionTime = ? WHERE SessionName = ?";
$update = $mysqli->prepare($updatesql);
$update->bind_param("sss", $formatdate, $formattime, $sessionname);
$update->execute();
echo 'update successful';
$query = "SELECT SessionName, SessionDate, SessionTime FROM Session WHERE SessionName = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s", $sessionname);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionName, $dbSessionDate, $dbSessionTime);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
echo 'select successful';
if ($numrows == 1){
echo "<span style='color: green'>Your Assessment's new Date and Time have been updated</span>";
}else{
echo "<span style='color: red'>An error has occured, your Assessment's new Date and Time have not been updated</span>";
}
?>