hi,
Can any one help me. I am facing problem with the radio button using while loop. i need to to select the choice for any number of questions displayed on the screen and insert the selected values to the table. currently I am able to select choice for only 1 question with the coding given below. As i am new to PHP can anyone help me.
Thanks
sreem
<html>
<head>
<title>PHP</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body>
<?
require "config.php"; // user passwd dbname
$limit = 20; // No of records selected
$counter = 1;
$query2=" SELECT * FROM exam_ques ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
?>
<table align=center border=1 width=80% height=150>
<tr>
<td width=100% id=ex align=center colspan=9>Module : <? echo $info['module_no']; ?> </td>
</tr>
<?
$query=" SELECT * FROM exam_ques limit $limit ";
$result=mysql_query($query);
$totalrow=mysql_num_rows($result);
echo mysql_error();
$i=1;
{
while($info = mysql_fetch_array( $result ))
{
{
?>
<tr>
<td colspan=9><br>
</td>
</tr>
<tr>
<td width=5% id=ex>Question :<? echo $counter++; ?></td>
<td colspan=9 width=100%> <font size="5"><? echo $info['question']; ?></font></td>
</tr>
<tr>
<td colspan=9><br>
</td>
</tr>
<td align=center colspan=9 id=ex>Choices</td>
</tr>
<form name="thisForm" action="insert_answers_to_table.php" method="post">
<?
?>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_1']; ?>" name="radio1"><? echo $info['choice_1']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_2']; ?>" name="radio1" ><? echo $info['choice_2']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_3']; ?>" name="radio1" ><? echo $info['choice_3']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_4']; ?>" name="radio1"><? echo $info['choice_4']; ?><br></td>
</tr>
<?
}
}
}
?>
</table>
<table border=0 width=80% align="center">
<tr>
<td align="center">
<input id="ex" type="submit" name="submit" value="Submit" action="insert_answers_to_table.php">
</form>
</td>
</tr>
</table>
</body>
</html>
while loop with radio buttons
Moderator: General Moderators
Re: while loop with radio buttons
skmdas wrote: <tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_1']; ?>" name="radio1"><? echo $info['choice_1']; ?><br></td>
</tr>
This code currently shows that all questions will be named "radio1".
change to below:
Code: Select all
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_1']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_1']; ?><br></td>
</tr>
Re: while loop with radio buttons
Thanks a lot Mr. Mark. Could you pls help me in inserting the values to the table once the submit button is clicked .
<html>
<head>
<title>TEST QUESTIONNAIRE</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
require "config.php"; // user passwd dbname
$limit = 5; // No of records to be shown per page.
$query2=" SELECT * FROM TEST_EXAMS ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
?>
<table width=80% align=center cellpadding=0 cellspacing=0 border=0>
<tr>
<td bgcolor='dfdfdf'>
<font bgcolor='#dfdfdf' face='arial,verdana,helvetica' style='font-weight: 300' color='#000000'><h2 align="center">QUESTIONNAIRE</h2></font>
</td>
</tr>
</table>
<table align=center border=1 width=80% height=150>
<tr>
<td width=100% id=ex align=center colspan=9>Module : <? echo $info['module_no']; ?> </td>
</tr>
<?
$query=" SELECT * FROM TEST_EXAMS limit $limit ";
$result=mysql_query($query);
echo mysql_error();
while($info = mysql_fetch_array( $result ))
{
?>
<tr>
<td colspan=9><br>
</td>
</tr>
<tr>
<td width=5% id=ex>Question :<? echo $counter++; ?></td>
<td colspan=9 width=100%> <font size="5"><? echo $info['question']; ?></font></td>
</tr>
<tr>
<td colspan=9><br>
</td>
</tr>
<td align=center colspan=9 id=ex>Choices</td>
</tr>
<form name="thisForm" action="TESTINSERT.PHP" method="post">
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_1']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_1']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_2']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_2']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_3']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_3']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_4']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_4']; ?><br></td>
</tr>
<?
}
?>
</table>
<table border=0 width=80% align="center">
<tr>
<td align="center">
<input id="ex" type="submit" name="submit" value="Submit" >
</form>
</td>
</tr>
</table>
</body>
</html>
TESTINSERT.PHP
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass1';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$answer = addslashes ($_POST['radio1']);
}
else
{
$answer = $_POST['radio1'];
}
$sql = "INSERT INTO test_answers ".
"(answer) ".
"VALUES('$answer)";
mysql_select_db('dbexam');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
include test_exams.php';
?>
Thanks
Sree
<html>
<head>
<title>TEST QUESTIONNAIRE</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
require "config.php"; // user passwd dbname
$limit = 5; // No of records to be shown per page.
$query2=" SELECT * FROM TEST_EXAMS ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
?>
<table width=80% align=center cellpadding=0 cellspacing=0 border=0>
<tr>
<td bgcolor='dfdfdf'>
<font bgcolor='#dfdfdf' face='arial,verdana,helvetica' style='font-weight: 300' color='#000000'><h2 align="center">QUESTIONNAIRE</h2></font>
</td>
</tr>
</table>
<table align=center border=1 width=80% height=150>
<tr>
<td width=100% id=ex align=center colspan=9>Module : <? echo $info['module_no']; ?> </td>
</tr>
<?
$query=" SELECT * FROM TEST_EXAMS limit $limit ";
$result=mysql_query($query);
echo mysql_error();
while($info = mysql_fetch_array( $result ))
{
?>
<tr>
<td colspan=9><br>
</td>
</tr>
<tr>
<td width=5% id=ex>Question :<? echo $counter++; ?></td>
<td colspan=9 width=100%> <font size="5"><? echo $info['question']; ?></font></td>
</tr>
<tr>
<td colspan=9><br>
</td>
</tr>
<td align=center colspan=9 id=ex>Choices</td>
</tr>
<form name="thisForm" action="TESTINSERT.PHP" method="post">
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_1']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_1']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_2']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_2']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_3']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_3']; ?><br></td>
</tr>
<tr><td width=5%> </td>
<td colspan=9 width=500><input type="radio" value="<? echo $info['choice_4']; ?>" name="radio_<?php echo $counter;?>"><? echo $info['choice_4']; ?><br></td>
</tr>
<?
}
?>
</table>
<table border=0 width=80% align="center">
<tr>
<td align="center">
<input id="ex" type="submit" name="submit" value="Submit" >
</form>
</td>
</tr>
</table>
</body>
</html>
TESTINSERT.PHP
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass1';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$answer = addslashes ($_POST['radio1']);
}
else
{
$answer = $_POST['radio1'];
}
$sql = "INSERT INTO test_answers ".
"(answer) ".
"VALUES('$answer)";
mysql_select_db('dbexam');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
include test_exams.php';
?>
Thanks
Sree
Re: while loop with radio buttons
Wouldn't it be easier to use arrayed checkboxes?
Then dump the POST for that field into an array and parse through it
Code: Select all
<input type=checkbox name=userinput[] value="hello" />
<input type=checkbox name=userinput[] value="world" />Code: Select all
if (isset($_POST[userinput]) {
$valuearray = $_POST[userinput];
foreach ($valuearray as $key=>$value) {
$query = "insert into mytable (id, column) values (0, $value)";
$result = mysql_query($query);
}
}