foreach in wordpress form does not work

Asked

Viewed 18 times

0

I’m doing an email sending, using the native wordpress function wp_mail where send multiple concatenated fields in the variable $message, including checkboxes marked by the user. Despite believing to be doing it right, these checkboxes are the only ones that point errors and are not sent, appearing error:

Warning: Invalid argument supplied for foreach() in functions.php line 92

where this line has the following

foreach ( $_POST['salgados_selecionados'] as $key => $value ) {
   $message .= $value;
}

the checkbox are several, but an example would be

<input type="checkbox" name="salgados_selecionados" id="232" value="Bolinha com carne seca">
<input type="checkbox" name="salgados_selecionados" id="258" value="Bolinha de queijo">
<input type="checkbox" name="salgados_selecionados" id="256" value="Coxinha">

I used this same idea in another project and it worked.

  • The problem is in $_POST['salteos_selecionados'] Where is this coming from? a select ? a radio ? a text ? . do this and show what appears : var_dump($_POST['salt_selected'])

  • I edited the question, they are checkbox. When I var_dump it appears only the last checkbox selected Warning: Invalid argument supplied for foreach() in functions.php on line 86 string(7) "Coxinha"

  • I changed the name to selected salty[] and now it works. In the first version it was already like this, but I already worked so much in this code that I ended up tidying up without even knowing what was wrong the first time.

  • The correct thing would be to treat this &_POST you are receiving before sending to a foreach, even for the security of your application.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.