简体   繁体   中英

Submitting multiple forms with one button in PHP

I have some dynamically added text boxes along with some static ones. The problem I'm having is when I remove an item from one of the dynamic forms, unless I have it wrapped with form tags, it will only remove the last item from the list. However, when I wrap it in form tags, then the static text boxes won't post. Basically I'm just wondering how to properly use tags if I want to submit textboxes.

For example,

<form method="post">
<textbox 1>
<textbox 2>
<textbox 3>

<?
 $tasks->task_form();

?>
 <another textbox> //these text boxes don't POST when the dynamic forms have tags wrapped around them
 <another textbox>
<?    
 $users->dynamic_form_2();
?>

</form>

For example, here's the task_form

function task_form(){

//textbox and submit to add tasks here (omitted to keep shorter)

foreach (array_combine($task, $task_num) as $task => $task_num){
            ?>
            <form method="post">
            <tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
            <td><input type="submit" name="remove_task" value="Remove"/></td>
            <td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
            <td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
            </form>
            <?
        }

}

If I don't place the form tags around the dynamic forms, then the last item from that form is removed regardless of which one I try to remove. However, with the form tags, then the values from some of the text boxes don't POST at all.

This is probably not the cleanest solution, but you could use a toolkit like jQuery and on click of one of your submit button, simply send ajax queries for each forms in parallel, and when everything is done, redirect to another page.

I would still strongly advise that you review your forms, it should probably be a single form!

Nope, you can't submit multiple forms with pure HTML/PHP.

You'll have to

  • Rework your forms to submit all at the same time (this usually sounds harder than it actually is) or
  • Rework your server logic to check which of several forms was just submitted (probably using isset($_POST['whatever']) ).

An AJAX solution could work, but there's pros and cons with that too.

Simply remove the tag from your loop:

foreach (array_combine($task, $task_num) as $task => $task_num){
            ?>
            <tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
            <td><input type="submit" name="remove_task" value="Remove"/></td>
            <td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
            <td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
            <?
        }

As other explained, you can not nest forms. Anyway, your fields belong to the same form, so you can simply add the fields you need.

Personnally, I have move to JQuery to handle the submission of forms as it provides a better experience for the user (you can give him feedback immediately instead of looping to the server), and it is also liberating as you don't really need form tags or even input (you can use writable divs if you like).

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