douya2982
douya2982
2014-10-02 08:25

如何在单个HTML表单中处理两个不同的提交按钮?

已采纳

If I have a form as in the below script:

<html> 
 <?php
   session_start();

     $con=mysqli_connect("localhost","root","","QSTNS");
      if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

       $dsply=$_SESSION['q_indx'][$_SESSION['cindx']] ; 
      //echo $dsply ;
      $qstn = mysqli_query($con,"SELECT * FROM qstns where qid = '". $dsply ."'");

      $qstn = $qstn->fetch_array(MYSQLI_ASSOC);

       //echo $qstn['qname'] . "<br>";
       //echo $qstn['opta'] ."<br>";
       //echo $qstn['optb']."<br>";
       //echo $qstn['optc']."<br>";
       //echo $qstn['optd']."<br>";

        mysqli_close($con);
           ?>

        <body>

     <?php echo $qstn['qname'] ."<br>"; ?>
     <form action="prcs_ansr.php" method="post">
      <input type="radio" name="rply" value="A" /><?php echo $qstn['opta'] ; ?><br />
     <input type="radio" name="rply" value="B" /><?php echo $qstn['optb']; ?> <br />
     <input type="radio" name="rply" value="C" /><?php echo $qstn['optc']; ?><br />
     <input type="radio" name="rply" value="D" /><?php echo $qstn['optd']; ?> <br/>
     <input type="submit" value="previous">
     <input type="submit" value="next">
      </form>


     </body>
      </html>

I have two buttons in a single form and I want to process each one separately and for that I used isset() but it didn't work. The code for the next page is:

           <?php
          session_start() ;

          $con=mysqli_connect("localhost","root","","QSTNS");
         if (mysqli_connect_errno()) {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }

         if (isset($_POST['previous'])) {
         if($_SESSION['cindx']>0)
         $_SESSION['cindx']-=1;
         }
         if(isset($_POST['next']))
           {
         $res=mysqli_query($con, 'SELECT COUNT(*) FROM qstns');
         $row = mysqli_fetch_array($res);
         //echo $row[0];

         if($_SESSION['cindx']<$row[0]-1)
         $_SESSION['cindx']+=1;
           }
        //echo 'here';
        header('Location: quiz_start.php');

    mysqli_close($con); 
     ?>

Can anyone help?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duandang2123 duandang2123 7年前

    Your submit buttons have no name, only a value. So the things you check as isset are never set.

    <input type="submit" name="next" value="next"/>
    

    That will help.

    点赞 评论 复制链接分享
  • duanchen7703 duanchen7703 7年前

    Your submit buttons contain values, but not names. Thus there will be no array named $_POST['previous'] or $_POST['next'].

    You need to name your buttons firstly:

    <button type="submit" name="next" value="some_value" />
    <button type="submit" name="previous" value="some_value" />
    

    This will create the POST arrays needed. Although, your code would still not be functioning, because when the form is submitted, it sends all the form elements to the server. Thus, both previous and next will always be set, because they are both in the form.

    What you want to do is to give both submit buttons the same name, like for example action. And then get the value from it in your PHP code:

    <button type="submit name="action" value="next" />
    <button type="submit name="action" value="previous" />
    

    Once you have done this, you verify the input in PHP:

    <?php
        if( isset( $_POST['action'] ) && $_POST['action'] == 'next' ){
            // do stuff if action is next
        }
        if( isset( $_POST['action'] ) && $_POST['action'] == 'previous' ){
            // do stuff if action is previous
        }
    

    Another question of the same subject exists here:

    Two submit buttons in one form

    点赞 评论 复制链接分享

相关推荐