Jump to content

Recommended Posts

Hello Everybody,

 

I am trying to output multiple values from check boxes on a page they are actually images.

 

So i am using this code to generate the images on the page with the check box

 

From what i have been reading i should be using interest[] to put them into an array...

echo '<a href="',$images_dir.$file,'" class="photo-link " title="'.$title.'" rel="lightbox[custom]"><span><input type="checkbox" name="interest[]" value="'.$title.'"></span><img src="',$thumbnail_image,'" /></a>';

 

So on my processing file i am using this to pull the values from the form

if( isset( $_POST['interest'] ) && is_array($_POST['interest']) ) {
   foreach( $_POST['interest'] as $value ) {
       //each $value is a selected value from the form
   }
}

 

Then i am using this to output the value into the email that it generates

$Body .= "Pictures Selected: ";
$Body .= $value;

 

This only send 1 value in the email, i need it to send all of the boxes that i am checking...

 

Thanks for any help anyone can give...

Please post more complete code, because there are no problems with the code you have posted. I will go ahead and assume that you have placed your checkboxes within a form and that you are sending the values to the correct script. Try to execute the below and tell us what you get:

 

die(print_r($_POST['interest']));

 

If the values are displayed correctly, then you must have an error in code that you have not posted.

Here is the complete code... I am not a PHP guy i am just trying to help out a friend with there site... My focus is all front end... any help with this would be great!

 

Thanks,

Travis

 

 

Yeah not a problem,

 

Here is contactengine.php

<?php

$EmailFrom = "travisjterry@gmail.com";
$EmailTo = "travisjterry@gmail.com";
$Subject = "ShutterBooth Collage Page Form";
$Name = Trim(stripslashes($_POST['Name'])); 
$Last = Trim(stripslashes($_POST['last'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Order = Trim(stripslashes($_POST['order'])); 
if( isset( $_POST['interest'] ) && is_array($_POST['interest']) ) {
foreach( $_POST['interest'] as $value ) {
       //each $value is a selected value from the form
}
}



// validation
$validationOK=true;
if (!$validationOK) {
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
 exit;
}

// prepare email body text
$Body = "";
$Body .= "First Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $Last;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "ShutterYou Order Date: ";
$Body .= $Order;
$Body .= "\n";

$Body .= "Pictures Selected: ";
$Body .=  $value;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
 print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

 

and here is the php file that is creating the images and form...

 

<form method="post" action="contactengine.php">
<p>
 <?php
/** settings **/
$images_dir = 'images/';
$thumbs_dir = 'thumbs/';
$thumbs_width = 150;
$images_per_row = 5;

/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
sort($image_files);
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);

}
}

$title = str_replace( ".jpg", "",$file);
echo '<a href="',$images_dir.$file,'" class="photo-link " title="'.$title.'" rel="lightbox[custom]"><span><input type="checkbox" name="interest[]" value="'.$title.'"></span><img src="',$thumbnail_image,'" /></a>';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>There are no images in this gallery.</p>';
}

?>
</p>
<label for="Name">First Name:</label>
<input type="text" name="Name" id="Name" />

<label for="City">Last Name:</label>
<input type="text" name="last" id="last" />
<br />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />

<label for="Email">ShutterYou Order Date:</label>
<input type="text" name="order" id="order" />

<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>

Oh, I thought the comment in your foreach loop was not part of the original code. Change it to something like this:

 


if( isset( $_POST['interest'] ) && is_array($_POST['interest']) ) {
$selected_values = '';

foreach( $_POST['interest'] as $value ) {
       $selected_values .= $value . '\n';
}
}

 

Now the selected values are added to a variable and then you can use it in your email's body like this:

 


$Body .= "Pictures Selected: ";
$Body .= $selected_values;

 

Before, it was only showing one value because $value was being overwritten in each iteration of your foreach loop. The foreach loop does not clear $value when it finishes executing, so the last value remained accessible later at which point you accessed it.

My HERO! Andy thanks so much... when i look at the code you wrote i see where i messing up....

 

I didnt understand how to store the values from the loop in a new variable...

 

Why is the necessary?

$selected_values = '';

 

everything works just trying to learn something here...

 

Thanks again,

Travis

You are very welcome, Travis. :)

 

It is in fact not necessary and you can remove it if you prefer. I just tend to do this because I am used to more static programming languages where the code is compiled. By initializing a variable like this in these languages, allocating space in the memory would occur once before the loop and not for each iteration of the loop. So it is just a performance optimization. If you needed the data to persist between the iterations, then you would have to define it outside of the loop anyways. I have thought if this is the case in PHP many times, but I never got around to researching it, so for now I just do it as a habit. :)

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.