firehawk777 Posted February 13, 2011 Share Posted February 13, 2011 Hi there I am trying to send an array of items to delete from a form, receive them back to the page then enter them into another form and then send them back to the page. Basically its a delete then confirm delete thing. So I can send the array from the first form though I am not sure how to place them into the second (confirm delete) form. What I've got so far considering the array does get sent in the first place is First I receive the array: if (isset($_POST['delete'])) { if (isset($_POST['todelete'])){ $todelete[] = ($_POST['todelete']);////here is my problem I think echo <<<_END <div>Are you sure you wish to make these changes</div> <div><form action="" method="post"> <input type='hidden' name='todelete'value="$todelete"/> <input type='hidden' name='confirm'value="confirm"/> <input id='inputform' type='submit' size='50' value='Yes' /> </form> <form action="" method="post"> <input type='hidden' name='rollback'value='rollback' /> <input id='inputform' type='submit' size='50' value='No' /> </form></div> _END; } } Then I try and receive the array back with if (isset($_POST['confirm'])){ foreach ($_POST['todelete'] as $delete_id) { $query ="DELETE FROM gallery WHERE id = $delete_id"; $result=mysql_query($query) or die("Invalid Query : ".mysql_error()); echo "Removing Data"; } } What I receive is a warning: 'Warning: Invalid argument supplied for foreach() in C:\wamp\www\css\enterphotos.php on line 91" I am sure that there is a simple solution! Can anyone help me here? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 According to your form, $_POST['todelete'] isn't an array, it's a string. foreach() requires an array. Quote Link to comment Share on other sites More sharing options...
firehawk777 Posted February 13, 2011 Author Share Posted February 13, 2011 The 'todelete' is an array and I can use it to delete from the database. It is sent from another form where it collects the ids of each picture using <input id='inputform' type='checkbox' value='$id' name='todelete[]' /> All I want to do is add a second step where the user confirms the delete. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 If it was an array, foreach() would have no complaints about it. Place the following code at the top of your script, and post the output it generates after the form is submitted. echo '<pre>'; print_r($_POST); echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
firehawk777 Posted February 13, 2011 Author Share Posted February 13, 2011 I got this working thanks to a little help from a webgypsy. Here is how: if (isset($_POST['delete'])) { if (isset($_POST['todelete'])){ $todelete = ($_POST['todelete']); $value = ''; echo <<<_END <div>Are you sure you wish to make these changes</div> <div><form action="" method="post"> _END; foreach($todelete as $value){ echo '<input type="hidden" name="delThis[]" value="'.$value.'" /> '; } echo <<<_END <input type='hidden' name='confirm'value="confirm"/> <input id='inputform' type='submit' size='50' value='Yes' /> </form> <form action="" method="post"> <input type='hidden' name='rollback'value='rollback' /> <input id='inputform' type='submit' size='50' value='No' /> </form></div> _END; } } 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.