表单提交不同的按钮

时间:2016-12-12 09:35:45

标签: php html forms

美好的一天, 我有一个带有两个按钮的表单。一个用于“编辑”,另一个用于“删除”试用记录:

<form method="post" action="<?php echo adminurl('/pilotmanager/pilotsedit');?>">
<table class="PManager">
    <tr><th colspan="2">pilot edit form</th></tr>
    <tr>
        <td><b>First Name:</b></td>
        <td><input type="text" name="firstname" value="<?php echo $firstname;?>" /></td>
    </tr>
    <tr>         
        <td><b>Last Name:</b></td>
        <td><input type="text" name="lastname" value="<?php echo $lastname;?>" /></td>
    </tr>
    <tr>
        <td><b>Airline:</b></td>
        <td>
            <select name="code">
            <?php
            $allairlines = OperationsData::GetAllAirlines();
            foreach($allairlines as $airline)
                {
                    echo '<option value="'.$airline->code.'" '.$sel.'>'.$airline->name.'</option>';
                }
            ?>  
            </select>
        </td>
    </tr>
    <tr>
        <td><b>Transfer Hours:</b></td>
        <td><input type="text" name="transferhours" value="<?php echo $transferhours;?>" /></td>
    </tr>
    <tr> 
        <td><b>Hub:</b></td>
        <td>
            <select name="hub">
            <?php
            $allhubs = OperationsData::GetAllHubs();
            foreach($allhubs as $hub)
                {
                    echo '<option value="'.$hub->icao.'" '.$sel.'>'.$hub->icao.' - ' . $hub->name .'</option>';
                }
            ?>  
            </select>
        </td>
    </tr>
    <tr>
        <td><b>Total Flights:</b></td>
        <td><input type="text" name="totalflights" value="<?php echo $totalflights;?>" /></td>
    </tr>
    <tr> 
        <td><b>Total Pay:</b></td>
        <td><input type="text" name="totalpay" value="<?php echo $totalpay;?>" /></td>
    </tr>
    <tr> 
        <td><b>Pilot active:</b></td>
        <td>
        <?php 
        if(intval($pilot->retired) == 1) 
            {  
                $retsel='selected'; 
                $activesel = ''; 
            }
        else
            {
                $activesel = 'selected'; 
                $retsel = '';
            }
        ?>
            <select name="retired">
                <option value="0" <?php echo $activesel?>>Active</option>
                <option value="1" <?php echo $retsel?>>Inactive</option>
            </select>
        </td>
    </tr>
    <tr> 
        <td><b>Email Address:</b></td>
        <td><input type="text" name="email" value="<?php echo $email;?>" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="hidden" name="pilotid" value="<?php echo $pilotid;?>">
            <input type="hidden" name="action" value="saveprofile">
            <input type="hidden" name="action" value="deletepilot">
            <input type="submit" value="Save Changes" style="width:200px;">
            <input type="submit" value="Delete" style="width:200px;">
        </td>
    </tr>

在我的模块中,我有以下功能:

public function pilotsedit() {
    $pilotid = $_POST['pilotid'];
    $action = $_POST['action'];
    switch($action) {
        case 'saveprofile':
            $this->savepro($pilotid);
            echo '<script type="text/javascript">alert("Profile Updated!");</script>';
            $url = $_SERVER['HTTP_REFERER']; // right back to the referrer page from where you came.
            echo '<meta http-equiv="refresh" content="5;URL=' . $url . '">';
            break;
        case 'deletepilot':
            $this->deletePilot($pilotid);
            echo '<script type="text/javascript">alert("Pilot Deleted!");</script>';
            $url = $_SERVER['HTTP_REFERER']; // right back to the referrer page from where you came.
            echo '<meta http-equiv="refresh" content="5;URL=' . $url . '">';
            break;
    }
}

我正在尝试使用模块中的“case”语句分隔按钮提交,但是当我使用“编辑”按钮提交表单时,记录将被删除。请帮我看看我做错了什么。感谢

2 个答案:

答案 0 :(得分:3)

答案是使用按钮而不是输入标记。我做的是下面的事情:

<td colspan="2">
  <input type="hidden" name="pilotid" value="<?php echo $pilotid;?>">
  <button type="submit"  name="action"  value="saveprofile" style="width:200px;">Save Changes</button>
  <button type="submit"  name="action"  value="deletepilot" style="width:200px;">Delete Pilot</button>
</td>

在我的PHP模块中:

public function pilotsedit() {
    $pilotid = $_POST['pilotid'];
    $action = $_POST['action'];
    switch($action) {
        case 'saveprofile':
            $this->savepro($pilotid);
            break;
        case 'deletepilot':
            $this->deletePilot($pilotid);
            break;
    }
}

它的效果就像我期待的那样。

答案 1 :(得分:0)

你们都在saveprofile和deletepilot上发送这些动作

<input type="hidden" name="action" value="saveprofile">

<input type="hidden" name="action" value="deletepilot">

你应该将它们分开。