sql display data in radio buttons - php

i have few answers to quiz stored in db. i am displaying them as radio buttons on form like this
$row = mysql_fetch_array(mysql_query('select * FROM quiz order by rand() limit 1'),MYSQL_ASSOC);
<?php
foreach ($row as $key => $value) {
echo "<input type='radio' name='answer'>".$value."</input><br />";
}
?>
but when i post this form the echo on other end is not getting a radio button answer i selected. please help

echo "<input type='radio' name='answer' value='".$value."'>".$value."</input><br />";

The only things that I can see is that
a) There is no form tag outside of the radio
b) You aren't giving the radio a value such as:
<input type='radio' name='answer' value="1"> Male
<input type='radio' name='answer' value="2"> Female

Related

Automatic marking

Am puzzled about how to make a system that can automatically mark.
Am using a variable $count in PHP and is the one storing data from the checked radio button.
I have not tried to check on how to do it and am just afraid of how to do it.
I have the following codes which out puts the data in radio buttons
<?php
$get=mysql_query("select * from courses where courseid='".$cid."'");
while ($picked=mysql_fetch_array($get)) {
echo "<b>The questions for ".$picked['coursename']." are:</b><br><br>";
}
$query=mysql_query("select * from papers where cid='".$cid."'");
if (mysql_num_rows($query)>0) {
$count=1;
while ($row=mysql_fetch_array($query)) {
echo "Qn ".$count.". ".$row['question']."<br><input type='radio' name='".$count."' value='A'>".$row['A']."<br><input type='radio' name='".$count."' value='B'>".$row['B']."<br><input type='radio' name='".$count."' value='C'>".$row['C']."<br><input type='radio' name='".$count."' value='D'>".$row['D']."<br><br>";
$count++;
}
}else{
echo "No papar has been set yet for this paper.";
}
?>
<input type="submit" name="but" value="Submit Assessment">
and it outputs the following:
And the database is like:

How to check which radio button a user checked if not in a form

I have a textfile with information that I am using to create a quiz. I am only displaying one quiz question at a time however. I am generating radio buttons based on the number of questions and then I have a submit button after the loop that generates the radio buttons. I can't figure out how to get which radio button the user selected in the $_POST array when I click submit. My initial thought is to use a form tag and then have the loop run but I don't know if this works or how to make it work syntactically.
textfile (the last number is the index of the right answer):
What does charmander evolve to?#Charmeleon:charizard:squirtle#0
WHo is the main character in Pokemon?#Misty:Ash:Obama#1
Script:
<?php
$indexTemp = intVal($_SESSION["index"]);
if($_SESSION["numQuestions"] == $indexTemp){
echo "Your Results are: ";
echo "<form action=\"process.php\" method=\"post\"> Back to Main screen: <input type=\"submit\"><br \> </form>";
exit();
}
$filename = $_SESSION["quizOfTheDay"];
$quizStuff = file($filename);
$ctr =1;
$questionInfo = $quizStuff[$indexTemp];
$questionParse = explode("#", $questionInfo);
$_SESSION["correctAns"] = $questionParse[2];
echo $_SESSION["correctAns"]." from line 55 <br />";
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";
#This is where the radio buttons are being generated
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans$ctr' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}
$_SESSION["index"] = $indexTemp +1;
echo "<form action=\"questions.php\" method=\"post\"> <input type=\"submit\"><br \> </form>";
?>
Obtaining data from radio buttons and checkboxes can be a little tricky, generally due to a lack of understanding of how radio buttons and checkboxes work.
It is important to remember two facts:
Checkbox "Names" Must Be Unique to Each Checkbox
Radio Button "Names" Must Be Identical For Each Group of Buttons
Update the foreach since you must have same name for all the radio buttons but with different values.
<?php
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value=".$ctr."> <label for='q1'>".$answerChoice."</label> <br />";
$ctr +=1;
}
?>
Now your for-each concatenation also looks wrong and i have updated it and since the concatenation is wrong the value will not be displayed in the radio button. The radio button value will be the count of the increment variable.
remove $ctr
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}

