当我的按钮在表单之外时,获取复选框的值

I have some problem with a form which contains foreach and I input submit button outside of form (out of foreach) but when i select checkboxes, it get only first checkbox!!! how it can be done ? Thanks

Remarque: I can't put the form around foreach.

{foreach from=$aWSForServer2 key=ws item=webservice}
    <form action="index.php" method="post" onSubmit="return confirm('{$i18n.confirm_suppress}')" id="FormDelSelWS" >
    <input type="hidden" name="view_mode" value="{$view_mode}" />
    <input type="hidden" name="action" value="del_sel_ws" />
    <input type="hidden" name="idServer" value="{$server_id}" />
    <input type="checkbox" name="WSSelcet[]" value="{$webservice.Webservice__}" />
   </form>
{/foreach}
<table style="width:620px;margin:5px;">
  <tr>
   <td style="width:300px"></td>
   <td style="width:140px"></td>
   <td style="width:160px"></td>
    <td>
   <button type="submit" value="{$i18n.suppress}" form="FormDelSelWS">
    supprimer</button>
  </td>
 </tr>
  </table>
  • A form control cannot be associated with multiple forms
  • An ID must be unique in a document (so you can't generate multiple forms with the same ID in a loop).

You need to rethink your logic, probably to include a single form.

It isn't clear which of the values changes in your loop, but it looks like only the checkbox does. If so, you just need to move your loop inside the form.

<form action="index.php" method="post" onSubmit="return confirm('{$i18n.confirm_suppress}')">
    <input type="hidden" name="view_mode" value="{$view_mode}" />
    <input type="hidden" name="action" value="del_sel_ws" />
    <input type="hidden" name="idServer" value="{$server_id}" />

    {foreach from=$aWSForServer2 key=ws item=webservice}
        <input type="checkbox" name="WSSelcet[]" value="{$webservice.Webservice__}" />
    {/foreach}

    <button type="submit" value="{$i18n.suppress}">
        supprimer
    </button>

If any of the other values change in your loop, and need to be associated with a particular submit button then you can either:

  • Just keep them on the server and look them up with the checkbox values
  • Encode them in the form associated with the checkbox name (and then look them up on the server, but in $_POST.

Such:

<input type="hidden" name="idServer[{$webservice.Webservice__}]" value="{$server_id}" />