Jump to content

deleting items depending on checked boxes using array


paulonline2501

Recommended Posts

hi everyone, outline:i have a web page that displays a lists of pictures from a certain folder, with a check box next to each picture.i want to allow the user to check the box next to the picture they want to delete, and then when the press the 'delete' button all those pics get deleted. ive got the page displaying the pics and the checkboxes fine, im even putting the name of each picture into an array for later - thats no problem. however, when i go back to the on submit section of the script the array name is not being remembered by the page and i can t even get the array value to 'echo' on to the page (just as a test). therefore i cant make any progress with the deletion process. any help on how i can get the array to work outside of the 'for each' loop would be great. my code is as follows:

<?php// Setup defaults.$error  = 0; //input errors$up_error = 0; //title and description error counter - used to only show error message once. //set up pictures array$pictures = array(); //if all input is valid then...if (isset($_POST['delete'])){//clear error message$errmsg = ''; //set up post data from form for each pic/*foreach (glob('../../_pics/about/*.jpg') as $file){$file = basename($file);$_POST['$file'] = isset($_POST['$file']) ? $_POST['$file'] : '';}*/}if (isset($_POST['delete']) && ($error==0)){//reset picture_counter$picture_counter = 0; //this echo test does NOT work!!!!!!!!!!!!!!!!echo"$pictures[0]"; //sleep(6); //$_POST['$pictures[$picture_counter]'] = isset($_POST['$pictures[$picture_counter]']) ? $_POST['$pictures[$picture_counter]'] : ''; /*****************************************************  IGNORE THIS CODE  FOR NOWif(isset($_POST['$pictures[$picture_counter]'])){  echo"its checked";  $file_to_delete = "../../_pics/about/myfile.txt";   if(unlink($file_to_delete))  {echo"<p>File(s) deleted.</p>";}  else  {echo"<p>File(s) not deleted. <a href=\"../\">Back to Control Panel</a></p>";} }else{  echo"<p>....its not checked!!!!</p>";  echo($pictures[$picture_counter]);}******************************************************************************/}else //output error messages{if ($error>0) {echo "<p><strong>There were errors in your submission:</strong> $errmsg</p>\n";}//render form?><form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="save"><fieldset><table id="site-form"><?php//reset counter$counter = 0;  //render pictures in the relavent folderforeach (glob('../../_pics/about/*.jpg') as $file){//$cleaned_up_file_name = str_replace('../../_pics/about/','',$file);//strip down full path to file name$file = basename($file);//set value of array$pictures[$counter] = $file;//output table row for current pictureecho"<tr>  <td>File: $pictures[$counter]</td>  <td>	  <a href=\"../../_pics/about/$pictures[$counter]\">	  <img src=\"../../_pics/about/$pictures[$counter]\">	</a>   </td>  <td><input type=\"checkbox\" name=\"$pictures[$counter]\" value=\"$pictures[$counter]\" /></td></tr>";$counter++;}?><tr>  <td colspan="3"><input name="delete" id="save_button" type="submit" value="Delete Picture(s)"/> <a href="../">Cancel</a>.</td></tr></table></fieldset></form><?php//this test echo DOES work.echo"$pictures[0]";}?>

Link to comment
Share on other sites

In the foreach loop you have the variable in single quotes. That's not really the best way to do this though. You should make the checkboxes an array and make the values the filenames. When they submit it then the array will only contain the filenames of the pictures they want to delete. e.g.: <input type=\"checkbox\" name=\"delete_pictures[]\" value=\"$pictures[$counter]\" /> After that, if $_POST['delete_pictures'] exists then it will be an array containing the filenames to delete.

Link to comment
Share on other sites

If you give all of the checkboxes the same name, with brackets on the end like I showed, then that will become an array in $_POST and will contain the values from the boxes that were checked. That sounds easier than getting a list of all of the files and checking if each one was checked. You'll need to do validation to make sure the file they're trying to delete is in the correct directory though, that they have permission to delete it.

Link to comment
Share on other sites

...a few more questions. 1.do i need to declare the 'delete_pictures[]' 2.how do i call the post???like this?: if (isset($_POST['delete_pictures[]'])) {$_POST['delete_pictures[]'] = isset($_POST['delete_pictures[]']) ? $_POST['delete_pictures[]'] : ''; $file_to_delete = "../../_pics/about/$_POST['delete_pictures[]']"; unlink($file_to_delete)} 3.when you said i was using single quotes incorrectly what variable where you reffring too?

Link to comment
Share on other sites

....i tried the code below. i dont get any error but the test echo doesnt display anything.

