简体   繁体   English

如何在PHP的FORM ACTION中的循环中为ACTION赋值

[英]how to assign a value to ACTION in a loop in FORM ACTION in PHP

In my MySQL database, the user can update values in the database via a result table by clicking on an UPDATE button next to the field that the user wants to update the value of (see below). 在我的MySQL数据库中,用户可以通过结果表来更新数据库中的值,方法是单击用户要更新其值的字段旁边的UPDATE按钮(请参见下文)。

            car         colour      update field
1           Mercedes    blue        UPDATE BUTTON
2           VW          grey        UPDATE BUTTON
3           Opel        red         UPDATE BUTTON
4           Alfa Romeo  white       UPDATE BUTTON
5           Renault     pink        UPDATE BUTTON

The table is produced by a "foreach" loop. 该表由“ foreach”循环产生。 Each time a new row is written in the table the UPDATE BUTTON is part of a FORM ACTION statement with a variable (ie $New_Update_File_Name) for the file name after ACTION. 每次在表中写入新行时,UPDATE BUTTON都是FORM ACTION语句的一部分,其中ACTION之后的文件名带有变量(即$ New_Update_File_Name)。 That variable is filled with a unique filename in every run of the loop. 在循环的每次运行中,该变量都填充有唯一的文件名。 In my verification echo statement $New_Update_File_Name appears exactly as I want it in every run of the loop as shown by the output of my echo statements: 在我的验证回显语句中,$ New_Update_File_Name完全按照我希望的方式在每次循环运行中显示,如我的回显语句的输出所示:

cars[Mercedes] = blue
New_Update_File_Name = Update_blue-SESSION.php
cars[VW] = grey
New_Update_File_Name = Update_grey-SESSION.php
cars[Opel] = red
New_Update_File_Name = Update_red-SESSION.php
cars[Alfa Romeo] = white
New_Update_File_Name = Update_white-SESSION.php
cars[Renault] = pink
New_Update_File_Name = Update_pink-SESSION.php

However, if I click on any of the UPLOAD BUTTONs they all have the same value as the first UPLOAD BUTTON: Update_blue-SESSION.php, rather than what I want, ie each UPLOAD BUTTON pointing to the correct New_Update_File_Name for that row. 但是,如果我单击任何一个上传按钮,它们都具有与第一个上传按钮相同的值:Update_blue-SESSION.php,而不是我想要的,即每个指向该行的正确New_Update_File_Name的上传按钮。 I cannot find out why this is not happening. 我不知道为什么这种情况没有发生。 Any help greatly appreciated. 任何帮助,不胜感激。 My code appears below. 我的代码显示在下面。

<?php

// BUILD THE CAR TABLE WITH UPDATE BUTTON
$cars = array("Mercedes"=>"blue","VW"=>"grey","Opel"=>"red","Alfa Romeo"=>"white","Renault"=>"pink");
$counter = 1;
echo "<table>
<tr>
<th></th>
<th>car</th>
<th>colour</th>
<th>update field</th>";
foreach ($cars as $field => $value)
{
    $Update_File_Name = 'Update_-SESSION.php';
    $New_Update_File_Name = substr_replace($Update_File_Name,$cars[$field],-12, 0);
    echo "cars[$field] = ".$cars[$field]."<br />";
    echo "New_Update_File_Name = ".$New_Update_File_Name."<br />";

    echo "<tr class='alt'>
    <td>$counter</td>
    <td>$field</td>
    <td>$value</td>
    <td><form action='$New_Update_File_Name' method='post'>
    <input type='submit' value='update'></td>
    </tr>";
    $counter++;
    }
echo "</table>";
?>

For starters, calling a different file for each action is a really bad idea. 对于初学者来说,为每个操作调用一个不同的文件是一个非常糟糕的主意。 What do you do when you want to update code that is the same in each? 当您想要更新每个相同的代码时,您会怎么做?

You should instead, have them all send to the same one, and send one extra variable ( such as color ) to determine what you want to do. 相反,您应该让它们全部发送到同一对象,然后发送一个额外的变量(例如color)来确定您要执行的操作。

Form action = "...update.php". 表单操作=“ ... update.php”。

By the way, the reason you're having problems is because you have a lot of invalid HTML. 顺便说一句,您遇到问题的原因是因为您有很多无效的HTML。 You are not closing your form fields, and therefore the first call is set to them all. 您没有关闭表单字段,因此将第一个调用设置为全部。

<td>
<form action='$New_Update_File_Name' method='post'>
    <input type="hidden" value="<?php echo $value; ?>" name="color" />
    <input type='submit' name="submit" value='update'>
</form>
</td>

Note: I added a hidden input. 注意:我添加了隐藏的输入。 In your update.php file, you can do 在您的update.php文件中,您可以执行

if($_POST['color'] == "blue") {
.... do stuff
}

Because you have opened a form in loop with new action but havn't closed the form inside the loop therefore only first form action will be treated create a single form for each new action try this 因为您已使用新动作在循环中打开了一个表单,但尚未在循环内关闭该表单,因此仅第一个表单动作将被视为为每个新动作创建一个表单,请尝试

foreach ($cars as $field => $value)
{
    $Update_File_Name = 'Update_-SESSION.php';
    $New_Update_File_Name = substr_replace($Update_File_Name,$cars[$field],-12, 0);
    echo "cars[$field] = ".$cars[$field]."<br />";
    echo "New_Update_File_Name = ".$New_Update_File_Name."<br />";

    echo "<tr class='alt'>
    <td>$counter</td>
    <td>$field</td>
    <td>$value</td>
    <td><form action='$New_Update_File_Name' method='post'>
    <input type='submit' value='update'></form></td>
    </tr>";
    $counter++;
    }

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

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