[英]success/error message keeps disappearing while submitting form
我有一個應用程序(editsessionadmin.php)
,用戶在相關的文本輸入中顯示評估的名稱,日期和時間。 現在,當用戶提交時,它將顯示一個確認,當用戶確認時,然后使用ajax,導航到updatedatetime.php
,它將在數據庫中更新評估的時間和日期,並在數據庫中顯示成功或錯誤消息。 editsessionadmin.php
頁面的頂部。
但是我有一個小問題。
問題:使用div標簽,我能夠從更新后檢索錯誤或成功的消息updatedatetime.php
腳本,並在顯示它editsessionadmin.php
使用此jQuery代碼腳本$("#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>";
}
?>
好的,遍歷代碼,執行表單提交,這就是ajax調用所需的全部內容
function submitupdate() {
$.ajax({
type: "POST",
url: "/updatedatetime.php",
data: $('#updateForm').serialize(),
success: function(html){
$("#targetdiv").html(html);
}
});
}
假設
/updatedatetime.php
正確計算並回顯更新的成功或失敗,
這是一個表格 ,向您顯示表單本身所需的最低要求。 您不需要在表單本身上放置任何方法或操作,只需在表單標簽上放置。
至於提交按鈕...您可以將其放置在任何地方,使其成為任何東西,只需確保為其提供一個ID並在其上附加一個點擊處理程序即可提交表單。
您必須在郵寄電話會議中刪除再次提交表單的行,並且刪除該行updateFormO.submit();
。 使用post方法,您可以進行ajax調用,並在成功函數中再次重新提交表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.