1

我有一个应用程序,用户在相关文本输入中显示他们的评估名称、日期和时间。现在发生的情况是,当用户提交表单时,它会显示确认信息。

下面是代码(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>";



            ?>
4

1 回答 1

0

PHP 在服务器上运行,确认的用户在客户端。这意味着您必须使用 javascript/Jquery 在用户确认时触发事件,然后调用执行 UPDATES 或其他查询的 php。这可以通过 AJAX 调用、提交表单或使用 url 重定向来完成。

所以它应该是这样的:

  1. 首先A.php获取与用户数据一起提交的表单,进行一些查询并显示确认文本。这也有 javascript 代码将确认提交给另一个B.php,通常这将通过 AJAX 调用来完成,该调用将收到一些回报。
  2. 第二个B.php收到第一个的确认A.php,执行更新并返回一些东西来告诉第一个A.php所有查询都正常。如果出现问题,它可能会返回错误代码或详细说明错误的错误消息。
  3. A.php显示返回B.php给用户。

更新代码:

$.post("updatedatetime.php", $("#updateForm").serialize() ,function(data){
    alert(data);
}); 

最后function(data)是一个回调函数,这意味着它会在执行updatedatetime.php结束后执行。data它接收的变量是updatedatetime.php(你在那个 php 中回显的内容) 的返回

要检查您的 AJAX 是否正确执行、变量是否有值、返回等。我建议使用 Firebug,它会在其控制台中显示所有这些信息。

于 2012-11-13T11:00:53.393 回答