Page 1 of 1

how to grab checkbox variables from html form? BEGINNER QUES

Posted: Fri Nov 21, 2014 4:09 pm
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 />";
}
}

?>

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

Posted: Fri Nov 21, 2014 4:15 pm
by Celauran
You need to wrap those inputs in a form, then grab the $_POST['checkboxvar'] array after the form has been submitted.

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

Posted: Fri Nov 21, 2014 4:27 pm
by chumba415
thank you! but it just out puts : array([0]=>)

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

Posted: Fri Nov 21, 2014 4:50 pm
by Celauran
Can you post more of your code, then?

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

Posted: Fri Nov 21, 2014 5:51 pm
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']);

?>

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

Posted: Fri Nov 21, 2014 7:02 pm
by Celauran

Code: Select all

$row["#"]
Wait, what?

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

Posted: Fri Nov 21, 2014 7:28 pm
by chumba415
where do i put that? i echoed it and nothing comes out

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

Posted: Fri Nov 21, 2014 7:29 pm
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.

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

Posted: Fri Nov 21, 2014 7:30 pm
by Celauran
I also just noticed you're only selecting Question. Won't you want the ID as well?

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

Posted: Fri Nov 21, 2014 7:32 pm
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 />";
}
}

?>