简体   繁体   English

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

[英]How to perform same event on two different buttons?

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   

            }

        }
        ?>

Use type="submit" instead of type="button" , and give them the same name. 使用type="submit"而不是type="button" ,并为其指定相同的名称。 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
    }
}

Try this code :- 试试这个代码:-

<?php
if (isset($_POST['accept']) && $_POST['accept'] == 'Accept' ) {
    // do what you want
     die("Accept is clicked");
} else if (isset($_POST['reject']) && $_POST['reject'] == 'Reject' ) {
    // do what you want
    die("Reject is clicked");
}
?>
<?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="submit" name="accept" value="Accept"/>
                      <input type="submit" name="reject" value="Reject"/>
                  <p></p>
                  </div>
              </form>
          </div>
        <?php
            }
        }
?>

Just try this logic..may help you 只要尝试这种逻辑..可能会帮助您

$query = "select data from database";
    .
    .
    .


 $data = $row['data'];

    if($data){  // reject button when they is data in database
       echo '<input type="submit" name="action" value="Reject"/>';
    }
    else{ // viceversa
       echo '<input type="submit" name="action" value="Accept"/>';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM