Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Input From Several Checkboxes 1

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
On a form that I did not create and cannot rewrite, I have managed to extract an array of a sample output but I am not sure how to get what I need from it. What the form does is to remove entries of image names from a table and it is doing so perfectly but I am trying to come up with the code to delete the physical images too. I don't need help in deleting the images - I can do that myself - but I do need help in making sense of the form output!

On one example with two entries, the form (which is an editable grid) is submitting something like this:

Code:
FormState -> 2;0;ID;334;332
ImageName_1 -> Image-020.jpg
ImageName_2 -> Image-021.jpg
CheckBox_Delete_2 -> 1
Button_Submit -> Delete Selected

In the case above I selected a check box and submitted the form to delete Image-021.jpg but it could have any number of images. The check box numbering is dynamic so CheckBox_Delete_2 is the one for Image-021.jpg so clearly CheckBox_Delete_2 -> 1 is saying that it was the box selected.

Here is an example with both check boxes checked and the form submitted:

Code:
FormState -> 2;0;ID;334;332
ImageName_1 -> Image-020.jpg
CheckBox_Delete_1 -> 1
ImageName_2 -> Image-021.jpg
CheckBox_Delete_2 -> 1
Button_Submit -> Delete Selected

. . . and here it is with NO check boxes checked and the form submitted:

Code:
FormState -> 2;0;ID;334;332
ImageName_1 -> Image-020.jpg
ImageName_2 -> Image-021.jpg
Button_Submit -> Delete Selected

The question is, how do I get the file name value from this?
 
Code:
$toDelete = array();
if(isset($_POST['Button_Submit']) && $_POST['Button_Submit'] =='Delete Selected'): 
 foreach($_POST as $key=>$val):
  if(preg_match('/CheckBox\_Delete\_(\d)+/', $key, $match)):
    $toDelete[] = $_POST['ImageName_'.$match[1]];
  endif;
 endforeach;
 print_r($toDelete);
endif;

by some bizarre coincidence I have on the slate today to write an image management class for the deletion (or suppression anyway) and addition of images to a site. synchronicity!
 
Amazing coincidence and it works perfectly! The code I wrote, which is just a few simple functions, is uploading and logging multiple images perfectly, including the creation of the folder (if it doesn't already exist) and thumbnail sub-folder. In theory I could delete the database entries and simply load the ones listed but I want to physically remove the files too for good housekeeping.

I've not yet added any image conversion or resizing to it as I wanted first to be able to manage the physical image storage and deletion so thank you for the help.

For the sake of completeness and in case it helps others, I added your code to the bit I was using to test the form output and here is what it gave when both check boxes were checked and the form submitted:

Code:
FormState -> 2;0;ID;343;346
ImageName_1 -> Image-020.jpg
CheckBox_Delete_1 -> 1
ImageName_2 -> Image-021.jpg
CheckBox_Delete_2 -> 1
Button_Submit -> Delete Selected
[COLOR=blue]0 -> Image-020.jpg
1 -> Image-021.jpg[/color]

and this is all output from a simple function, including your code slightly modified, for testing purposes:

Code:
function GetInput() {
	foreach ($_POST as $key => $value):
		$body .= $key . ' -> ' . $value . '<br>';
	endforeach;

	$toDelete = array();
	if(isset($_POST['Button_Submit']) && $_POST['Button_Submit'] =='Delete Selected'): 
		 foreach($_POST as $key=>$val):
		  if(preg_match('/CheckBox\_Delete\_(\d)+/', $key, $match)):
			$toDelete[] = $_POST['ImageName_'.$match[1]];
		  endif;
		 endforeach;

		foreach ($toDelete as $key => $value):
			$body .= $key . ' -> ' . $value . '<br>';
		endforeach;	 
	endif;
	
  echo $body;
}
 
as it happens I wrote an image cacheing and resizing/resampling script last week. let me know if you want it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top