I echo the value of the radio-buttons from my database and in this form ( a while loop ) is the fastest way to get them. When I do this it sees all the radio-buttons as in the same "group". How do to make it so that it has multiple "groups" so it is possible to answer multiple questions?
I tried adding a div to it but that didn't work.
<form action="Antwoord.php" method="POST">
<?php
$sql = "SELECT * FROM questionlist_choice";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
echo "<br><p>$vraag</p>";
echo "<input type='radio' name='gender' value='$vraagA'> $vraagA<br>";
echo "<input type='radio' name='gender' value='$vraagB'> $vraagB<br>";
echo "<input type='radio' name='gender' value='$vraagC'> $vraagC<br>";
echo "<input type='radio' name='gender' value='$vraagD'> $vraagD<br>";
echo "<input type='radio' name='gender' value='$vraagE'> $vraagE<br>";
echo "<input type='radio' name='gender' value='$vraagF'> $vraagF<br>";
echo "</div>";
}
}
?>
<input type="submit">
</form>
Sorry I don't understand your question that much, but if you want to GROUP them you need to add index on name tag like this:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
echo "<br><p>$vraag</p>";
echo "<input type='radio' name='gender[$i]' value='$vraagA'> $vraagA<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagB'> $vraagB<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagC'> $vraagC<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagD'> $vraagD<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagE'> $vraagE<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagF'> $vraagF<br>";
echo "</div>";
$i++;
}
I use $i as the index, I think it would be better if you use for loop cause it already has $key value, I think using this in while is a bad habit.
Related
this is my first file code, where the php code is inside an html form and sending data by post method:
<form name="quiz" action=quizaction.php method="POST">
<?php
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
i want to use the $index variable in other file named quizaction.php file ,how do i ??
Use SESSION
<form name="quiz" action=quizaction.php method="POST">
<?php
session_start();//session starts here
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
// set session value and use $_SESSION['index'] at quizaction.php to find session value
if($index> 0){
$_SESSION['index'] = $index;
}
?>
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
You can take hidden text-box in your form. then you can get that value in quizaction.php file.
while($row = $result->fetch_assoc()) {
echo "";
echo "Q:" . $row["question_name"]. "";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
echo "<input type='hidden' name='indexid' value='".$index."'/>
$index++;
echo $index;
}
this code outputs different question from db until while loop expires with MCQ options of type radio:
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question1' value='answer1.1'/ >
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question1' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question1' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question1' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
}
} else {
echo "0 results";
}
but here every options of every question have the same name attribute of question1 only , but i want that for the next question name attribute should change to question2 and so on. Please help
If you have an identifier in your table question, you could use it.
But you could just use a variable to increment question number :
$index = 1 ;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/ >
<code>".$row["answer1"]."</code>". "<br>";
//...
$index++;
}
This code gets some data from the database and displays it with a group of radio buttons. How do I get the value for every group of radio buttons??
<form name="functional" method="post" action="funcsub.php">
<?php
$n=0;
$con=mysqli_connect('localhost','Sanjana','sanjana');
mysqli_select_db($con,'mydatabase');
$sql = mysqli_query($con,"select * from functional") or die("Failed");
while($result = mysqli_fetch_array($sql)){
?>
<br/>
<?php
echo $result["Skill"];
echo "</br>";
echo"<input type='radio' name='tech[.$n.]' value='0'>0";
echo"<input type='radio' name='tech[.$n.]' value='1'>1";
echo"<input type='radio' name='tech[.$n.]' value='2'>2";
echo"<input type='radio' name='tech[.$n.]' value='3'>3";
echo"<input type='radio' name='tech[.$n.]' value='4'>4";
echo"<input type='radio' name='tech[.$n.]' value='5'>5";
$n=$n+1;
}
?>
<br/>
<input type="submit" name="submit" value="Submit">
</form>
Those dots are not needed in these. Perhaps you meant?:
echo "<input type='radio' name='tech[".$n."]' value='0'>0";
echo "<input type='radio' name='tech[".$n."]' value='1'>1";
echo "<input type='radio' name='tech[".$n."]' value='2'>2";
echo "<input type='radio' name='tech[".$n."]' value='3'>3";
echo "<input type='radio' name='tech[".$n."]' value='4'>4";
echo "<input type='radio' name='tech[".$n."]' value='5'>5";
Assuming that's what you wanted, you can get values like this (I prefer writing above the printing part (or your form, in this case)):
if (!empty($_POST)) {
$tech = $_POST['tech'];
echo 'Value of the second row: '.$tech[1];
}
Here is the scenario. in my database, I have like 4 questions. each questions have individual 5 radio buttons. I tried to retrieve the information from database. it shows my list of questions and radio buttons individually, but the BIG problem here is they are in the same group. example. in question 1, I picked 1st radio button, then in question 2, I picked the 2nd radio button. in question 1, the radio button I choose disappeared. so basically. the whole loop have single radio button name. how do I fix this dynamically? like auto change of radio-button-name for each question?
<form method='post' action='test.php'>
<?php
include 'db_connect.php';
$query = "SELECT * FROM test";
$result = $conn->query($query);
$num_results = $result->num_rows;
#if ($num_results > 0) {
#}
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
echo "<input type='radio' name='question_button' value='1'>";
echo "<input type='radio' name='question_button' value='2'>";
echo "<input type='radio' name='question_button' value='3'>";
echo "<input type='radio' name='question_button' value='4'>";
echo "<input type='radio' name='question_button' value='5'>";
echo "<br>";
}
$testbutton = isset($_POST['question_button']) ? $_POST['question_button'] : "";
if (isset($_POST['submit'])) {
echo $testbutton;
}
?>
<html>
<input type='submit' name='submit' value='submit'>
</form>
</html>
P.S. Edit. my original intention is to add or get the sum of the radio buttons. what syntax should i use?
to change button name dynamically
change like following
$question=0;
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
$question++;
echo "<input type='radio' name=".$question." value='1'>";
echo "<input type='radio' name=".$question." value='2'>";
echo "<input type='radio' name=".$question." value='3'>";
echo "<input type='radio' name=".$question." value='4'>";
echo "<input type='radio' name=".$question." value='5'>";
echo "<br>";
}
$question is just for example,
you can change redio button name according to your need.
I can't see what your database query is producing, but at a guess, I'd say you should be assigning a radio button id to your question in the database field. Then, when you get the results back, you can assign it with something like:
while ($row = $result->fetch_assoc()) {
extract($row);
echo "<input type='radio' name='".$row['question_name']."' value='".$row[question_id]."'>";
echo "<br>";
}
This should make every radio box unique so long as the data in the database is unique. If this doesn't help, may I see the results of the database query and I can edit my answer.
Simply give the radio button a group name differently as bellow example
// 1. Get the handler for counter.
$counter = 1;
// 2. Iterating through the results.
while ($row = $result->fetch_assoc()) {
$name = "question_" . $counter . "_button";
extract($row);
echo $row['test1'];
echo "<input type='radio' name=$name value='1'>";
echo "<input type='radio' name=$name value='2'>";
echo "<input type='radio' name=$name value='3'>";
echo "<input type='radio' name=$name value='4'>";
echo "<input type='radio' name=$name value='5'>";
// Increment the counter by 1 for each question.
$counter += 1;
}
I have problems displaying my checkboxes. Can anyone please help? I search the solution online and I tried the codes but it doesn't work.
Here is my codes
<?php
include "mysqli.connect.php";
// Make a MySQL Connection
$retrieveflavor = "SELECT * FROM flavor";
$result = $mysqli->query($retrieveflavor);
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
//echo "<input type='checkbox' name='candyFlavors[]' value=".$row['flavorname']."/>";
//echo "<input type=\"checkbox\" name=\"candyFlavors[]\" value=\"$row[flavorname]\">";
//echo "<input name=\"candyFlavors[]\" type='checkbox' value='"$row[flavorname]"'/>";
echo "<td><img src=".$row['image']." width='240px' height='190px'></td>";
echo "<input type=\"checkbox\" name=\"candyFlavors[]\" value=\"$row[flavorname]\">";
}
?>
I think it should be like this.
echo "<input type=\"checkbox\" name=\"candyFlavors[]\" value=\"".$row['flavorname']."\">";
You missed single quote in $row[flavorname]
echo '<input type="checkbox" name="candyFlavors[]" value="'.$row['flavorname'].'">';
Try this