How to Keep Checkbox checked when summon with foreach

i have checkbox summon with forearch like this
foreach( $orderarray as $key => $cucian )
{
switch ($cucian['tipe']) {
case 'cuci dan setrika':
echo "<input type='checkbox' name='cuci' /> Cuci";
echo "<input type='checkbox' name='setrika' /> Setrika";
break;
case 'setrika':
echo "<input type='checkbox' name='cuci' disabled /> Cuci";
echo "<input type='checkbox' name='setrika' /> Setrika";
break;
}
}
i have read this link :
PHP keep checkbox checked after submitting form
and add this
<?php if(isset($_POST['setrika'])) echo "checked='checked'"; ?>
but why after submitting form , all checkbox with name 'setrika' is checked
any method to solve my problem ?
thanks in advance
Either you give different names to your checkboxes, or use the array notation:
<input type='checkbox' name='setrika[]'>
Notice the [] brackets: you'll have to iterate over $_POST['setrika'], which will be an array:
$_POST['setrika'][$n]
grants you access to the $n-th position of your array.

Issues with javascript validation of radio buttons on php form

This is the code for the php file containing the form with radio buttons:
<?php
while($r7 = mysql_fetch_array($rt3)){
$name=$r7["name"];
$s=$r7["question"];
echo "
<div>$s</div>
<form name='Tester' method='post' onSubmit='return RegValidate(this);' action='quest.php' >
<input type='radio' name='w1' value='1'>1<br>
<input type='radio' name='w1' value='2'>2<br>
<input type='radio' name='w1' value='3'>3<br>
";
}
echo" <input name='Submit' value='Tester' type='submit' />
</form>";
?>
The $name and $q are derived from the database using a mysql query. This works fine.
I have used the following javascript code so far:
function RegValidate(Tester)
{
if($('#w1').not(':checked'))
{
alert('Radio not checked');
return false;
}
}
But with this code, I continue to get the error message, and I am not able to move on even though I have selected a radio button.
The while loop produces several sets of radio buttons, as $q, gets the question from the database and posts it on the page along with 3 radio buttons. Each question $q, has the 3 radio buttons in the above, hence the reason for the $name.
How do I validate this form with javascript. NB. The form will only contain radio buttons.
function RegValidate(Tester) {
if ($(":radio[name=w1]:checked").toArray().length == 0) {
alert('nothing selected');
return false;
}
}​
FIDDLE
you have an error, your input doesn't have ID or CLASS, so add to all of the input radio class='w1' and try the next code:
if ($(".w1 option:selected").length<1)
alert ('nothing selected');
also take a look to this post: Check if option is selected with jQuery, if not select a default
do these things:
<input type='radio' class='w1' name='w1' value='1'>1<br>
<input type='radio' class='w1' name='w1' value='2'>2<br>
<input type='radio' class='w1' name='w1' value='3'>3<br>
And in js:
Latest edit:
if ( $(':radio[class=w1]').attr('checked') != 'checked' ) {
alert('Radio not checked');
}

getting Checkbox value in php

When I pass the html form having checkboxes to a php page, when I retrieve them I get value true for all the boxes that I have checked, and false for unchecked boxes. I need to get the value only.
<?php
$contact=$_POST['contact'];
foreach ($contact as $conid){
echo $conid
}
?>
One possible approach is to set names like that:
<input type='checkbox' name='box[1]'>
<input type='checkbox' name='box[2]'>
<input type='checkbox' name='box[3]'>
This way you could access them in PHP with
foreach ($_POST['box'] as $id=>$checked){
if ($checked =='on')
//process your id
}
The second approach is more clean, I think. Set value's attributes on checkboxes:
<input type='checkbox' name='box[]' value='1'>
<input type='checkbox' name='box[]' value='2'>
<input type='checkbox' name='box[]' value='3'>
With this you'll receive only checked values:
foreach ($_POST['box'] as $id){
//process your id
}

Categories