Deanznet Posted September 29, 2010 Share Posted September 29, 2010 I am having problem with my edit.php page, Keep on getting Invalid argument supplied for foreach() My edit.php is suppose to get the drop down list from the first page. and replace the string [Replace] with whats in the list box But it has to Use the first thing in the list box 2 times, and ect. <form method="post" action="edit.php" > <select name="drop1"> <?php echo $dropdownOptions; ?> </select> <input type="submit" value="drop1" /> </form> Edit.php <? $file = file_get_contents('myfile.txt'); foreach($_POST['drop1'] as $replace) { $file = preg_replace('#\[Replace\]#', $replace, $file, 2); } ?> Link to comment https://forums.phpfreaks.com/topic/214751-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
khizar Posted September 29, 2010 Share Posted September 29, 2010 $_POST is array but $_POST['foo'] is not array so are geting error Link to comment https://forums.phpfreaks.com/topic/214751-invalid-argument-supplied-for-foreach/#findComment-1117297 Share on other sites More sharing options...
Deanznet Posted September 29, 2010 Author Share Posted September 29, 2010 How do i fix this? Thanks Link to comment https://forums.phpfreaks.com/topic/214751-invalid-argument-supplied-for-foreach/#findComment-1117298 Share on other sites More sharing options...
kenrbnsn Posted September 29, 2010 Share Posted September 29, 2010 By default, a select menu will only have one value, so you don't need the foreach loop at all. If you want the user to be able to select more than one value, you need to use the 'multiple' attribute. You would also need to make the name an array: <select name="drop1[]" multiple="multiple"> <?php echo $dropdownOptions; ?> </select> Then your foreach loop would work. Ken Link to comment https://forums.phpfreaks.com/topic/214751-invalid-argument-supplied-for-foreach/#findComment-1117305 Share on other sites More sharing options...
the182guy Posted September 29, 2010 Share Posted September 29, 2010 As khizar said, $_POST['drop1'] is not an array, it's a string. foreach() needs an array to loop over. Link to comment https://forums.phpfreaks.com/topic/214751-invalid-argument-supplied-for-foreach/#findComment-1117306 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.