数据库驱动表单中的多个“提交”按钮

时间:2022-11-24 20:15:49

OK folks,

I am trying to display the contents of a database table along with a REMOVE or PUSH button. The REMOVE button will remove said item from the database. The PUSH button will push the item into another database, then FLAG said item by setting a variable pushed to true.

我试图显示数据库表的内容以及REMOVE或PUSH按钮。 REMOVE按钮将从数据库中删除所述项目。 PUSH按钮将项目推送到另一个数据库,然后通过将变量设置为true来标记所述项目。

I have the connection and the while loop echoing each item in the database along with a REMOVE button and a PUSH button. the echo looks like this:

我有连接和while循环回显数据库中的每个项目以及REMOVE按钮和PUSH按钮。回声看起来像这样:

echo '<form method='post' name='$ID' action='$_PHP_SELF'>
    <tr>
        <td width='5%' align='center'>$ID</td>
        <td width='75%'>$content</td>
        <td width='10%' align='center'><input type='submit' name='remove' id='remove' value='REMOVE'></td>
        <td width='10%' align='center'><input type='submit' name='push' id='push' value='PUSH'></td>
    </tr>
</form>';

The problem I'm running into, is how I can set a simple submit code that will check with form and either push or remove that specific item. If/Else or Switch statements won't work because I'm expecting dozens of database entries and I want the submit code itself to be dynamic as well.

我遇到的问题是,我如何设置一个简单的提交代码,它将检查表单并推送或删除该特定项目。如果/ Else或Switch语句不起作用,因为我期待数十个数据库条目,并且我希望提交代码本身也是动态的。

I started out with:

我开始时:

if(isset($_POST['push'])){
 echo "PUSH ".$ID." TO DATABASE";
}

if(isset($_POST['remove'])){
 echo "REMOVE ".$ID." FROM DATABASE";
}

but both of these are only returning the last $ID variable.

但这两个都只返回最后一个$ ID变量。

thanks in advance.

提前致谢。

3 个解决方案

#1


2  

An easy solution would be to change your HTML output to have two forms, with one submit for each. You could keep your PHP exactly the same.

一个简单的解决方案是将HTML输出更改为两个表单,每个表单一个提交。你可以保持你的PHP完全相同。

EDIT: In order to dynamically get the ID of the button pushed, then add a hidden data field with the ID in it, as below:

编辑:为了动态获取按下的按钮的ID,然后添加一个带有ID的隐藏数据字段,如下所示:

<tr>
    <td width='5%' align='center'>$ID</td>
    <td width='75%'>$content</td>
    <td width='10%' align='center'>
        <form method='post' action='$_PHP_SELF'>
            <input type='hidden' value='$ID' name='id'>
            <input type='submit' name='remove' id='remove' value='REMOVE'>
            </form>
    </td>
    <td width='10%' align='center'>
        <form method='post' name='$ID' action='$_PHP_SELF'>
            <input type='hidden' value='$ID' name='id'>
            <input type='submit' name='push' id='push' value='PUSH'>
        </form>
    </td>
</tr>

Then, your PHP would be:

那么,你的PHP将是:

$ID=mysql_real_escape_string($_POST['id']); //Encapsulate in this function to prevent bad IDs causing SQL injections.
if(isset($_POST['push'])){
 echo "PUSH ".$ID." TO DATABASE";
}

if(isset($_POST['remove'])){
 echo "REMOVE ".$ID." FROM DATABASE";
}

#2


2  

Make each relevant row it's own form, with it's own two push buttons. They can all submit to the same page. Also, keep the ID in a hidden field (and verify server side), not as the form name.

用它自己的两个按钮将每个相关的行设置为它自己的形式。他们都可以提交到同一页面。此外,将ID保存在隐藏字段中(并验证服务器端),而不是表单名称。

#3


0  

Follow this tutorial for creating a checkbox system and php processor. Php Checkboxes

按照本教程创建复选框系统和php处理器。 Php复选框

You can do it one of two ways. If you want the user to be able to delete several items at a time with one link you can do it like this.

你可以用两种方法之一做。如果您希望用户能够使用一个链接一次删除多个项目,您可以这样做。

Have the user select via checkbox as many rows as they want to. Then hit the "REMOVE" or "PUSH" button that sends it to another page with processes each row.

让用户通过复选框选择他们想要的多行。然后点击“REMOVE”或“PUSH”按钮,将其发送到另一页,每行都有进程。

The other way would be to have a REMOVE and PUSH button next to each row which will remove or push a single row at a time. I would recommend the first approach. If you follow that guide its pretty straight forward.

另一种方法是在每行旁边有一个REMOVE和PUSH按钮,一次删除或推送一行。我会推荐第一种方法。如果您遵循该指南,它非常直接。

#1


2  

An easy solution would be to change your HTML output to have two forms, with one submit for each. You could keep your PHP exactly the same.

一个简单的解决方案是将HTML输出更改为两个表单,每个表单一个提交。你可以保持你的PHP完全相同。

EDIT: In order to dynamically get the ID of the button pushed, then add a hidden data field with the ID in it, as below:

编辑:为了动态获取按下的按钮的ID,然后添加一个带有ID的隐藏数据字段,如下所示:

<tr>
    <td width='5%' align='center'>$ID</td>
    <td width='75%'>$content</td>
    <td width='10%' align='center'>
        <form method='post' action='$_PHP_SELF'>
            <input type='hidden' value='$ID' name='id'>
            <input type='submit' name='remove' id='remove' value='REMOVE'>
            </form>
    </td>
    <td width='10%' align='center'>
        <form method='post' name='$ID' action='$_PHP_SELF'>
            <input type='hidden' value='$ID' name='id'>
            <input type='submit' name='push' id='push' value='PUSH'>
        </form>
    </td>
</tr>

Then, your PHP would be:

那么,你的PHP将是:

$ID=mysql_real_escape_string($_POST['id']); //Encapsulate in this function to prevent bad IDs causing SQL injections.
if(isset($_POST['push'])){
 echo "PUSH ".$ID." TO DATABASE";
}

if(isset($_POST['remove'])){
 echo "REMOVE ".$ID." FROM DATABASE";
}

#2


2  

Make each relevant row it's own form, with it's own two push buttons. They can all submit to the same page. Also, keep the ID in a hidden field (and verify server side), not as the form name.

用它自己的两个按钮将每个相关的行设置为它自己的形式。他们都可以提交到同一页面。此外,将ID保存在隐藏字段中(并验证服务器端),而不是表单名称。

#3


0  

Follow this tutorial for creating a checkbox system and php processor. Php Checkboxes

按照本教程创建复选框系统和php处理器。 Php复选框

You can do it one of two ways. If you want the user to be able to delete several items at a time with one link you can do it like this.

你可以用两种方法之一做。如果您希望用户能够使用一个链接一次删除多个项目,您可以这样做。

Have the user select via checkbox as many rows as they want to. Then hit the "REMOVE" or "PUSH" button that sends it to another page with processes each row.

让用户通过复选框选择他们想要的多行。然后点击“REMOVE”或“PUSH”按钮,将其发送到另一页,每行都有进程。

The other way would be to have a REMOVE and PUSH button next to each row which will remove or push a single row at a time. I would recommend the first approach. If you follow that guide its pretty straight forward.

另一种方法是在每行旁边有一个REMOVE和PUSH按钮,一次删除或推送一行。我会推荐第一种方法。如果您遵循该指南,它非常直接。