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); } ?> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Deanznet Posted September 29, 2010 Author Share Posted September 29, 2010 How do i fix this? Thanks Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.