I have a form with an image, the image file name with a checkbox, and a field text (with the quantity) (this is dynamically created when the admin uploads the image on a wordpress folder) The user select the checkbox and write a quantity, I want that information sent to an email. I made that possible, but it only works when I click on all the checkboxes, if one of them is not selected, it sends the email with empty information.
this is the part of the code where I think the problem is:
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
$result ="";
$myQuantity = $_POST["quantity"];
$myFile = $_POST["fileName"];
//Combine both filename and quantity arrays
$values = array_combine($myFile, $myQuantity);
if(!empty($_POST["fileName"])){
foreach($values as $key => $value){
$result .= "$key - Quantity: $value <br/>";
}
}
... Any help will be appreciated.
update: this is the whole form just in case:
echo '<div class="images-form"><form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
foreach($images as $image) {
echo '<div class="thumb"><img src="';
echo $uploads['baseurl'].'/'.$a['folder_name'].'/'.$image;
echo '" alt="" /><br/>';
$fileName = basename($path.'/'.$image);
echo $fileName;
echo ' <input name="fileName[]" type ="checkbox" value="'.$fileName.'" /><br/>';
echo 'Quantity: <input name="quantity[]" type="text" value="" size="5" /></div>';
}
echo '<div class="send-form"><input type="submit" name="cf-submitted" value="Send email"/></div></form></div>';
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {
$result ="";
$myQuantity = $_POST["quantity"];
$myFile = $_POST["fileName"];
//Combine both arrays
$values = array_combine($myFile, $myQuantity);
if(!empty($_POST["fileName"])){
foreach($values as $key => $value){
$result .= "$key - Quantity: $value <br/>";
}
}
// to get wordpress user name and last name
global $current_user;
get_currentuserinfo();
$userName = $current_user->user_firstname;
$userLastName = $current_user->user_lastname ;
$to = get_option( 'admin_email' );// get the blog administrator's email address
$email = $current_user->user_email;
$headers = "From: $email" . "
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$subject = "Print request";
$message = "<html><body>";
$message .= $userName." ".$userLastName." wants to print the following file(s): <br/>".$result;
$message .= "</body></html>";
// If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<div>';
echo '<p>Thanks for contacting me, expect a response soon.</p>';
echo '</div>';
} else {
echo 'An unexpected error occurred';
}
}