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;
$tempCheck = filter_input(INPUT_POST, "defaultCheck1", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$defaultCheck1 = (is_array($tempCheck)) ? implode(',', $tempCheck1) : null;
$sqlInsert = "INSERT INTO dbo.form (fName, lName, email, category ) VALUES (:fName, :lName, :email, :defaultCheck1)
$stmt = $conn->prepare($sqlInsert);
$stmt->bindParam(':fName', $fName);
$stmt->bindParam(':lName', $lName);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':defaultCheck1', $defaultCheck1);
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.
Aucun commentaire:
Enregistrer un commentaire