Forum Moderators: coopster
echo "<input type = radio name = Option value ='".$row->Option2"'</input>";
Can anyone explain why?
Thanks in advance.
#!c:/php/php.exe
<?php
// open database connection
$server="localhost";
$user="student1";
$password="student1pw";
$database="localdatabase";
$connect = mysql_connect("$server","$user","$password");
if (!$connect)
{
die('Could not connect!' . mysql_error());
// generate and execute query
$query = "SELECT QuestionID, Question, Option1, Option2, Option3, Option4 FROM compwork";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// if records are present
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_object($result);
if (mysql_num_rows($result) > 0) {
// print answer list as radio buttons
while ($row = mysql_fetch_object($result)) {
echo "<input type = radio name = Option value ='".$row->Option1"'</input>";
echo "<input type = radio name = Option value ='".$row->Option2"'</input>";
echo "<input type = radio name = Option value ='".$row->Option3"'</input>";
echo "<input type = radio name = Option value ='".$row->Option4"'</input>";
}
echo "<input type = submit name = submit value = 'Submit!'>";
}
echo '</form>';
}
if (!mysql_query($sql,$connect))
{
die ('Error' . mysql_error());
}
}
mysql_close($connect)
?>
I just saw your code and find some syntax error on the below lines.
echo "<input type = radio name = Option value ='".$row->Option1"'</input>";
echo "<input type = radio name = Option value ='".$row->Option2"'</input>";
echo "<input type = radio name = Option value ='".$row->Option3"'</input>";
echo "<input type = radio name = Option value ='".$row->Option4"'</input>";
It should be like:
echo "<input type = radio name = Option value ='".$row->Option1."'</input>";
echo "<input type = radio name = Option value ='".$row->Option2."'</input>";
echo "<input type = radio name = Option value ='".$row->Option3."'</input>";
echo "<input type = radio name = Option value ='".$row->Option4."'</input>";
Please let us know actually what error you have got.
Thanks
Mahabub
</input>
I may have missed the memo, why are you using a (invalid? nonexistent?) closing tag on a radio? Honestly I've never seen this.
<input type="radio" name="Option" id="Option1234" value="somevalue">
echo "<input type=\"radio\" name=\"Option\" id="\Option" . $row->Option2 . "\" value=\"". $row->Option2 . "\">";
If you're using an XHTML doctype (which you only should if you're actually using XHTML features,)
echo "<input type=\"radio\" name=\"Option\" id="\Option" . $row->Option2 . "\" value=\"". $row->Option2 . "\"/>";
As for the checked value,
echo "<input type=\"radio\" name=\"Option\" id="\Option" . $row->Option2 . "\" value=\"". $row->Option2 . "\"";
if (isset($row->Option2) and ($row->Option2 != '')) { echo ' checked'; }
echo ">";
Although, by nature of the way a radio button is supposed to work, one of the values should always be checked by default. So it might be better to rethink how you're accessing these, and it would simplify your overall problem. I suggest getting all the options into an array, if none of them have a value assign "checked" to the default one. Then output the radio buttons.
If it's vital that you allow none of them to be checked, you should be using a checkbox.
Parse error: parse error, expecting `','' or `';'' in C:\Apache\Apache2\cgi-bin\RadioButtons.php on line 27
I tried the solution provided by Mahabub and it does not produce this error, it just produces a blank screen, can anyone suggest why this is?
Thanks again
error_reporting(E_ALL);
ini_set('display_errors', '1');
Per the error in your logs, it's (probably) right where it's always been as Mahabub pointed out:
.....value ='" . $row->Option1"'</input>";
.....value ='" . $row->Option1 <where is the dot?> "'</input>";
You're missing concatenation operators (.) Look right after "Option1". When concatenating it's easier to "spot" the missing ones if you add spaces:
....value ='" . $row->Option1 . "'</input>";
It's still an invalid input tag though, unless I'm missing something?
Thank u
Table students
id¦question_id¦answer
(correct answer is blank or 1, question_id, sequence, and answer are integer fields.)
$out_text = NULL;
$select = "select answers.sequence, answers.correct_answer, answers.answer_text, students.answer, from answers,students where answers.question_id=students.question_id and answers.correct_answer<>'' order by answers.sequence asc";
$result=@mysql_query("$select");
if (!$result) { error("Cannot get answers:" . mysql_error()); }
while ($row=mysql_fetch_array($result)) {
list($num,$correct_ans,$ans_text,$student_ans) = $row;
if ($student_ans==$correct_ans) {
$out_text = "<li>Question $num, Correct: $ans_text</li>\n";
}
else {
$out_text = "<li>Question $num, Incorrect, the correct answer is $ans_text</li>\n";
}
}
mysql_free_result($result);
header("content-type:text/html\n\n");
print "<h1>Results</h1>\n";
print "
<ul>
$out_text
</ul>
";