Forum Moderators: coopster

Message Too Old, No Replies

Passing checkbox To Table in PHP

         

Demoman2007

3:48 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



I have this html form with this table. I am trying to get it so when you hit the submit button it takes you to a php page that stores all of the trips that were checked in a table. I am pretty new to php and do not have the slighest idea how to do this.

<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>

d40sithui

4:41 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Hi Demonman2007,
1) first you need to point your form to an "action" php script to handle all the form data processing. that means you would have to change your form declaration to something like this (i'm assuming "saveTrips.php" is your action script):
<form action="saveTrips.php" method="POST" onSubmit="return Checkfields();">

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

Demoman2007

5:13 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



The reason I had them named CHKBOX_1-4 was because I used them to get the total after the form was submitted like this.

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?

d40sithui

10:34 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



you certainly dont have to change it to trip[] or any other name. the only benefit of my suggestion was so that it would be easier to retrieved checked values via php and easier to manage. of course you can leave it as so, but you'll have to retrieve each checkbox by itself, rather than one line to retrieve them all.

phnord

6:06 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



Adding [] to your check boxes signifies that the HTTP request variable for that element name should be treated as an array. For instance, if you want to retrieve all check boxes the user checked. The name of each input element would be <input type="checkbox" name="somename[]" value="whatever" />

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.