Reputation: 109
I have three columns in the database table and want to add 0 to 3 values (topics) from the checkboxes into the columns.
HTML:
<ul class="checkbox" id="topicarea">
<li><input type="checkbox" name="topic[]" value="AG">Agriculture<br /></li>
<li><input type="checkbox" name="topic[]" value="ART">Art<br /></li>
<li><input type="checkbox" name="topic[]" value="BIO">Biological Sciences<br /></li>
<li><input type="checkbox" name="topic[]" value="BUS">Business<br /></li>
<li><input type="checkbox" name="topic[]" value="COM">Communication<br /></li>
<li><input type="checkbox" name="topic[]" value="IT">Information Technology<br /></li>
<li><input type="checkbox" name="topic[]" value="EDU">Education<br /></li>
<li><input type="checkbox" name="topic[]" value="ENGR">Engineering<br /></li>
<li><input type="checkbox" name="topic[]" value="ENVS">Environmental Science<br /></li>
<li><input type="checkbox" name="topic[]" value="HE">Health<br /></li>
<li><input type="checkbox" name="topic[]" value="LANGLIT">Language and Literature<br /></li>
<li><input type="checkbox" name="topic[]" value="LAW">Law<br /></li>
<li><input type="checkbox" name="topic[]" value="PHR">Philosophy and Religion<br /></li>
<li><input type="checkbox" name="topic[]" value="PHYS">Physical Science<br /></li>
<li><input type="checkbox" name="topic[]" value="PSY">Psychology and Counseling<br /></li>
<li><input type="checkbox" name="topic[]" value="RECFIT">Recreation and Fitness<br /></li>
<li><input type="checkbox" name="topic[]" value="CON">Skilled Trade and Construction<br /></li>
<li><input type="checkbox" name="topic[]" value="LIBART">Social Sciences and Liberal Arts<br /></li>
<li><input type="checkbox" name="topic[]" value="SOCSRV">Social Services<br /></li>
<li><input type="checkbox" name="topic[]" value="TRANS">Transportation<br /></li>
<li><input type="checkbox" name="topic[]" value="OTHER">Other<br /></li>
</ul>
PHP: I have set three variables (topics 1, 2, and 3) But I do not know what to set them to. I figure they must loop through the checkboxes and get set when they find a value? How can I capture 0 to 3 checked values so I can insert them?
<?php
include('local-connect.php');
$fname = mysqli_real_escape_string($dbc, $_POST['firstname']);
$lname = mysqli_real_escape_string($dbc, $_POST['lastname']);
$email = $_POST['email'];
$password = $_POST['pword'];
$address1 = $_POST['lineone'];
$address2 = $_POST['linetwo'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$ageGroup = $_POST['agegroup'];
$topic1 =
$topic2 =
$topic3 =
$bio = $_POST['bio'];
$profilePic = $_POST['fileToUpload'];
$query = "INSERT INTO teachers(fname, lname, email, pword, address1, address2, city, state, zip, phone, ageGroup, topic1, topic2, topic3, bio, profilePic)" .
"VALUES('$fname','$lname','$email','$password','$address1','$address2','$city','$state','$zip','$phone','$ageGroup','$topic','$bio','$profilePic')";
$result = mysqli_query($dbc, $query) or die('Unable to Connect to Database or the Registration is incomplete!');
mysqli_close($dbc);
?>
Upvotes: 0
Views: 2040
Reputation: 449
You can't be sure that all 3 of them are set (even with client side javascript validation) so you need some validation for input.
$topics = $_POST['topic'];
if(is_array($topics)) {
// have an array of values
$topic1 = (array_key_exists(0, topics))? $topics[0] : 'NULL';
$topic2 = (array_key_exists(1, topics))? $topics[1] : 'NULL';
$topic3 = (array_key_exists(2, topics))? $topics[2] : 'NULL';
}
else {
// have single or none value
$topic1 = (isset($topics))? $topics : 'NULL';
$topic2 = 'NULL';
$topic3 = 'NULL';
}
Upvotes: 1
Reputation: 464
The data you seek will be in
$_POST['topic'];
You should be able to get the top three like this:
$topic1 = $_POST['topic'][0];
$topic2 = $_POST['topic'][1];
$topic3 = $_POST['topic'][2];
Upvotes: 1
Reputation: 438
You can use jquery to do this. Initialize a variable
var n=0;
When user checks a checkbox, increase value of n by 1
n++;
and simultanously check, if value of n
is equal to 3. Disable all the checkboxes.
Upvotes: 0