0

Hi ppl...

I have a piece of html code like this, to generate 10 radio button having values from 1 to 10. When the user selects a radio button, i want to print only that value. Any inputs on how i can achieve this is very much appreciated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>

<body>
<form action="evaluate.php" method="post">
  	happy: <br/>
  	<?php
  	
	for($i=1;$i<=10;$i++) 
	{ 
		echo $i ;
	?>
		<input type="radio" name="a[]" value= "<?php echo $i ?>"/>
		
	<?php 
	}?>

<input type="submit" value="Submit" />
</form>



</body>

</html>
<?php
//end
?>

Thanks for your time..

4
Contributors
5
Replies
10
Views
5 Years
Discussion Span
Last Post by jmichae3
1

@puvi, radio buttons will only return one value so you don't need the array braces in the field name. That technique works great for checkboxes and multi-select dropdown lists.

You can retrieve the value as $_POST as a string. As your code stands now, you will receive an array.

<input type="radio" name="a" value= "<?php echo $i ?>"/>
0

Thanks a ton madCoder, that worked out well..

0

are you SURE you don't just want to simply have the radio button checked instead? that would be s simple job of having the word
checked
in the <input type="radio" ...> tag.

you are missing a terminating semicolon after the echo statement.
html 4.01 does not use /> for an end.
example of what a radio button group is supposed to look like:

<form>
<input type="radio" name="color" value="red">
<input type="radio" name="color" value="blue">
<input type="radio" name="color" value="green">
<input type="radio" name="color" value="maroon">
<input type="radio" name="color" value="olive">
<input type="submit">
</form>
<?php
//did the radio button variable come back?
//sanity check in case someone is hacking
if (isset($_POST['a'] && int($_POST['a']) >= 1 && int($_POST['a']) <= 10) {
	//process form
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form action="evaluate.php" method="post">
happy: <br/>
<?php
	//are you sure you don't want a <br> in these?
	for ($i = 1; $i <= 10; $i++) { 
		if (int($_POST['a']) == $i) {
			//form variable that came back matches current radio control.  show it as checked
			echo $i."<input type=\"radio\" name=\"a\" checked value=\"$i\">\n";
		} else {
			echo $i."<input type=\"radio\" name=\"a\" value=\"$i\">\n"; 
		}
	}
?>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
} else {
    //nope.  show the form
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form action="evaluate.php" method="post">
happy: <br/>
<?php
	for ($i = 1; $i <= 10; $i++) { 
		echo $i."<input type=\"radio\" name=\"a\" value=\"$i\">\n";
	}
?>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
} //endif
//end
?>

Edited by jmichae3: n/a

0

Belo Code Should be save in anything.html file

<form method="post" action="anything.php">
<input type="radio" name="gender" value="Male"><br>
<input type="radio" name="gender" value"Female"><br>
<input type="submit" value"Check">
</form>

Below code should be save in anything.php file

<?php
$gender = $_POST['gender'];
if($gender=="Male")
{
echo "Gender is Male";
}
else
{
echo "Gender is Female";
}
?>
0

Hi ppl...

I have a piece of html code like this, to generate 10 radio button having values from 1 to 10. When the user selects a radio button, i want to print only that value. Any inputs on how i can achieve this is very much appreciated.
...
Thanks for your time..

given this specification only and ignoring the code you gave, simply displaying the value you got from the radio button, it would be...

radio buttons are grouped simply by name. for instance, with the following form:

<?php 
if (isset($_POST['a'])) {
    //form was submitted.  display the radio button value instead of the form
    //if you want to display the form with the current value checked, you can here.
    echo "<p>".$_POST['a']."</p>\n";
} else {
    //form was not submitted, so display the form.
    echo "<form action=\"somephp.php\" method=\"post\">\n";
    for ($a=1; $x <= 10; $x++) {
        echo "$x<input type=\"radio\" name=\"a\" value=\"$x\"/><br />\n";
    }
    echo "<input type=\"submit\"/>\n";
    echo "</form>\n";
}
?>

php returns the radio button and all post variables using the name= attribute with that name= as a $_POST[] map index. for example, your name="a" would come out as $_POST.

you can read all about it in the PHP chm manual, which I suggest you get. it will help you get started much faster. take a look at the
notes at the bottom of help pages for tips.

http://www.php.net/download-docs.php

Edited by jmichae3: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.