2017-05-28 60 views
1

确保自引用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> 

回答

1

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

在你的HTML表单:

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

在你的jQuery:

$('#wasClicked').val("0"); 
在你的PHP

然后,测试$_POST['wasClicked'],看它是否是01

+0

不会这只是告诉我表单已经提交 - 它不会告诉我编辑或删除按钮是否被触发? – jon

+0

如果'$ _POST ['wasClicked'] ==“0”'的值,那么它是由编辑按钮触发的。这里的关键原则是将隐藏的输入字段设置为您提交的值_before_,以查看哪个方法触发了提交 –