Codeigniter控制器中的2个按钮

时间:2014-07-17 03:03:49

标签: php codeigniter

以下是我的问题' if(!empty($ _ POST [' btn_editinventory]))'正在工作,但如果我点击btndeleteinventory它也将执行我的btneditinventory中的代码。任何人都知道如果在视图中有两个按钮,我将如何在我的控制器中纠正我的状况:

    <input type="button" value="Edit" name="btn_editinventory" id="btn_editinventory" onclick="validate_editinventory();"/>
   <input type="button" value="Delete" name="btn_deleteinventory" id="btn_deleteinventory" onclick="validate_deleteinventory();"/>

提前感谢您的帮助! :)

这是我的观点

    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/mystyle.css') ?>">

<div id="content">
<form action="edit_delete_others2" method="POST" id='frm_otherequips2' name="frm_otherequips2">
    <fieldset id="edit_other">
        <legend>Inventory Details</legend>
        <table>
            <tr>
                <?php $otherequips = $other2->Equipment;
                      $otherdesc = $other2->ItemDescription;
                      $otherID = $other2->ID;
                      $otherserialnum = $other2->SerialNumber;
                ?>
                <td>Equipment</td>
                <?php // if(isset($other)) { ?>
                <td><input type="text" id="txt_editotherequip" name="txt_editotherequip" value="<?php echo $otherequips // echo (isset($other->Equipment) AND $other->Equipment) ? $other->Equipment:'';  ?>"/></td>

                <td>Item Description</td>
                <td><input type="text" id="txt_editotherdesc" name="txt_editotherdesc" value="<?php echo $otherdesc //  echo (isset($other->ItemDescription) AND $other->ItemDescription) ? $other->ItemDescription:'';  ?>"/></td>
                <td style="visibility: hidden;"><input type="text" id="txt_editotherID" name="txt_editotherID" value="<?php echo $otherID  //echo (isset($other->ID) AND $other->ID) ? $other->ID:'';  ?>"/></td>
            </tr>
            <tr>
                <td>Serial Number</td>
                <td><input type="text" id="txt_editotherserialnum" name="txt_editotherserialnum" value="<?php echo $otherserialnum // echo (isset($other->SerialNumber) AND $other->SerialNumber)  ? $other->SerialNumber:'';  ?>"/></td>
                <?php // } ?>
            </tr>
            <tr>
                <td><input type="button"  id="btn_editother" name="btn_editother" value="Edit" onclick="validate_edit_otherequip();"/></td>
                <td><input type="button" id="btn_deleteother" name="btn_deleteother" class="btn_deleteother" value="Delete" onclick="validate_delete_otherequip();"/></td>
            </tr>
        </table>
    </fieldset>
</form>
</div>

这是我的javascript:

<script>
function validate_edit_otherequip(){
    if(document.getElementById('txt_editotherequip').value === ""){
        alert('Please Input Equipment!');
        return false;
    }
    else if(document.getElementById('txt_editotherdesc').value === ""){
        alert('Please Input Item Description');
        return false;
    }
    else if(document.getElementById('txt_editotherserialnum').value === ""){
        alert('Please Input Serial Number');
        return false;
    }
    else{
        var y;
        if(confirm("Are you sure you want to save updated data?") === true){
            var y = document.forms['frm_otherequips2'].submit();
        }
        else if(confirm("Are you sure you want to save updated data?") === false){
            return false;
        }
    }
}

function validate_delete_otherequip(){
    var x;
    if(confirm("Are you sure you want to delete this data?") === true){
        var x = document.forms['frm_otherequips2'].submit();
    }
    else if(confirm("Are you sure you want to delete this data?") === false){
        return false;
    }
}

这是我的控制器:

public function edit_delete_others2($id){
            $data_other['other2'] = $this->inventory_model->other_search($id);
            $this->load->view('homeview');
            $this->load->view('frm_otherequips2', $data_other);
            $this->load->view('footer_view');
}

我不知道如何在按钮中有条件。

1 个答案:

答案 0 :(得分:0)

至于你的两个按钮都有一个名称属性,它在你的控制器中很容易处理

public function edit_delete_others2($id){

    if($this->input->post('btn_editinventory')) {

        // Do your edit stuff

    } elseif($this->input->post('btn_deleteinventory')) { 

        // Do your delete stuff
    }
}

当您点击它时,该按钮永远不会在帖子中发送,这就是它工作的原因