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
chumba415
Forum Newbie
Posts: 4 Joined: Fri Nov 21, 2014 4:08 pm
Post
by chumba415 » Fri Nov 21, 2014 4:09 pm
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 />";
}
}
?>
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 21, 2014 4:15 pm
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
Post
by chumba415 » Fri Nov 21, 2014 4:27 pm
thank you! but it just out puts : array([0]=>)
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 21, 2014 4:50 pm
Can you post more of your code, then?
chumba415
Forum Newbie
Posts: 4 Joined: Fri Nov 21, 2014 4:08 pm
Post
by chumba415 » Fri Nov 21, 2014 5:51 pm
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']);
?>
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 21, 2014 7:02 pm
chumba415
Forum Newbie
Posts: 4 Joined: Fri Nov 21, 2014 4:08 pm
Post
by chumba415 » Fri Nov 21, 2014 7:28 pm
where do i put that? i echoed it and nothing comes out
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 21, 2014 7:29 pm
I just copy/pasted your code from above. That can't be right. You don't have a row called # in your DB.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 21, 2014 7:30 pm
I also just noticed you're only selecting Question. Won't you want the ID as well?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Fri Nov 21, 2014 7:32 pm
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 />";
}
}
?>