简体   繁体   English

PHP:两个按钮提交

[英]PHP: Two buttons to submit

I've been trying to submit two buttons, one to delete and one to edit/add. 我一直在尝试提交两个按钮,一个要删除,一个要编辑/添加。 But I can't seem to get it right. 但我似乎无法正确解决。

HTML: HTML:

    <!-- Edit/Delete Account Form -->
        <form method="get" action="edit.php" onsubmit="setTimeout('location.reload()');">
          <div id="edit-form" class="modal hide fade">
                    <div class="modal-header">
                        <a class="close" data-dismiss="modal">&times;</a>
                        <h4><?php _e('Edit/Delete Account'); ?></h4>
                    </div>
                    <div class="modal-body">
                        <div id="message"></div>
                            <div class="control">
                <div id="edit-account-form">
    <table id="add-account" class="table">
        <tbody>
            <tr>
            <td valign="middle">
            Account Number
            </td>
            <td>
            <input type="hidden" id="editid" name="editid" value=""/>
            <input type="text" id="number" name="number" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Account Description
            </td>
            <td>
            <input type="text" id="description" name="description" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Level 01
            </td>
            <td>
            <input type="text" id="level01" name="level01" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Level 02
            </td>
            <td>
            <input type="text" id="level02" name="level02" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Level 03
            </td>
            <td>
            <input type="text" id="level03" name="level03" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Level 04
            </td>
            <td>
            <input type="text" id="level04" name="level04" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Tax
            </td>
            <td>
            <input type="text" name="account" value="Enter Some text here "/>
            </td>
            </tr>
            <tr>
            <td>
            Allow General Journals
            </td>
            <td>
            <select>
            <option selected disabled>Select</option>
            <option>Yes</option>
            <option>No</option>
            </select>
            </td>
            </tr>
            <tr>
            <td>
            Allow Payments
            </td>
            <td>
            <select>Select</select>
            </td>
            </tr>
            <tr>
            <td>
            Allow Expense Claims
            </td>
            <td>
            <select>Select</select>
            </td>
            </tr>
            <tr>
            <td>
            Show On Dashboard
            </td>
            <td>
            <select>Select</select>
            </td>
            </tr>
        </tbody>
</table>
                </div>
                            </div>
                        </div>
                    <div class="modal-footer">
                        <button type="submit" data-complete-text="<?php _e('Done'); ?>" name="add" class="btn btn-primary pull-right"><?php _e('Submit'); ?></button>
                        <p class="pull-left"><button type="submit" name="delete" class="btn btn-primary pull-right"><?php _e('Delete'); ?></button></p>

                    </div>
                </div>
        </form>

PHP: PHP:

<?php

    //Account Details
    $number = $_GET['number'];
    $description = $_GET['description'];
    $level01 = $_GET['level01'];
    $level02 = $_GET['level02'];
    $level03 = $_GET['level03'];
    $level04 = $_GET['level04'];
    $id = $_GET['editid'];

    $dbc = mysqli_connect('localhost', 'root', 'root', 'accounting') or die('Connection error!');

    if(isset($_POST['delete'])) { 
     $check = "DELETE FROM accountSlave WHERE `id` = '$id' ";
     mysqli_query($dbc, $check) or die('Database error, delete account!');
     }
    if(isset($_POST['add'])) {      
     //$check = "DELETE FROM accountSlave WHERE `id` = '$id' ";
    $check = "UPDATE accountSlave SET `accountNumber` = '$number', `accountDescription` = '$description', `accountLevel1` = '$level01', `accountLevel2` = '$level02', `accountLevel3` = '$level03', `accountLevel4` = '$level04' WHERE `id` = '$id'";
     mysqli_query($dbc, $check) or die('Database error, add account!');
     }

    header('location:master-accounts.php');

?>

It works when I have one query. 当我有一个查询时,它起作用。 I've tried one query to add and to delete and it worked. 我试过一个查询来添加和删除,它的工作。 But I need to submit a delete if delete button clicked and vice versa. 但是如果单击删除按钮,我需要提交一个删除,反之亦然。

I followed this post to try get it right multiple buttons on a form 我按照这篇文章尝试正确使用表单上的多个按钮

Any help or suggestions will be appreciated! 任何帮助或建议,将不胜感激!

You need to use $_GET instead of $_POST . 您需要使用$_GET而不是$_POST It should be like 应该像

if(isset($_GET['delete'])) { 

and

if(isset($_GET['add'])) { 

Because your form submit method is GET not the POST 因为您的表单提交方法是GET而不是POST

You have to check which button is pressed and according to that do your operation. 您必须检查按下了哪个按钮,并据此进行操作。

Also you have to use $_GET not $_POST because your form method is GET . 另外,您必须使用$_GET而不是$_POST因为您的form methodGET

if(isset($_GET['add'])) {
    // code for add
} elseif(isset($_GET['delete'])) {
    // code for delete
}

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

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