我有一個應用程序(editsessionadmin.php)
,用戶在相關文本輸入中顯示其評估的名稱,日期和時間。現在,當用戶提交時,它將顯示一個確認信息,當用戶確認時,然後通過使用ajax,它將導航到updatedatetime.php
,在那裏它將更新數據庫中的評估時間和日期,並將成功或錯誤消息顯示在頂部editsessionadmin.php
頁面。成功/錯誤消息在提交表單時不斷消失
但我有一個小問題。
問題:使用div標籤,我能夠從updatedatetime.php
腳本更新後,檢索錯誤或成功的消息,並通過使用此jQuery代碼$("#targetdiv").html(data)
在editsessionadmin.php
腳本顯示。問題在於,當用戶提交表單時,它顯示消息,然後消息在表單提交後消失。我希望信息顯示在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>";
}
?>
@VIDesignz會發生什麼事是,當我在確認點擊「確定」,它顯示的信息,而該頁面提交(你可以看到瀏覽器選項卡上的頁面加載),當頁面提交自己,然後它消失了。我已經包括半結腸順便說一句,沒有變化 – user1819709
@VIDesignz是的這就是正確 – user1819709
@VIDesignz這可能是問題所在。我只是想從'updatedatetime.php'檢索回聲和運行這個'$(「#targetdiv」),將其放置在'targetdiv' div標籤HTML(數據);'。如果'#targetdiv'需要在頁面加載時設置,那麼這就是我出錯的地方,它應該設置什麼? – user1819709