I have set the below in my HTML form. How do I retrieve it back in my PHP page(ON Submit)?
foreach ($rows as $i => $value) {
echo "<br/>What do you think about : ";
echo "<li>$value<br/>";
echo "1<input type='radio' value='1' name='answer-$i' /> ";
echo "2<input type='radio' value='2' name='answer-$i' /> ";
echo "3<input type='radio' value='3' name='answer-$i' /> ";
echo "4<input type='radio' value='4' name='answer-$i' /> ";
echo "5<input type='radio' value='5' name='answer-$i' /> ";
}
?>
How do I retrieve my answer back ?
I use POST method for my form. Help me!!
Adding Details.
<?php
echo "hello";
$answer = _POST("answer");
echo $answer-0;
echo $answer[0];
foreach ($answer as $i => $value) {
echo $value;
echo $i;
}
?>
answer-0=3&answer-1=1
Above Values shows that The parameters are set(Get Method). But In POST METHOD I am not able to access them.
Can Someone help me? I still havent resolved the issue.
<form method="post">
<?php
foreach ($_POST as $i => $value) {
echo "$i $value<br/>";
}
echo $_POST['answer-1'];
echo "<br>";
echo $_POST['answer-2'];
$rows = array(
"1" => "bar",
"2" => "foo",
);
foreach ($rows as $i => $value) {
echo "<br/>What do you think about : ";
echo "<li>$value<br/>";
echo "1<input type='radio' value='1' name='answer-$i' /> ";
echo "2<input type='radio' value='2' name='answer-$i' /> ";
echo "3<input type='radio' value='3' name='answer-$i' /> ";
echo "4<input type='radio' value='4' name='answer-$i' /> ";
echo "5<input type='radio' value='5' name='answer-$i' /> ";
}
?>
<input type="submit"/>
</form>
Looks more like, what you need.
So you need to use $_POST array with square brackets (it's an array). This code will build an array of $answers for you, assuming you still have $rows available.
$answers = array();
foreach ($rows as $i => $value) {
$answers[$i] = $_POST["answer-$i"];
}
print_r($answers);
Related
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.
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 is the PHP code to display some content and corresponding 5 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 skills") or die("Failed");
while($result = mysqli_fetch_array($sql)){
?>
<br/>
<?php
echo $result["Technical"];
echo "</br>";
echo"<input type='radio' name='tech[$n]' value='0' required>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>
</html>
The below code is to get the values of the radio buttons and display it corresponding to the data.
<?php
$con=mysqli_connect('localhost','Sanjana','sanjana');
mysqli_select_db($con,'mydatabase');
$sql = mysqli_query($con,"select * from skills") or die("Failed");
while($result=mysqli_fetch_array($sql))
{
$skill = $result["Technical"];
echo $skill;
$op_num = $_POST['tech'];
echo $op_num;
echo "<br/>";
}
?>
It returns an error stating Array to String Conversion. Any idea on how to resolve this??
I have created a database and retrieved some values and show them in my view as a text. I want to show these values as radio buttons. How can i do this?
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo $rec->ans1."<br/>";
echo $rec->ans2."<br/>";
echo $rec->ans3."<br/>";
echo $rec->ans4."<br/>";
}
?>
Like this i have shown my values in database as texts. How can i show these like radio buttons?
Thank you.
Try this
<?php
foreach ($records as $rec) {
?>
<input type="radio" name="" value="<?php echo $rec['ans'] ?>"><?php echo $rec['ans'] ?>
<?php
}
?>
Question should come in another array
You can do this as follows:
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans1_".$rec->id."'>".$rec->ans1."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans2_".$rec->id."'>".$rec->ans2."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans3_".$rec->id."'>".$rec->ans3."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans4_".$rec->id."'>".$rec->ans4."<br/>";
}
?>
i m trying to increment value 1 using while loop condition, i m developing quiz generation system, but stuck here and not found any solution, please help and tell what to do....
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$i++;
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech1; ?>'><?php echo $qech1."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech2; ?>'><?php echo $qech2."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech3; ?>'><?php echo $qech3."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech4; ?>'><?php echo $qech4."<br>"; ?><br>
<?php
}
?>
you seen above code, it is $i = 1; and then i echo $i as well in 4 fields of radio. it retrives around 3 questions from database and each question has 4 values, but all of the total of 12 values of 3 questions has same name that is 1, it is not increment using $i++ not sure why ?
here is out put
<span style='font-size:18px;font-weight:550px; '>english</span><br><input type='radio' name='1' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='jkj'>jkj<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<span style='font-size:18px;font-weight:550px; '>adnan </span><br><input type='radio' name='1' value='jhwjksahajsh'>jhwjksahajsh<br><br>
<input type='radio' name='2' value='kjhjhjh'>kjhjhjh<br><br>
<input type='radio' name='2' value='kjhkjjh'>kjhkjjh<br><br>
<input type='radio' name='2' value='hjkjhj'>hjkjhj<br><br>
<span style='font-size:18px;font-weight:550px; '>sdfsd</span><br><input type='radio' name='1' value='gfgf'>gfgf<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gf'>gf<br><br>
You can use a Heredoc string to help you with this. I made a simple example that will output what you want in a simple way. Take a look:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
echo <<<HTML
<input type='radio' name='$i' value='$qech1'>$qech1<br><br>
<input type='radio' name='$i' value='$qech2'>$qech2<br><br>
<input type='radio' name='$i' value='$qech3'>$qech3<br><br>
<input type='radio' name='$i' value='$qech4'>$qech4<br><br>
HTML;
$i++;
}
The reason it isn't incrementing is because you are calling $i right after incrementing it.
while ... {
$i++
...
..name="$i"...
}
Right now, it goes through the loop once and prints all names as 2. The next time all of them would be 3, etc.
What you want is more of a foreach loop.
I assigned basic values just for my testing purposes. I personally would eliminate assigning $quech1 = $rows[optiona]. You can just work with the array of $rows with the foreach loop.
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$correct=$rows['correct'];
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
You'll see from my inspect element, that you are now getting incremented names:
However, if I'm not mistaken. Don't you generally want all of the names to be the same for a group of radio buttons? You'll want unique ID's. And then have all buttons that are to be grouped together with the same name. This makes it so only one button in that group can be selected. If that is what you're trying to accomplish, use the revised code:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$name = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$name++;
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' id='<?php echo $i; ?>' name='<?php echo $name; ?>'value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
Only change is that $i is being assigned to id now. And we'll declare a $name counter as well. We'll increment that in the while loop, so that it will only be incremented once for each set of button options. And then you use $name in the name attribute for the button.
That's what makes sense to me. But without more information, we don't really know exactly what you're trying to accomplish. :)