How to check all radio buttons based on database values? - mysql

I'm busy making a small comment system. I want users to be able to give ratings, besides giving comments, and right now i'm storing these rating values (that come from radio buttons) in the database. However, when I try to populate radio buttons with rating values for each comment from the database, the following script only checks the radio button that corresponds to the last value in the database.
<?php
$sql = mysql_query("SELECT * FROM comments WHERE id_post = '$id_post'") or die(mysql_error());;
while($affcom = mysql_fetch_assoc($sql)){
$name = $affcom['name'];
$email = $affcom['email'];
$comment = $affcom['comment'];
$rating = $affcom['rating'];
$date = $affcom['date'];
// Get gravatar Image
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;
?>
<div class="cmt-cnt">
<img src="<?php echo $grav_url; ?>" />
<div class="thecom">
<h5><?php echo $name; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
<p>
<input type="radio" name="hoi" value="1" <?php echo ($rating=='1')?'checked':'' ?> />
<input type="radio" name="hoi" value="2" <?php echo ($rating=='2')?'checked':'' ?> />
<input type="radio" name="hoi" value="3" <?php echo ($rating=='3')?'checked':'' ?> />
<input type="radio" name="hoi" value="4" <?php echo ($rating=='4')?'checked':'' ?> />
<input type="radio" name="hoi" value="5" <?php echo ($rating=='5')?'checked':'' ?> />
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php } ?>
This is an image of what it looks like right now:
Can someone tell me how to check all of the boxes (based on database values)?

I don't understand your question properly???
If you want to check the radio buttons according to ratting you can use in this way:
<form>
<p>
<input type="radio" name="hoi1" value="1" checked />
<input type="radio" name="hoi2" value="2" />
<input type="radio" name="hoi3" value="3" />
<input type="radio" name="hoi4" value="4" />
<input type="radio" name="hoi5" value="5" />
<input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
<input type="radio" name="hoi1" value="1" checked />
<input type="radio" name="hoi2" value="2" checked />
<input type="radio" name="hoi3" value="3" />
<input type="radio" name="hoi4" value="4" />
<input type="radio" name="hoi5" value="5" />
<input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
<input type="radio" name="hoi1" value="1" checked />
<input type="radio" name="hoi2" value="2" checked />
<input type="radio" name="hoi3" value="3" checked />
<input type="radio" name="hoi4" value="4" />
<input type="radio" name="hoi5" value="5" />
<input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
<input type="radio" name="hoi1" value="1" checked />
<input type="radio" name="hoi2" value="2" checked />
<input type="radio" name="hoi3" value="3" checked />
<input type="radio" name="hoi4" value="4" checked />
<input type="radio" name="hoi5" value="5" />
<input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
<input type="radio" name="hoi1" value="1" checked />
<input type="radio" name="hoi2" value="2" checked />
<input type="radio" name="hoi3" value="3" checked />
<input type="radio" name="hoi4" value="4" checked />
<input type="radio" name="hoi5" value="5" checked />
<input type="radio" name="hoi6" value="5" />
</p>
</form>
<form>
<p>
<input type="radio" name="hoi1" value="1" checked />
<input type="radio" name="hoi2" value="2" checked />
<input type="radio" name="hoi3" value="3" checked />
<input type="radio" name="hoi4" value="4" checked />
<input type="radio" name="hoi5" value="5" checked />
<input type="radio" name="hoi6" value="5" checked />
</p>
</form>
Use different names for the tags. And you can put them inside the form tag.

Never mind, fixed it. The name was the problem. It should be unique for every set of radio buttons ;). Tnx for your help Dineth

Related

How to check the array of radio buttons is checked or not?

Following is the html content and I want to check whether one of the radio button is checked or not?
<div class="col-sm-4 rating">
<input type="hidden" name="questionId[]" value="4"/>
<input type="radio" class="career_ratings" id="career_star31" name="career_rating_answer[3]" value="1" />
<label for="career_star31" title=""></label>
<input type="radio" class="career_ratings" id="career_star32" name="career_rating_answer[3]" value="2"/>
<label for="career_star32" title=""></label>
<input type="radio" class="career_ratings" id="career_star33" name="career_rating_answer[3]" value="3"/>
<label for="career_star33" title=""></label>
<input type="radio" class="career_ratings" id="career_star34" name="career_rating_answer[3]" value="4"/>
<label for="career_star34" title=""></label>
</div>
https://plnkr.co/edit/vlYNcBeEUkP0wLtcbqtV?p=preview
<!DOCTYPE html>
<html>
<body>
<form>
<div class="col-sm-4 rating">
<input type="hidden" name="questionId[]" value="4" />
<input type="radio" class="career_ratings" id="career_star31" name="career_rating_answer[3]" value="1" />
<label for="career_star31" title="1">1</label>
<input type="radio" class="career_ratings" id="career_star32" name="career_rating_answer[3]" value="2" />
<label for="career_star32" title="2">2</label>
<input type="radio" class="career_ratings" id="career_star33" name="career_rating_answer[3]" value="3" />
<label for="career_star33" title="3">3</label>
<input type="radio" class="career_ratings" id="career_star34" name="career_rating_answer[3]" value="4" />
<label for="career_star34" title="4">4</label>
</div>
<button onclick="check()">Try it</button>
</form>
<script>
function check() {
var radio = document.getElementsByClassName('career_ratings');
var x = false;
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
x = true;
}
}
alert(x)
}
</script>
</body>
</html>

Why am i able to check multiple radio buttons at once?

