Forum Moderators: coopster

Message Too Old, No Replies

Populating radio box in while loop using php

         

reneer

4:57 am on Apr 10, 2010 (gmt 0)

10+ Year Member



Hi

I'm trying to populate some radio buttons by using a while loop to pull information from the DB. I am then selecting one of the radio buttons n parsing the value to another table in the db. This is the code below:

<?php
session_start(); // initialise session
ob_start();

if(isset($_SESSION[voterid])) {
$db=mysql_connect("localhost", "root", "");
mysql_select_db("government", $db);

$sql = "SELECT * FROM Voter WHERE VoterID='$_SESSION[voterid]';";
$result = mysql_query($sql,$db) or die("Cannot run query 1");
if(mysql_num_rows($result)>0) {
$sql = "SELECT c.CandidateName, p.StationName FROM Candidates AS c
LEFT JOIN PollingStation AS p ON p.CountyID = c.CountyID
LEFT JOIN Voter AS v ON v.PollingID = p.PollingID
WHERE VoterID='$_SESSION[voterid]';";
$result = mysql_query($sql,$db) or die("Cannot run query 2");
if(mysql_num_rows($result)>0) {
$myrow=mysql_fetch_array($result);
$voterid=$myrow[VoterID];
}
}
} else {
header("Location: ../xhtml/vlogin.html");
}
echo '<?';
?>


xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Welcome to the Online Voting System | Vote</title>
<link rel="stylesheet" type="text/css" href="../css/index.css" />
<style type="text/css">
legend
{
color:#0000ff;
}
</style>
</head>

<body>
<div id="container">

<div id="header" >
<div class="picture">
<img style="border:0" src="../images/coat.jpg" width="780" height="139" alt="Welcome to the Online Voting System" />

</div>
</div>

<div id="content">

<h3>Welcome to <?php echo $myrow['StationName']?> Polling Station</h3>
<br /><br />
<h3>PLEASE PROCEED WITH ELECTION</h3>

<form name="regform" action = "vinfo.php" method="POST" >
<input type="hidden" name="pid" value="<?php echo $myrow['PollingID']?>" />
<input type="hidden" name="cid" value="<?php echo $myrow['CountyID']?>" />
<input type="hidden" name="eid" value="2010" />
<input type="hidden" name="vid" value="<?php echo $myrow['VoterID']?>" />
<br /><br />
<?php
echo"<table border='1'>";
echo"<tr>
<td><b>Electoral Party Name</b></td>
<td><b>Electoral Candidate Name</b></td>
<td><b>Selection</b></td>
</tr>";

$sql = "SELECT c.CandidateID, c.CandidateName, c.Party FROM Candidates AS c
LEFT JOIN PollingStation AS p ON p.CountyID = c.CountyID
LEFT JOIN Voter AS v ON v.PollingID = p.PollingID
WHERE VoterID='$_SESSION[voterid]';";
$result = mysql_query($sql,$db) or die("Cannot run query 3");

while($myrow = mysql_fetch_array($result)) {
$party=$myrow["Party"];
$cand=$myrow["CandidateName"];
echo "<tr>
<td>".$party."</td>
<td>".$cand."</td>
<td><input type='radio' value="<? print($party['Party']); ?>" name='vote'/></td></tr>";
}
echo"</table>";
?>
<br />
<input style="float:right" type="button" onclick="document.location = '../xhtml/thank.html';" name="cancel" value="Cancel" />
<input style="float:right" type="button" onclick="document.location = 'vinfo.php';" name="Review" value="Review" />

<br /><br /><br /><br />
</div>


<div id="footer">
<br /><br /><br /><br />

<p id="vali">
<a href="http://jigsaw.w3.org/css-validator/">
<img style="border:0;width:88px;height:31px" src="../images/vcss-blue.gif" alt="Valid CSS!" />
</a>

<a href="http://validator.w3.org/check?uri=referer">
<img style="border:0" src="../images/valid-xhtml10-blue.png" alt="Valid XHTML 1.0 Strict" height="31" width="88" />
</a>
</p>

</div>

</div>
</body>

</html>


<? ob_flush(); ?>

The person should be able to select the radio button click on review and be able to see their choice. If choice is acceptable it's just a final submit and automatic logout. I've been searching for code to populate n parse the data with no avail. Am I even on the right track?

Help please?

[edited by: tedster at 6:21 am (utc) on Apr 10, 2010]
[edit reason] removed identifying specifics [/edit]

rocknbil

7:37 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ugh . . . multiple inline PHP statements . . . output that will never validate . . . getting dizzy . . . .

Here's the cliff notes. I can't tell what determines if a radio is checked, but do something like this, I'm using the var $is_checked to represent that.

$radioval = $party['Party'];
$checkedval=(isset($is_checked) and ($is_checked==1))?' checked="checked"':NULL;

echo <input type=\"radio\" value=\"$radioval\" name=\"vote\"$checkedval/>";

checked for HTML 4+, checked="checked" for XHTML.

reneer

4:07 am on Apr 14, 2010 (gmt 0)

10+ Year Member



Hey thx.. I'll check it and let you kno...