简体   繁体   English

提交表单时成功/错误消息不断消失

[英]success/error message keeps disappearing while submitting form

I have an application (editsessionadmin.php) where the user displays their assessment's name, date and time in the relevant text inputs. 我有一个应用程序(editsessionadmin.php) ,用户在相关的文本输入中显示评估的名称,日期和时间。 Now when the user submits, it will display a confirmation, when the user confirms, then by using ajax, it navigates to the updatedatetime.php where it will update the assessment's time and date in the database and display the success or error message at the top of the editsessionadmin.php page. 现在,当用户提交时,它将显示一个确认,当用户确认时,然后使用ajax,导航到updatedatetime.php ,它将在数据库中更新评估的时间和日期,并在数据库中显示成功或错误消息。 editsessionadmin.php页面的顶部。

But I have a small problems. 但是我有一个小问题。

Problem : Using a div tag, I am able to retrieve the error or success message after the update from the updatedatetime.php script and display it in the editsessionadmin.php script by using this jquery code $("#targetdiv").html(data) . 问题:使用div标签,我能够从更新后检索错误或成功的消息updatedatetime.php脚本,并在显示它editsessionadmin.php使用此jQuery代码脚本$("#targetdiv").html(data) Problem is though is that when the user submits the form, it displays the message and then the message disappears after form is submitted. 问题是,当用户提交表单时,它会显示消息,然后在提交表单后消息消失。 I want the message to be displayed at the top of the editsessionadmin.php page and not disappear. 我希望该消息显示在editsessionadmin.php页面的顶部,并且不会消失。 Why is it disappearing? 为什么消失了?

Below is the code for 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;


        }

        ?>

Below is the code for updatedatetime.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();
  }

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>";

}

        ?>

Ok, to weed through your code, to perform the form submit, this is all you need for the ajax call 好的,遍历代码,执行表单提交,这就是ajax调用所需的全部内容

function submitupdate() {    

    $.ajax({
        type: "POST",
        url: "/updatedatetime.php",
        data: $('#updateForm').serialize(),
        success: function(html){
            $("#targetdiv").html(html);
        }
     });        
}

This assumes the 假设

/updatedatetime.php

Calculates correctly and echos either the success or failure of the update, 正确计算并回显更新的成功或失败,

Here is a FIDDLE to show you the minimum you need for the form itself. 这是一个表格 ,向您显示表单本身所需的最低要求。 You don't need to put any method, or action on the form itself, just the form tags. 您不需要在表单本身上放置任何方法或操作,只需在表单标签上放置。

As far as the button to submit...you can put that anywhere, make it anything, just make sure you give it an ID and attach a click handler to it to submit the form. 至于提交按钮...您可以将其放置在任何地方,使其成为任何东西,只需确保为其提供一个ID并在其上附加一个点击处理程序即可提交表单。

You must remove the line that submits the form again in the post call and is exaclty this line updateFormO.submit(); 您必须在邮寄电话会议中删除再次提交表单的行,并且删除该行updateFormO.submit(); . With the post method you make an ajax call and in the success function you resubmit the form again. 使用post方法,您可以进行ajax调用,并在成功函数中再次重新提交表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM