-2

I'm in the middle of developing a html form whose data gets saved into a MS-SQL server database. I'm able to get all other fields get saved to their respective columns in the database, however the checkbox field leaves the value null. I'm not very familiar with that concept.

Here's the code:

form.php

<form class="cmxform" action ='functions/Form.php'  method="post">
<div class="form-row">

                   <h3> Contact Information</h3>
                    <div class="form-group col-md-4">
                        <label for="fName">First Name </label>
                        <input type="text" class="form-control" id="fName" name="fName">
                    </div>
                    <div class="form-group col-md-4">
                        <label for="lName">Last Name</label>
                        <input type="text" class="form-control" id="lName" name="lName">
                    </div>
                    <div class="form-group col-md-4">
                        <label for="email">Email </label>
                        <input type="email" class="form-control" id="email" name="email">
                    </div>
                </div>
<div class="form-group col-md-12">
<h3>Category: Please choose one of the following: </h3>
     <div class="form-check">
   <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
       <label class="form-check-label" for="defaultCheck1">
           Vegetarian
     </div>
<div class="form-check">
         <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
         <label class="form-check-label" for="defaultCheck1">
        Ham
         </label> 
     </div>
<div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
    <label class="form-check-label" for="defaultCheck1">
         Turkey
         </label>
</div>
<div class="form-check">
    <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
         <label class="form-check-label" for="defaultCheck1">
        Other
    </label>
</div>

functions/form.php:

<?php
require_once 'connection.php';
$fName = filter_input(INPUT_POST, "fName") ? filter_input(INPUT_POST, 'fName') : null;
$lName = filter_input(INPUT_POST, "lName") ? filter_input(INPUT_POST, 'lName') : null;
$email = filter_input(INPUT_POST, "email") ? filter_input(INPUT_POST, 'email') : null;
$tempCheck1 = filter_input(INPUT_POST, "defaultCheck1") ? filter_input(INPUT_POST, 'defaultCheck1') : null;
$tempCheck2 = filter_input(INPUT_POST, "defaultCheck2") ? filter_input(INPUT_POST, 'defaultCheck2') : null;
$tempCheck3 = filter_input(INPUT_POST, "defaultCheck3") ? filter_input(INPUT_POST, 'defaultCheck3') : null;
$tempCheck4 = filter_input(INPUT_POST, "defaultCheck4") ? filter_input(INPUT_POST, 'defaultCheck4') : null;
$sqlInsert = "INSERT INTO dbo.form (fName, lName, email, category ) VALUES (:fName, :lName, :email, :defaultCheck1, :defaultCheck2, :defaultCheck3, :defaultCheck4)
$stmt = $conn->prepare($sqlInsert);
$stmt->bindParam(':fName', $fName);
$stmt->bindParam(':lName', $lName);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':defaultCheck1', $tempCheck1);
$stmt->bindParam(':defaultCheck2', $tempCheck2);
$stmt->bindParam(':defaultCheck3', $tempCheck3);
$stmt->bindParam(':defaultCheck4', $tempCheck4);
if ($stmt->execute()) {
        $conn->commit();
        return true;
    } else {
        $conn->rollback();
        return false;
    }
?>

That's the only box in the form I'm stuck sending the data to the database. Rest everything is working fine.

AAM
  • 321
  • 5
  • 24

2 Answers2

1
<div class="form-check">
         <input class="form-check-input" type="checkbox" value="" **id="defaultCheck1"**>
         <label class="form-check-label" for="defaultCheck1">
        Ham
         </label> 
     </div>
<div class="form-check">
    <input class="form-check-input" type="checkbox" value="" **id="defaultCheck1"**>
    <label class="form-check-label" for="defaultCheck1">
         Turkey
         </label>
</div> 

First off you're duplicating IDs. In any scenario this is not appropriate as element ids must be unique.

Second, forms only submit values for field elements that have names. They also need values, which you have left all as "". "Ham" and "Turkey" in this slice of code are just text strings and don't submit with the form. See HTML Forms - Are name and id required?

Edit 1:

PHP- beyond that, your php will need a couple touches:

$tempCheck = filter_input(INPUT_POST, "defaultCheck1", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$defaultCheck1 = (is_array($tempCheck)) ? implode(',', $tempCheck1) : null;

These values aren't coming in an array. Say you have three checkboxes, named "a", "b" and "c". If the checkbox for "a" is selected, I will get its value in $_POST['a']. If "b" is not selected, $_POST['b'] will not exist.

So for every checkbox value you want to deal with, you need to a) check if it exists in $_POST and if so, b) use that value (i.e., add it to the comma-separated string).

parttimeturtle
  • 1,125
  • 7
  • 22
  • Right. Let me make the changes that you suggested. – AAM May 19 '20 at 19:20
  • Okay. How do I exactly do that? – AAM May 19 '20 at 19:41
  • You've already done it mate, you just need to do it for every checkbox you might expect: $fName = filter_input(INPUT_POST, "fName") ? filter_input(INPUT_POST, 'fName') : null; – parttimeturtle May 19 '20 at 19:43
  • Okay let me test it. Update you soon – AAM May 19 '20 at 19:43
  • But the there is only one column in the database for the category. How do I get to save multiple checked checkboxes – AAM May 19 '20 at 19:44
  • @AAM you can still do it the way you did before by imploding the values into a comma-separated string. Make an empty array, and for each checkbox value you find, push it into that array. Then implode said array just like you did before. – parttimeturtle May 19 '20 at 19:46
  • Im just not able to implode multiple names on a single line there. It's giving me syntax error – AAM May 19 '20 at 20:11
0

It is because your checkbox fields don't have names, but I suppose that you want to allow choose only one category, in that case you have to use radio fields in you HTML markup:

<div class="form-check">
    <input class="form-check-input" type="radio" name="category" value="vegetarian" id="defaultCheck1">
    <label class="form-check-label" for="defaultCheck1">
        Vegetarian
</div>
<div class="form-check">
    <input class="form-check-input" type="radio" name="category" value="ham" id="defaultCheck2">
    <label class="form-check-label" for="defaultCheck1">
        Ham
    </label>
</div>
<div class="form-check">
    <input class="form-check-input" type="radio" name="category" value="turkey" id="defaultCheck3">
    <label class="form-check-label" for="defaultCheck1">
        Turkey
    </label>
</div>
<div class="form-check">
    <input class="form-check-input" type="radio" name="category" value="other" id="defaultCheck4">
    <label class="form-check-label" for="defaultCheck1">
        Other
    </label>
</div>

Also I have noticed that you are using same id attributes in multiple places, id is used to specify uniqueness of the element, it will not break functionality, but keep it in mind.

UPDATE

If you want to have multiple categories which mean the same thing, you can name them as categories[0], categories[1], categories[2] ...

<div class="form-check">
    <input class="form-check-input" type="checkbox" name="categories[0]" value="vegetarian" id="defaultCheck1">
    <label class="form-check-label" for="defaultCheck1">
        Vegetarian
</div>
<div class="form-check">
    <input class="form-check-input" type="checkbox" value="ham" name="categories[1]" id="defaultCheck2">
    <label class="form-check-label" for="defaultCheck2">
        Ham
    </label>
</div>
<div class="form-check">
    <input class="form-check-input" type="checkbox" value="turkey" name="categories[2]" id="defaultCheck3">
    <label class="form-check-label" for="defaultCheck3">
        Turkey
    </label>
</div>
<div class="form-check">
    <input class="form-check-input" type="checkbox" value="other" name="categories[3]" id="defaultCheck4">
    <label class="form-check-label" for="defaultCheck4">
        Other
    </label>
</div>

And then in PHP update this fields:

$tempCheck = filter_input(INPUT_POST, "categories", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$stmt->bindParam(':defaultCheck1', implode(',', $tempCheck));
Evaldas
  • 26
  • 3
  • Okay I added the name attribute to it, but it's a checkbox not a radio. It should also be able to save multiple entries to the database. How do I send the value to the sql server further. – AAM May 19 '20 at 19:23
  • *" it will not break functionality,"* Wrong. It will break the functionality of CSS and JS – Jay Blanchard May 19 '20 at 19:41
  • I can't agree with that, you can still use it like selector in CSS, but for JS it will select only the first element. But yes, it is a wrong way of doing things. – Evaldas May 19 '20 at 19:52
  • Can you please post the entire update to be made to the PHP? The implode part? – AAM May 19 '20 at 20:43