查询表单提交后验证 PHP $_POST

Validating PHP $_POST after query form submission

确保自引用 PHP 表单上的逻辑测试正确识别用于提交表单的 HTML 按钮的最佳方法是什么,而该表单实际上是通过 jquery.submit() - 而不是点击按钮本身?

<!DOCTYPE html>
<html>

  <head>
      <meta charset="utf-8">
       <title> Total Tutor </title>
        <link type="text/css" rel="stylesheet" href="course.css"> 
          <script src="../jquery.js"></script>

  </head>
  <body>

   <script>
     $(document).ready(function() {

     //event that triggers the javascript/jquery form submission

     $('li').dblclick(function() 
            {
                selectedCourse=$(this).text();
                  sessionStorage.removeItem('selectedCourse');
                    sessionStorage.setItem('selectedCourse', selectedCourse);
                     editForm(); 
            });

      //the function called by the normal button submission and the alternative jQuery only submission: note the non jQuery one sets the $-POST variable fine

     function editForm() {

                     var s= confirm( 'Would you like to edit ' +selectedCourse+ '');

                      if(s)
                          {
                         alert('form is called');   
                          } 
                     }

      $('#edit-button').click(function() { editForm(); });

         });

         </script> 

    <?php 
        if(!isset($_POST['submit']))
        {
     ?>

      <ul>
         <li>Maths</li>
         <li>English</li>
      </ul>

      <form  id="edit_delete" method="POST" action=" <?php echo $_SERVER['PHP_SELF']; ?>">
             <button type="submit"  id="edit-button"  name="submit"  value="edit"    ><img  src="../images/pencil.png"></button>
              <button  type="submit" id="delete-button"  name="submit" value="delete"  ><img src="../images/deleteo.png"></button>
       </form>

       }

      <?php 
        else
        {
           if($_POST['submit']=='edit')
            {
             ?>
                <p>Thank you for selecting edit</p>
           <?php
            }

           if($_POST['submit']=='delete')
            {
             ?>
                <p>Thank you for selecting delete</p>
           <?php
            }

        }
       ?>
 </body>
</html>

使用变量指示数据的发布方式。

在您的 html 表格中:

<input type="hidden" name="wasClicked" id="wasClicked" value="1" />

在你的jQuery中:

$('#wasClicked').val("0");

然后在你的PHP中测试$_POST['wasClicked']看看是0还是1