<?php // Setup defaults.$error  = 0; //input errors$up_error = 0; //title and description error counter - used to only show error message once. //set up pictures array$pictures = array();//$delete_pictures = array(); //if all input is valid then...if (isset($_POST['delete'])) {//clear error message$errmsg = '';  if (isset($_POST['delete_pictures[]'])) {$_POST['delete_pictures[]'] = isset($_POST['delete_pictures[]']) ? $_POST['delete_pictures[]'] : '';  echo($_POST['delete_pictures[]']);//$file_to_delete = "../../_pics/about/$_POST['delete_pictures[]']";//unlink($file_to_delete)}  }   else //output error messages {if ($error>0) {echo "<p><strong>There were errors in your submission:</strong> $errmsg</p>\n";}//render form?><form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="save"><fieldset><table id="site-form"><?php//reset counter$counter = 0;  //render pictures in the relavent folderforeach (glob('../../_pics/about/*.jpg') as $file){//strip down full path to file name$file = basename($file);//set value of array$pictures[$counter] = $file;//output table row for current pictureecho"<tr><td>File: $pictures[$counter]</td><td>  <a href=\"../../_pics/about/$pictures[$counter]\">  <img src=\"../../_pics/about/$pictures[$counter]\"></a></td><td><input type=\"checkbox\" name=\"delete_pitures[]\" value=\"$pictures[$counter]\" /></td></tr>";$counter++;}?><tr><td colspan="3"><input name="delete" id="save_button" type="submit" value="Delete Picture(s)"/> <a href="../">Cancel</a>.</td></tr></table></fieldset></form><?php//testecho"$pictures[0]";}?>

Link to comment
Share on other sites

hi, ive simpilified my code so i can get my head around whats going on here because i dont understand:

  1. how the array is being populated
  2. how to get the results to show up.

the code is much simplier now (not using a variable any more).

 <?php//has form been submitted???if (isset($_POST['submission'])){if (isset($_POST['questionaire'])){foreach($_POST['delete_pictures[]'] as $key){echo $key;}}} //render formelse{?><form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="save"><fieldset><p>Do you like Northern Soul?<input type="checkbox" name="questionaire[]" value="northern" /></p><p>Do you like the blues?<input type="checkbox" name="questionaire[]" value="blues" /></p><p><input type="submit" name="submission" value="Submit"/> <a href="">Cancel</a>.</p></fieldset></form><?php}?>

P.S. probably not that relevant as its a PHP page but im testing the code here

Edited by as_bold_as_love
Link to comment
Share on other sites

snapback.pngkanchatchai, on 05 September 2012 - 10:36 AM, said:foreach($_POST['delete_pictures'] as $key)echo $key;good but curly brackets missed in for loops
if there is one statement it does need to put curly braces same as if-else statement. though it is always best to use curly braces for better readability.
Link to comment
Share on other sites

foreach($_POST['delete_pictures[]'] as $key)
delete_pictures will be stored as array. so if you want to get delete_pictures array which is list of selected checkbox. you will need to point it like
foreach($_POST['delete_pictures'] as $path){//$path is the value passed with checkboxes which is probably a path of directories//you will validate those paths and remove them}

  • Like 1
Link to comment
Share on other sites

I would add some additional checks, if no boxes were selected then the array won't be in $_POST.

if (isset($_POST['delete_pictures']) && is_array($_POST['delete_pictures'])){  foreach ($_POST['delete_pictures'] as $path)  {    ...  }}

3.when you said i was using single quotes incorrectly what variable where you reffring too?
Lines like this: $_POST['$file'] = isset($_POST['$file']) ? $_POST['$file'] : ''; You're using the string "$file", not the value of the $file variable.
  • Like 1
Link to comment
Share on other sites

thats great!!!!!!!!! thanks a lot guys. so for any one who wants to see the code that worked (slightly changed from the original to be simpler to understand) here it is:

<?php//has form been submitted???if (isset($_POST['submission'])){   //render title and open list   echo"<h2>Music you like</h2><ul>";    //check for checked boxes   if (isset($_POST['questionaire']) && is_array($_POST['questionaire']))   {      //for each item checked do the following      foreach ($_POST['questionaire'] as $answer)      {         //render name of item checked         echo"<li>$answer</li>";      }   }   //if nothing is checked render this:   else{echo"<li>You dont like any of the options.</li>";}   //close list   echo"</ul>";} //render formelse{?>   <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="save"><fieldset>      <p>Do you like Northern Soul?<input type="checkbox" name="questionaire[]" value="northern" /></p>      <p>Do you like the blues?<input type="checkbox" name="questionaire[]" value="blues" /></p>      <p><input type="submit" name="submission" value="Submit"/> <a href="">Cancel</a>.</p>   </fieldset></form><?php}?>

if you want to try the form go here:http://paulyouthed.c...using-array.php thanks again, paul

Edited by as_bold_as_love
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...