-1

I am getting a notice when I submit a form with empty checkboxes and I do not know how to get rid of them.

Here is my PHP:

if(count($_POST) > 0) {
    $Q1 = $db->real_escape_string(trim($_POST['Q1']));
    $Q2_1 = $db->real_escape_string(trim($_POST['Q2_1']));
    $Q2_2 = $db->real_escape_string(trim($_POST['Q2_2']));
    $Q2_3 = $db->real_escape_string(trim($_POST['Q2_3']));
    $Q2_4 = $db->real_escape_string(trim($_POST['Q2_4']));
    $Q2_5 = $db->real_escape_string(trim($_POST['Q2_5']));
    $Q2_6 = $db->real_escape_string(trim($_POST['Q2_6']));
    $Q3 = $db->real_escape_string(trim($_POST['Q3']));

    if($insert = $db->query("
        INSERT INTO response (Q1, Q2_1, Q2_2, Q2_3, Q2_4, Q2_5, Q2_6, Q3)
        VALUES ('$Q1', '$Q2_1', '$Q2_2', '$Q2_3', '$Q2_4', '$Q2_5', '$Q2_6', '$Q3');
    ")) {
        echo $db->affected_rows;
    }
}

and here is my html:

<input type='checkbox' name="Q2_1" value="1">Not related to my issue<br/>
<input type='checkbox' name="Q2_2" value="1">Too complicated explanations<br/>
<input type='checkbox' name="Q2_3" value="1">Too much information<br/>
<input type='checkbox' name="Q2_4" value="1">Incorrect information<br/>
<input type='checkbox' name="Q2_5" value="1">Unclear information<br/>
<input type='checkbox' name="Q2_6" value="1">Incomplete information<br/>

EDIT The error message is: Notice: Undefined index: Q2_1 in C:\xampp\htdocs\srsurvey\connect.php on line 49

Notice: Undefined index: Q2_4 in C:\xampp\htdocs\srsurvey\connect.php on line 52

Notice: Undefined index: Q2_5 in C:\xampp\htdocs\srsurvey\connect.php on line 53

Notice: Undefined index: Q2_6 in C:\xampp\htdocs\srsurvey\connect.php on line 54

when the checkboxes are left empty

Alex
  • 25
  • 1
  • 3
  • 10

4 Answers4

1

Unchecked checkboxes aren't submitted with the $_POST data, so they don't exist in your array. You can check whether any given checkbox is checked by testing with isset():

$myVar = isset($_POST('myCheckbox')); // true if checked, false otherwise

Setting up for insertion into a database can be done like this:

$Q2_1 = (isset($_POST['Q2_1']))?1:0;   // No need to escape since the data is provided by your code.
1

Checkboxes won't send as part of the post array if they aren't checked. You can use isset in a simple ternary to set these values:

$Q2_1 = isset($_POST['Q2_1']) ? $db->real_escape_string(trim($_POST['Q2_1'])) : 0;

assuming you are ok with passing 0 if it is not checked. Change that to fit your needs.

Also notice Mike W's answer. If the post value is set at all it looks like you're just using 1 as the value. His example is more condensed and eliminates the need for unnecessary escaping processing. I'd say that's a better fit.

Kai Qing
  • 18,359
  • 5
  • 34
  • 56
0

Checkboxes don't get submitted by browsers if they are unchecked, so $_POST['Q2_1'] is not defined if that checkbox has been cleared.

To get rid of the notice either check if the index is defined, like

$Q1 = $db->real_escape_string(array_key_exists('Q2_1', $_POST) ? trim($_POST['Q2_1']) : false);

or by simply suppressing the notice by adding a @:

$Q1 = $db->real_escape_string(trim(@$_POST['Q2_1']));

HTH

ErnestV
  • 119
  • 1
  • 6
  • 1
    Suppressions are costly and in general a dirty practice. While this would kill the error message, it is a much better habit to not generate the errors in the first place – Kai Qing Oct 29 '13 at 21:07
  • "are costly" - I have never done a profiling on this issue, do you have any numbers? As for "suppressing the error" - it's only a Notice ;-) – ErnestV Oct 29 '13 at 21:10
  • 2
    @KaiQing you are right, I made a quick profiling test comparing supression, isset and arraykey_exists, each checking 100k random indices: supression 0.81sec, array_key_exists 0.21sec, isset 0.14sec. Reliable and repeatable measurements. Thanks for the input! – ErnestV Oct 29 '13 at 21:18
  • Yeah, I don't really keep resources to that handy but it is actually a common topic here. And while it may only be a notice, it is still just a better idea to write code that does not send warnings either. One of the guys in this post explains in great detail: http://stackoverflow.com/questions/136899/suppress-error-with-operator-in-php – Kai Qing Oct 29 '13 at 21:21
0

The problem is that checkboxes do not send POST data when they are not clicked, so you need to check if the index is defined. Since this task is always the same for every checkbox you can easily automate it.

$fields = array('Q2_1','Q2_6'); // all values
$query_fields = "";
$query_values = "";
foreach ($fields as $index) {
    $query_fields .= $index.",";
    if (isSet($_POST[$index]))
        $query_values .= "'1',";
    else
        $query_values .= "'0',";
}
$query = "INSERT INTO table " . rtrim($query_fields,",") . " VALUES (" . rtrim($query_values,",") . " )";

This way your code is more flexible, since you can just change the array $fields and it will produce a valid SQL query, so if you want to add or delete a field later, you just have to edit one line.

kero
  • 10,389
  • 5
  • 38
  • 47