Log in

View Full Version : The value of a group of checkboxes



dog
04-02-2008, 12:14 AM
Hello everybody,

I've made a form that contains a lot of checkboxes. The idea is that users can check the boxes of the products they are interested in and send the form.

I've given the name 'artwork' to all of the checkboxes and changed the value depending on which artwork it is.

The problem I've come across is that when multiple checkboxes are checked only the value of the last one gets mentioned in the email. I'm looking for a way of sending the values of all the checkboxes that are checked, without having to create a new name for each checkbox. This is because new products are going to be continually added to the form and I don't want to have to keep adding to the php of the form sender.

The form looks a little like this:

<div id="purchaseForm">
<form action="sender2.php" name="purchase" id="purchase" method="post">
<div id="formPart1"><table cellspacing="0">
<tr id="titleBar">
<td id="itemDescription">Item Description</td>
<td id="itemType">Item Type</td>
<td id="price" colspan="2">Price</td>
</tr>

<tr>
<td>Product 1</td>
<td>Lazer Print</td>
<td>$10</td>
<td><input type="checkbox" name="artwork" value="07-late-01" /></td>
</tr>

<tr>
<td>Product 2</td>
<td>Lazer Print</td>
<td>$10</td>
<td><input type="checkbox" name="artwork" value="07-late-02" /></td>
</tr>

<tr>
<td>Product 3</td>
<td>Lazer Print</td>
<td>$10</td>
<td><input type="checkbox" name="artwork" value="07-late-03" /></td>
</tr>

<tr>
<td>Product 4</td>
<td>Lazer Print</td>
<td>$10</td>
<td><input type="checkbox" name="artwork" value="07-late-04" /></td>
</tr>

</table></div><!-- end of formPart1 -->

<!-- ...the rest of the form, etc. ... -->

</form>

And the form-sender looks like this:


<?

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = "result.php?sent=yes";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$artwork= $_POST['artwork'];
$mail = $_POST['mailing'];

$to = "monkeyzbox@dc5b.com";
$subject = "Purchase";
$body = "Name: ".$name."\r\n\r\n";
$body .= "Email: ".$email."\r\n\r\n";
$body .= "Delivery Address: ".$address."\r\n\r\n";
$body .= "Artwork selected: ".$artwork."\r\n\r\n";
$from = "dog@dc5b.com";

mail($to, $subject, $body, $from);
header("Location: http://$host$uri/$extra");
exit;

?>

If anyone has any ideas please let me know.

Thanks,

Dog

thetestingsite
04-02-2008, 12:17 AM
Try changing the name of the checkboxes to artwork[] (that way it makes it an array), then change your php to something like the following:



<?php

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = "result.php?sent=yes";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$artwork = $_POST['artwork'];
$mail = $_POST['mailing'];

$to = "monkeyzbox@dc5b.com";
$subject = "Purchase";
$body = "Name: ".$name."\r\n\r\n";
$body .= "Email: ".$email."\r\n\r\n";
$body .= "Delivery Address: ".$address."\r\n\r\n";
$body .= "Artwork selected: \r\n".

foreach ($artwork as $selected) {
$body .= " > ".$selected."\r\n";
}

$from = "dog@dc5b.com";

mail($to, $subject, $body, $from);
header("Location: http://$host$uri/$extra");
exit;

?>


Hope this helps.

dog
04-02-2008, 11:43 PM
Hey man,

Thanks a lot. It looks like that's nearly working.

What happened is that in the email I received:


Artwork selected:
> Array
> Array
> Array

Please excuse me, I don't have any more ideas but hope that someone else does.

Dog

thetestingsite
04-02-2008, 11:55 PM
Sorry, the php code should be this:



<?php

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = "result.php?sent=yes";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$artwork = $_POST['artwork'];
$mail = $_POST['mailing'];

$to = "monkeyzbox@dc5b.com";
$subject = "Purchase";
$body = "Name: ".$name."\r\n\r\n";
$body .= "Email: ".$email."\r\n\r\n";
$body .= "Delivery Address: ".$address."\r\n\r\n";
$body .= "Artwork selected: \r\n".

foreach ($artwork as $selected) {
$body .= " > ".$selected."\r\n";
}

$from = "dog@dc5b.com";

mail($to, $subject, $body, $from);
header("Location: http://$host$uri/$extra");
exit;

?>


Hope this helps.

dog
04-03-2008, 12:07 AM
Thanks once again!

That works perfectly.

Thanks!

dog
05-12-2008, 11:11 PM
Hello everybody. Hello thetestingsite,

Sorry to bring an old thread back up but a new problem has come to light.

In order for the PHP form sender to work I ended up changing the name of my checkboxes.


Try changing the name of the checkboxes to artwork[] (that way it makes it an array)

Now I've got a problem with the javascript form validation.

I'd like to use something like this:


var checkboxes =""
for(var i=0; i < document.purchase.artwork.length; i++){
if(document.purchase.artwork[i].checked)
checkboxes +=document.purchase.artowork[i].value + "\n"
}

if(checkboxes=="") {
alert("Please check the box beside the artwork you would like to purchase.");
return false;
}

But the square brackets in the name complicate things.

Any advise would be much appriciated.

Thanks,

Dog

PS Re. forum etiquette. If I should have posted this as a new thread in the javascript forum please let me know.