Anton Steinbeck
Anton Steinbeck

Reputation: 1

Checkboxes don't pass on value to DB

I have created a contact form on my website using php and some of the values, more specifically the ones that have a checkbox-type input, are not send to my DB. I think there must be something wrong with the way I use the checkboxes or something. Upon submitting a form I get several notices like such:

Notice: Undefined index: ironing in /Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 79

Notice: Undefined variable: POST in /Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 94

The rest of the code seems to work fine and the data is loaded into my DB.

Here is some of my code in the demo-form.php file that I think is wrong:

<h4>i) Cleaningservice:</h4>
     <p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
     <p>Skills: <br>
       <input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
       <input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
       <input type="checkbox" name="laundry" value="yes"> Laundry<br>
       <input type="checkbox" name="ironing" value="yes"> Ironing<br>
       <input type="checkbox" name="window" value="yes"> Windows<br>
     <p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
     </p>

And here is the corresponding demo.php file that connects to the DB and stores the data there:

/* Cleaningservice data is gathered... */

$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];

/* ...and inserted into the table cleaningservice */

$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
        VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";

if (!mysql_query($sql)) {
 die('Error: ' . mysql_error());
}

What am I missing here ?? Thanks in advance

Upvotes: 0

Views: 38

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94672

The browser only passes the checkbox to the PHP script if the checkbox was actually checked.

So in PHP you have to check that the $_POST variable actually exists before using its contents, and provide the UNCHECKED i.e. default option if it does not exist.

/* Cleaningservice data is gathered... */

$clean_value1 = $_POST['cleanExpInYears']);
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = $_POST['other_clean'];

To avoid error messages like `array index does not exist you should check all POST/GET values exist like this anyway.

/* Cleaningservice data is gathered... */

$clean_value1 = isset($_POST['cleanExpInYears']) ? $_POST['cleanExpInYears'] : '';

$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';

$clean_value7 = isset($_POST['other_clean']) ? $_POST['other_clean'] : '';

Upvotes: 1

Halayem Anis
Halayem Anis

Reputation: 7795

You must switch between name and values

     <p>Skills: <br>
       <input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
       <input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
       <input type="checkbox" value="laundry" name="yes"> Laundry<br>
       <input type="checkbox" value="ironing" name="yes"> Ironing<br>
       <input type="checkbox" value="window" name="yes"> Windows<br>
     <p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
     </p>

and in PHP side, you will find your selected values in var_dump($_POST['yes'])

Upvotes: 0

Related Questions