简体   繁体   中英

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).

            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. 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. 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:

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. 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.

Form action = "...update.php".

By the way, the reason you're having problems is because you have a lot of invalid 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

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++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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