ユーザーが関連するテキスト入力に評価名、日付、時刻を表示するアプリケーションがあります。ここで、ユーザーがフォームを送信すると、確認が表示されます。
以下はコード(editsessionadmin.php)です:
<script>
function showConfirm(){
var examInput = document.getElementById('newAssessment').value;
var dateInput = document.getElementById('newDate').value;
var timeInput = document.getElementById('newTime').value;
var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + examInput + "\n" + "Date: " + dateInput + "\n" + "Time: " + timeInput);
}
</script>
<?php
$sessionquery = "
SELECT SessionId, SessionName, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionName
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$moduleId);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
if($sessionnum ==0){
echo "<p>Sorry, You have No Assessments under this Module</p>";
} else {
$sessionHTML = '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while ( $sessionqrystmt->fetch() ) {
if(time() > strtotime($dbSessionDate." ".$dbSessionTime)){
$class = 'red';
} else {
$class = 'green';
}
$sessionHTML .= sprintf("<option value='%s' style='color: %s'>%s - %s - %s</option>", $dbSessionId, $class, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;
}
$sessionHTML .= '</select>';
$assessmentform = "<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post'>
<p><strong>Assessments:</strong> {$sessionHTML} </p>
</form>";
echo $assessmentform;
}
$editsession = "<form action=".htmlentities($_SERVER['PHP_SELF'])." method='post' 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>
<p><input id='updateSubmit' type='submit' value='Update Date/Start Time' name='updateSubmit' onClick='myClickHandler(); return false;'/></p>
</form>
";
echo $editsession;
}
?>
<script type="text/javascript">
function myClickHandler(){
if(editvalidation()){
showConfirm();
}
}
</script>
ここで、UPDATEコマンドを実行して、ユーザーが確認を確認した後、データベース内の試験の日時を更新し、SELECTクエリをコンパイルして、更新が発生した場合は成功したことをエコーし、そうでない場合はエラーが発生したことをエコーします。 。
私の質問は、まず第一に、以下のコードが正しいことです。第二に、確認が確認された後にコマンドを実行するように、このコードをどこに配置すればよいですか?
<?php
$sessionname = (isset($_POST['Assessmentnew'])) ? $_POST['Assessmentnew'] : '';
$sessiondate = (isset($_POST['Datenew'])) ? $_POST['Datenew'] : '';
$sessiontime = (isset($_POST['Timenew'])) ? $_POST['Timenew'] : '';
$updatesql = "UPDATE Session SET SessionDate = ?, SessionTime = ? WHERE SessionName = ?";
$update = $mysqli->prepare($updatesql);
$update->bind_param("sss", $sessiondate, $sessiontime, $sessionname);
$update->execute();
$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("sss", $sessionname, $sessiondate, $sessiontime);
// 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();
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 occurred, your Assessment's new Date and Time have not been updated</span>";
?>
アップデート:
以下はeditsessionadmin.phpのコードです
<script>
function showConfirm(){
var examInput = document.getElementById('newAssessment').value;
var dateInput = document.getElementById('newDate').value;
var timeInput = document.getElementById('newTime').value;
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();
}
}
function submitform()
{
$.post("updatedatetime.php", $("#updateForm").serialize() ,function(data){
var updateFormO = document.getElementById("updateForm");
updateFormO.submit();
});
}
</script>
<?php
$sessionquery = "
SELECT SessionId, SessionName, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionName
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$moduleId);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
if($sessionnum ==0){
echo "<p>Sorry, You have No Assessments under this Module</p>";
} else {
$sessionHTML = '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while ( $sessionqrystmt->fetch() ) {
if(time() > strtotime($dbSessionDate." ".$dbSessionTime)){
$class = 'red';
} else {
$class = 'green';
}
$sessionHTML .= sprintf("<option value='%s' style='color: %s'>%s - %s - %s</option>", $dbSessionId, $class, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;
}
$sessionHTML .= '</select>';
$assessmentform = "<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post'>
<p><strong>Assessments:</strong> {$sessionHTML} </p>
</form>";
echo $assessmentform;
}
$editsession = "<form action=".htmlentities($_SERVER['PHP_SELF'])." method='post' 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>
<p><input id='updateSubmit' type='submit' value='Update Date/Start Time' name='updateSubmit' onClick='myClickHandler(); return false;'/></p>
</form>
";
echo $editsession;
}
?>
<script type="text/javascript">
function myClickHandler(){
if(editvalidation()){
showConfirm();
}
}
</script>
上記のコードでは、jquery / ajax関数を追加しましたsubmitform()
。確認後、ページのバックグラウンドで投稿が実行されupdatedatetime.php
ます。更新を表示し、ステートメントを選択する場所があります。問題は、editsessionadmin.phpページで更新を行っていないかエコーを表示していないことです
以下は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();
}
$sessionname = (isset($_POST['Assessmentnew'])) ? $_POST['Assessmentnew'] : '';
$sessiondate = (isset($_POST['Datenew'])) ? $_POST['Datenew'] : '';
$sessiontime = (isset($_POST['Timenew'])) ? $_POST['Timenew'] : '';
$updatesql = "UPDATE Session SET SessionDate = ?, SessionTime = ? WHERE SessionName = ?";
$update = $mysqli->prepare($updatesql);
$update->bind_param("sss", $sessiondate, $sessiontime, $sessionname);
$update->execute();
$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("sss", $sessionname, $sessiondate, $sessiontime);
// 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();
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>";
?>