This is the code i use for my form and I'm using radio button as i know only one can be ticked but for some reason I can click on multiple. How do I fix this?
<form name="Question1" method="post" onsubmit="return CheckAnswer1()">
<input type="radio"
name="Q1opt1"
value="1" >Having loads of pictures on the website<br /><br />
<input type="radio"
name="Q1opt2"
value="2" >Making the website nice and pretty.<br /><br />
<input type="radio"
name="Q1opt3"
value="3" >Making the website most user friendly.<br /><br />
<input type="submit" value="Select Your Answer and Click Here" />
</form>
They should have the same name but different values, that's how the browser distinguishes between sets of radio button groups:
<form name="Question1" method="post" onsubmit="return CheckAnswer1()">
<input type="radio"
name="Q1opt"
value="1" >Having loads of pictures on the website<br /><br />
<input type="radio"
name="Q1opt"
value="2" >Making the website nice and pretty.<br /><br />
<input type="radio"
name="Q1opt"
value="3" >Making the website most user friendly.<br /><br />
<input type="submit" value="Select Your Answer and Click Here" />
</form>
It's because their names are all the same.
Try this:
<input type='radio' name='Qstn1' Id='q1Opt1' value='1' />Opt 1<br />
<input type='radio' name='Qstn1' Id='q1Opt2' value='2' />Opt 2<br />
<input type='radio' name='Qstn1' Id='q1Opt3' value='3' />Opt 3<br />

Only allow one radio button to be checked HTML

I have a series of radio buttons in which i'd only like 1 to be able to be selected. I have them all with the same name so I thought this would do the trick but i can still select all four.
<h1>Portion</h1>
<input type="radio" name="portion_num" value="2" /> Two
<input type="radio" name"portion_num" value="4" /> Four
<input type="radio" name"portion_num" value="6" /> Six
<input type="radio" name"portion_num" value="8" /> Eight
The reason it doesn't work is because your name attribute syntax is wrong.
name"portion_num" should be name="portion_num"
http://jsfiddle.net/65ba8/
Put them inside of form tags.
Like so
<form>
<input type="radio" name="portion_num" value="2" /> Two
<input type="radio" name="portion_num" value="4" /> Four
<input type="radio" name="portion_num" value="6" /> Six
<input type="radio" name="portion_num" value="8" /> Eight
</form>
http://jsfiddle.net/
<form>
<input type="radio" name="portion_num" value="2" />Two
<input type="radio" name="portion_num" value="4" />Four
<input type="radio" name="portion_num" value="6" />Six
<input type="radio" name="portion_num" value="8" />Eight
</form>

Validate a Radio Button

I want to make a simple validation form, which will show warning message, when it will be uncheck in a <div>.
This is what, i come up till now.
<form action="result.php" method="post">
<b>1st question:</b><br />
Option 1 <input type="radio" name="question1" value="Option1" /><br />
Option 2 <input type="radio" name="question1" value="Option2" /><br />
Option 3 <input type="radio" name="question1" value="Option3" /><br />
Option 4 <input type="radio" name="question1" value="Option4" /><br />
<br />
<b>2nd question:</b><br />
Option 1 <input type="radio" name="question2" value="Option1" /><br />
Option 2 <input type="radio" name="question2" value="Option2" /><br />
Option 3 <input type="radio" name="question2" value="Option3" /><br />
Option 4 <input type="radio" name="question2" value="Option4" /><br />
<br />
<input type="submit" value="submit" />
</form>`
you can do this by jQuery. The length attribute it calculate The number of elements in the jQuery object
$("#forms").submit(function(){ //or you can use click event
if ($("input[name='question1']:checked").length > 0){
$('.show_message').html(' ');
$('.show_message').html('selected ');
}
else{
$('.show_message').html(' ');
$('.show_message').html(' no one selected ');
return false;
}
});
and
<div class="show_message"></div>
<form id="form" action="result.php" method="post">
. . . .
</form>

Two radio buttons on at the same time

Hello Can you tell why I am allowed to select two radio buttons in this code
<html>
<head><title>Survey</title>
<body>
<h3>Please Enter the following Information: </h3>
<form method= "post" action="/~fmccown/cgi-bin/printinfo.cgi">
<input type ="hidden" name="input-source" value="survey2.html" />
<p>
Name: <input type="text" name="name" size="30" />
</p><p>
Classification:
<select name="class">
<option>Freshman</option>
<option selected="selected">Sophomore</option>
<option>Junior</option>
<option>Senior</option>
</select>
</p><p>
Gender:
<input type="radio" name="gender" value="M" checked="checked" />Male
<input type="radio" name="gender" value="F" />Female
</p><p>
Email address: <input type="text" name="email" size="30" />
</p><p>
Password: <input type="password" name="pword" />
</p><p>
What are your favorite computer classes?
<br />
<label>
<input type="checkbox" name="class-int" />Internet Development
</label>
<label>
<input type="checkbox" name="class-net" />Networking
</label>
<label>
<input type="checkbox" name="class-gui" />GUI
</label>
<label>
<input type="checkbox" name="class-oop" />OOP
</label>
</p><p>
Are you graduating this spring?
<label>
<input type="radio" name="grad-yes" value="Yes" />Yes
</label>
<label>
<input type="radio" name="grad-no" value="Yes" />No
</label>
</p><p>
<input type="submit" value="Submit Survey" />
<input type="reset" value="Clear Form" />
</p>
</form>
<p>
Thank You!
</p>
</body>
</html>
These two elements should probably have the same name:
<input type="radio" name="grad-yes" value="Yes" />
<input type="radio" name="grad-no" value="Yes" />
Radio buttons get grouped together if and only if they share a common name.
The first pair is called gender, so it works.
The second pair doesn't, since one is called grad-yes and the other grad-no.
The way to go would be:
<label>
<input type="radio" name="grad" value="Yes" />Yes
</label>
<label>
<input type="radio" name="grad" value="No" />No
</label>