Drewser33 Posted July 1, 2013 Share Posted July 1, 2013 foreach($_POST['customerID'] as $value) Warning: Invalid argument supplied for foreach() I have used _GET as well, to confirm that there are values for this variable. What am I missing? Thanks Link to comment https://forums.phpfreaks.com/topic/279759-for-each-error/ Share on other sites More sharing options...
Psycho Posted July 1, 2013 Share Posted July 1, 2013 What do you get with this var_dump($_POST['customerID']); Link to comment https://forums.phpfreaks.com/topic/279759-for-each-error/#findComment-1438811 Share on other sites More sharing options...
Drewser33 Posted July 1, 2013 Author Share Posted July 1, 2013 I get : string(2) "27" Link to comment https://forums.phpfreaks.com/topic/279759-for-each-error/#findComment-1438812 Share on other sites More sharing options...
Psycho Posted July 1, 2013 Share Posted July 1, 2013 OK, you can't do a foreach() loop on a string, which is why you are getting the error. Are you allowing the user to make multiple customerID selections/entries? Did you name the fields appropriately so it will be sent as an array? E.g. <input type="checkbox" name="customerID[]" value="33" /> Note the [] at the end of the input name Link to comment https://forums.phpfreaks.com/topic/279759-for-each-error/#findComment-1438814 Share on other sites More sharing options...
Drewser33 Posted July 1, 2013 Author Share Posted July 1, 2013 echo '<option value='.$row['ID'].'>'.$row['LastName'].', '. $row['FirstName'].'</option>'; This is part of a select box, so no, I did not have the []. I will use a checkbox instead, that will likely be a better type of input instead, and I will use the [] as you mention. Thank you. I will build the checkbox quickly and check it out. Link to comment https://forums.phpfreaks.com/topic/279759-for-each-error/#findComment-1438815 Share on other sites More sharing options...
Psycho Posted July 1, 2013 Share Posted July 1, 2013 echo '<option value='.$row['ID'].'>'.$row['LastName'].', '. $row['FirstName'].'</option>'; This is part of a select box, so no, I did not have the []. I will use a checkbox instead, that will likely be a better type of input instead, and I will use the [] as you mention. Thank you. I will build the checkbox quickly and check it out. The same also holds for a select field that uses the multiselect parameter. You need to name the field with a [] at the end for the multiple values to be passed as an array. <select name="customerID[]"> Also, I prefer to use double quotes when defining strings with variables echo "<option value='{$row['ID']}'>{$row['LastName']}, {$row['FirstName']}</option>"; Whether you use a select list or a checkbox is a personal decision. I prefer using checkboxes since many users don't know how to make multiple selections in a select field. Link to comment https://forums.phpfreaks.com/topic/279759-for-each-error/#findComment-1438816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.