View Full Version : Checked? in a Dynamic Radio Group
Jon Gibson
05-31-2009, 02:59 PM
Hello:
What is working:
Dynamically generated a radio group using PHP and MySQL (with Dreamweaver built in Record Set and a loop).
What I can't get done:
How can I determine which radio button has been checked by the user?
What I have tried:
Googleing this thing to death.
I found a site that uses a checked status type thing, but that assumed the values were hard coded. I would like to maintain dynamic values if possible. Also, I realize some (maybe all:) of the php below may not be well formed . . . I am trying to learn how to use Dreamweaver's built in functionality and the generated code is throwing me a bit.
Here is my current Form code:
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
<?php $checkedStatus = 'unchecked'; do { ?>
<?php ?>
<input type="radio" checked="<?php print $checkedStatus; ?>" name="candidate" value="<?php echo $row_getCandidateInfo['numberVotes']; ?>" id="<?php echo $row_getCandidateInfo['id'];?>" /><?php echo $row_getCandidateInfo['candidate'] . "<br />"; ?>
<?php } while ($row_getCandidateInfo = mysql_fetch_assoc($getCandidateInfo)); ?>
<br />
<input name="vote" type="submit" value="vote" />
<input type="hidden" name="MM_update" value="form1" />
</form>
Thank you!
Jon
Ray Borduin
06-01-2009, 07:49 AM
Set the value attribute of the radio input tag.
Set all radio buttons of the same group to have the same name attribute.
The form will behave the same way as if you used and input type text button and they typed in the value of the selected radio.
Jon Gibson
06-02-2009, 08:00 AM
Thank you for the suggestion Ray, but unfortunately it did not work. I have my name attribute hard coded to 'candidate' and the value hard coded to '1' now. The id is dynamic where I used a server behavior to assign the database id to the respective person.
I believe my error is in the MySQL statement. For whatever reason I cannot perform an update that searches for the where of the dynamic id.
I know this is not a MySQL forum, but here is the partial statement I am using in case you have a suggestion:
within an if statement for submit:
$query: "UPDATE candidates SET numberVotes=numberVotes+1 WHERE id=id";
QUESTION:
the original SQL statement that DW generated had numberVotes=%s. What does the %s stand for, as I could not see it repeated within the generated code.
Regardless of a new suggestion, thank you for your help.
Jon
Ray Borduin
06-02-2009, 10:49 AM
%s is probably correct. It is used as a token to be replaced with a parameter. So it would be used to identify the id of the candidate to update. It will need the %s to differentiate which to update.
Jon Gibson
06-04-2009, 05:38 PM
Good afternoon Ray:
I have attempted to figure this out on my own with little success. I have, however, discovered a pattern to my problem:
If I vote for person A, person B (the next person in the voting list) gets that vote and all of the total votes of person A. Even if the person is not 'visible' from my database, they still hog the votes of the previous person.
Here is the SQL code:
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE candidates SET numberVotes=%s+1 WHERE id=%s",
GetSQLValueString($_POST['voteValue'], "int"),
GetSQLValueString($_POST['voteValue'], "int"));
Here is the Form PHP/HTML:
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
<?php do { ?>
<input type="radio" value="<?php echo $row_rsCandidates['numberVotes']; ?>" name="voteValue" id="<?php echo $row_rsCandidates['id']; ?>" /><?php echo $row_rsCandidates['candidate']; ?><br />
<?php } while ($row_rsCandidates = mysql_fetch_assoc($rsCandidates)); ?>
Question:
Would the data and security assist products help create more specific queries (server behaviors)? I have yet to purchase but am strongly considering if they would aid in the server behavior area.
Thank you for the support Ray!
Jon Gibson
06-04-2009, 05:48 PM
I still would like to know about the server behavior bit in the above post, but I believe my problem is that I am using the value property of a form incorrectly.
Namely, since all candidates would start out with 0 votes, and then I tried to update via a vote, the value would conflict. And in the 'pattern' I discovered was just pure coincidence because candidate id 4 had four votes and when I voted for her (adding 1) it would post to candidate id 5 because the new value was 5.
Just thought this might someone else who is making a similar mistake . . .
Ray Borduin
06-05-2009, 07:18 AM
I think it should be:
Here is the SQL code:
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE candidates SET numberVotes=numberVotes+1 WHERE id=%s",
GetSQLValueString($_POST['voteValue'], "int"));
Here is the Form PHP/HTML:
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
<?php do { ?>
<input type="radio" value="<?php echo $row_rsCandidates['id']; ?>" name="voteValue" /><?php echo $row_rsCandidates['candidate']; ?><br />
<?php } while ($row_rsCandidates = mysql_fetch_assoc($rsCandidates)); ?>
Jon Gibson
06-05-2009, 09:18 AM
I actually came to the same solution . . . it boiled down to me incorrectly using the value option within the radio button.
Thank you for answering my SQL question here.
Have a great weekend Ray!
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.