dongweihuan8610 2015-04-25 12:03
浏览 80

如何在两个不同的按钮上执行相同的事件?

I have this piece of code below, supposing it will display no request when no data in database and it will display the request with an accept and reject button when they is data in database. How should i write my code in order to make the accept and reject button perform some action?

<?php 

        $travelRequest = $user->userTravelRequest($userid);
        if(!$travelRequest){
            echo '<div class="requestbox">
                  <p> You have no request from others at the moment. </p>
                  </div>';
        }

        else {
            foreach($travelRequest as $request){

                echo '<div class= "requestbox">
                      <p>'. $request->trip_name.'</p>
                      <p>Organised by '.$request->username.'</p>';

        ?>

                      <form method="POST">
                      <div class = "AR-btn">
                      <input type="button" name="accept" value="Accept"/>
                      <input type="button" name="reject" value="Reject"/>
                      <p></p>
                      </div>
                      </form>
                      </div>

        <?php   

            }

        }
        ?>
  • 写回答

3条回答 默认 最新

  • dousi9215 2015-04-25 12:15
    关注

    Use type="submit" instead of type="button", and give them the same name. Then they'll submit the form, and the script can test which button was used from the value of that parameter.

    <input type="submit" name="action" value="Accept"/>
    <input type="submit" name="action" value="Reject"/>
    

    You then test this with:

    if (isset($_POST['action'])) {
        if ($_POST['action'] == 'Accept') {
            // Add to database
        } elseif ($_POST['action'] == 'Reject') {
            // Delete from database
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真