Jump to content

Invalid argument supplied for foreach()


Deanznet

Recommended Posts

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.