how to grab checkbox variables from html form? BEGINNER QUES

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chumba415
Forum Newbie
Posts: 4
Joined: Fri Nov 21, 2014 4:08 pm

how to grab checkbox variables from html form? BEGINNER QUES

Post by chumba415 »

i have a column of questions in my database so i out put them all with a check box next to each of them. i want the user to be able to check the questions they want and hit submit and on the next page be able to see the questions checked on the from. I just cant figure it out. I just cant figure out how to access them it should be simple im just not getting it.

Code: Select all

<?php 
$sql = " SELECT Question FROM multQuestions ";
$result = mysqli_query($dbCon, $sql);

if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo '<input type="checkbox" name="checkboxvar[]" value="'.$row["ID"].'">' . $row["Question"]." <br />";
}
}

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to grab checkbox variables from html form? BEGINNER

Post by Celauran »

You need to wrap those inputs in a form, then grab the $_POST['checkboxvar'] array after the form has been submitted.
chumba415
Forum Newbie
Posts: 4
Joined: Fri Nov 21, 2014 4:08 pm

Re: how to grab checkbox variables from html form? BEGINNER

Post by chumba415 »

thank you! but it just out puts : array([0]=>)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to grab checkbox variables from html form? BEGINNER

Post by Celauran »

Can you post more of your code, then?
chumba415
Forum Newbie
Posts: 4
Joined: Fri Nov 21, 2014 4:08 pm

Re: how to grab checkbox variables from html form? BEGINNER

Post by chumba415 »

thats really all my code i have the html containing the form

Code: Select all

<h1 style="font-family: kodakku; font-size:45px; font-weight:bold;">Choose Questions:</h1>

<form role="form" action="makeExam.php" method="post">
 
  <div class="form-group">
         <span style="font-family: kodakku; font-size:20px; font-weight:bold;">Exam Name: </span><input type="text" placeholder="Exam name..." class="form-control" name="examName">
   </div>
<span style="font-family: kodakku; font-size:52px; font-weight:bold;">Select Questions:</span><br>

<span style="font-family: kodakku; font-size:35px; font-weight:bold;"><u>Multiple Choice</u></span><br>
<div style="font-family: kodakku; font-size:25px; font-weight:bold;">
<?php 
$sql = " SELECT Question FROM multQuestions ";
$result = mysqli_query($dbCon, $sql);

if(mysqli_num_rows($result)>0)
{
  while($row = mysqli_fetch_assoc($result))
  {
    echo '<input type="checkbox" name="checkboxvar[]" value="'.$row["#"].'">' . $row["Question"]." <br />";
  }
}

?>
</div>
<span style="font-family: kodakku; font-size:35px; font-weight:bold;"><u>True/False</u></span><br>

<span style="font-family: kodakku; font-size:35px; font-weight:bold;"><u>Open Ended</u></span><br>

<span style="font-family: kodakku; font-size:35px; font-weight:bold;"><u>Create a Program</u></span><br>



  <button type="submit" name="submit" class="btn btn-primary">Submit</button>
 
</form>       

and php outputting :

<?php 

 print_r($_POST['checkboxvar']);

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to grab checkbox variables from html form? BEGINNER

Post by Celauran »

Code: Select all

$row["#"]
Wait, what?
chumba415
Forum Newbie
Posts: 4
Joined: Fri Nov 21, 2014 4:08 pm

Re: how to grab checkbox variables from html form? BEGINNER

Post by chumba415 »

where do i put that? i echoed it and nothing comes out
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to grab checkbox variables from html form? BEGINNER

Post by Celauran »

I just copy/pasted your code from above. That can't be right. You don't have a row called # in your DB.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to grab checkbox variables from html form? BEGINNER

Post by Celauran »

I also just noticed you're only selecting Question. Won't you want the ID as well?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to grab checkbox variables from html form? BEGINNER

Post by Celauran »

Something like this is probably closer to what you want.

Code: Select all

<?php 
$sql = " SELECT ID, Question FROM multQuestions ";
$result = mysqli_query($dbCon, $sql);

if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo '<input type="checkbox" name="checkboxvar[]" value="'.$row["ID"].'">' . $row["Question"]." <br />";
}
}

?>
Post Reply