【发布时间】:2012-11-14 21:13:12
【问题描述】:
我有一个应用程序(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 发生的情况是,当我在确认中单击“确定”时,它会在页面提交时显示消息(您可以在浏览器选项卡上看到页面加载),当页面提交给自己,然后它就消失了。顺便说一句,我已经包括了分号,没有变化
-
@VIDesignz 这可能是问题所在。我只是想从
updatedatetime.php中检索回声,并通过运行$("#targetdiv").html(data);将其放在targetdivdiv 标记中。如果在页面加载时需要设置#targetdiv,那么这就是我出错的地方,应该设置什么? -
@VIDesignz 尝试将数据更改为 html 但仍然做同样的事情
-
@VIDesignz 我不太习惯使用 ajax,我一直在尝试遵循我在研究中找到的某些示例,然后将其包含在代码中。如果您有更好的方法,那么如果您能展示一个更可靠和更清洁的方法,将不胜感激
-
@VIDesignz 我没有包含提交课程和模块按钮时的代码以尝试减少代码。你想让我把它包括进来吗?当您单击更新日期/开始时间按钮时会发生什么情况,然后它将显示确认框,如果用户在确认框中单击确定,那么它将提交表单(这是到它自己的页面)但同时页面正在加载它显示成功回显,然后当它被提交时,回显消息消失,回显消息应该在表单提交后显示并且提交后不会消失