Forum Moderators: coopster
<form name=form onSubmit="return checkFields();">
<table width="500" border="3" class = "table1">
<tr>
<th> <h3> Trips </h3> </th>
</tr>
<tr>
<td> Ski Trip $499.95 </td>
<td>
<alt="Ski" />
<input type="checkbox" name="CHKBOX_1" value="1" />
<br />
</td>
</tr>
<tr>
<td>Fishing Trip $259.99</td>
<td>
<alt="Fishing" />
<input type="checkbox" name="CHKBOX_2" value="2" />
<br />
</td>
</tr>
<tr>
<td>Hunting Trip $359.99</td>
<td>
<alt="Hunting" />
<input type="checkbox" name="CHKBOX_3" value="3" />
<br />
</td>
</tr>
<tr>
<td>Camping Trip $99.99</td>
<td>
<alt="Camping" />
<input type="checkbox" name="CHKBOX_4" value="4" />
<br />
</td>
</tr>
</table>
<input type=submit name="submit" value="Submit Form!" >
</form>
2) it would be easier and more efficient if you change the names of your checkboxes into the same name with brackets so saveTrips.php will be able to retrieve the checkboxes as an array. so instead of having CHKBOX_1, CHKBOX_2, and so on, change their names to CHKBOX[] or whatever you want. I'd recommend "trip[]"(or something easy/relevant) for this application
3) on the saveTrips.php, you can retrieve the check boxes by reading the $_POST variable. so something like this should work.
<?
$tripsChecked = $_POST['trip']; //assuming you named it as CHKBOX
?>
the variable $tripsChecked will now contain all the information of the checked boxes in your previous form. it will be in an array so you will need to use a for or while loop to access the data. i'm just printing out the data below.
<?
for($i = 0; $i < sizeof($tripsChecked); $i++){
echo "\$tripsChecked[$i]: ". $tripsChecked[$i]."<br>\n";
}
?>
4) as for the saving part, you'd want to save this information in a database? i havent wokred with any other db besides mysql, so if you have something else, perhaps someone else can help you. but likely, you are using mysql and if that is the case you will need to get aquainted with some key mysql functions.
a) connect to the db: [us.php.net...]
b) select the db:
[us.php.net...]
c) making the query
[us.php.net...]
GL and post here if you need more help
var totalcost=0, form=document.forms['form'];
totalcost+=form.CHKBOX_1.checked? 499.95 : 0;
totalcost+=form.CHKBOX_2.checked? 259.99 : 0;
totalcost+=form.CHKBOX_3.checked? 359.99 : 0;
totalcost+=form.CHKBOX_4.checked? 99.99 : 0;
alert(totalcost? "Total cost is " + totalcost + " dollars" : 'You didn\'t select a trip!');
return totalCost;
How would I go about getting the total if I named them in the array trip[]. I tried trip[1].checked , but it didn't do anything. Is there a way that I can go by their values instead of the name trip[] or is there an easier way to do it?
Then you can do something like:
if(!empty( $_POST['somename'] ) && is_array( $_POST['somename'] ) ) {
$total = 0;
foreach( $_POST['somename'] as $value ) {
echo $value; // Value of the individual check box checked.
$total += $value;
}
echo $total;
}
(This is just pseudo code and proper data validation should be done.)
You should always check that your checkbox array is actually an array. If your user does not select any checkboxes in your checkbox group, the variable will be sent as NULL and not an array and PHP will complain if you then try to perform any array operations.
Since you are applying a different name to each checkbox element, your simply can do an if statement and echo either an empty string or "checked" in your form elements on your final page.
echo ( empty( $_POST['elementnamehere'] ) )? '' : "checked";
To get the total...just add up all your POST variables...or if your method is get, then all your GET variables.