[SOLVED] Need some help here with checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
butlerjw
Forum Newbie
Posts: 3
Joined: Sun Jul 17, 2011 7:06 pm

[SOLVED] Need some help here with checkboxes

Post by butlerjw »

Ok, I am developing a job application form. I have 8 checkboxes to represent Yes if checked, No if not checked.
In my table in mysql I have set these to tinyint(1) with a defined value of 0

Here is my form snipit
[text] <tr>
<td><u>Check All that apply:</u></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Owner Operator
<input type="checkbox" name="inputownerop" value="1" />
</td>
<td>Independent Driver
<input type="checkbox" name="inputidriver" value="1" />
</td>
<td>Class A CDL
<input type="checkbox" name="inputclass_a" value="1" />
</td>
<td>Class B CDL
<input type="checkbox" name="inputclass_b" value="1" />
</td>
</tr>
<tr>
<td>Hazmat
<input type="checkbox" name="inputhazmat" value="1" />
</td>
<td>Team Driver
<input type="checkbox" name="inputteam" value="1" />
</td>
<td>Solo Driver
<input type="checkbox" name="inputsolo" value="1" />
</label></td>
<td>Husband and Wife
<input type="checkbox" name="inputhuswife" value="1" />
</td>
</tr>
[/text]


And here is my code snipit:

Code: Select all

<?php
     	include 'include/connections.php';

        $lname = $_POST['inputlname'];
        $fname = $_POST['inputfname'];
        $email = $_POST['inputemail'];
        $phone = $_POST['inputphone'];
        $add1 = $_POST['inputadd1'];
        $add2 = $_POST['inputadd2'];
        $city = $_POST['inputcity'];
        $state = $_POST['inputstate'];
        $zip = $_POST['inputzip'];
        $ownerop = $_POST['inputownerop'];
        $idriver = $_POST['inputidriver'];
        $class_a = $_POST['inputclass_a'];
        $class_b = $_POST['inputclass_b'];
        $hazmat = $_POST['inputhazmat'];
        $huswife = $_POST['inputhuswife'];
        $solo = $_POST['inputsolo'];
        $team = $_POST['inputteam'];

        if(!$_POST['submit']) {
                echo "please fill out the form";

        } else {
                mysql_query("INSERT INTO applications (`id`, `fname`, `lname`, `phone`, `email`, `add1`, `add2`, `city`, `state`, `zip`, `ownerop`, `idriver`, `class_a`, `class_b`,$
`huswife`,
`solo`, `team`)
                        VALUES(NULL, '$fname', '$lname', '$phone', '$email', '$add1', '$add2', '$city', '$state', '$zip', '$ownerop', '$idriver', '$class_a', '$class_b', '$hazmat',$
'$solo', '$team')") or die(mysql_error());
         echo "User has been added!";


        }
?>
In the above code, everything is working and is being put in the database, but it fails when I hit ownerop, idriver and so on. The last bit of code is my check boxes.

If I actually check the box in my test, I do not get any errors, but it does not update the database to show a value of 1 for true. All the values remain set to 0.
If the boxes are unchecked, they all fail.

Thanks
Jim
Last edited by butlerjw on Mon Jul 18, 2011 11:43 pm, edited 1 time in total.
butlerjw
Forum Newbie
Posts: 3
Joined: Sun Jul 17, 2011 7:06 pm

Re: Need some help here with checkboxes

Post by butlerjw »

Not sure what I did, but I seem to have gotten it to work correctly.
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: Need some help here with checkboxes

Post by maneetpuri »

Hi,

When a checkbox is not checked the variables respective to it does not get created, and this what is creating the problem. No using isset function you can check if the variable is not set then set its value to zero and your problem will be resolved.

Cheers,

~Maneet
butlerjw
Forum Newbie
Posts: 3
Joined: Sun Jul 17, 2011 7:06 pm

Re: [SOLVED] Need some help here with checkboxes

Post by butlerjw »

Thanks!!
Post Reply