简体   繁体   中英

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. 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"/>';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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