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
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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.