Get Radio Button value using php $_GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madzmar25
Forum Newbie
Posts: 2
Joined: Sat Apr 23, 2011 12:27 am

Get Radio Button value using php $_GET

Post by madzmar25 »

Hi Guys i have a problem of retrieving the value of the radio button via a $_GET[] function... the use of button to retrieve the value of the radiobutton is no problem but i cant do it and i cant find a descend reply regarding this problem when it comes to get method... here is a simple code i hope you can help me guys

***** PLEASE USE THE PHP CODE TAG *****

Code: Select all

<?PHP

if (isset($_GET["submit"])) {
echo $_GET["myradio"];
}

?>

<html>
<head>
</head>
<body>
<form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="get">
One <input type="radio" name="myradio" value="one">
Two <input type="radio" name="myradio" value="two">
<a href="<?php echo $_SERVER["PHP_SELF"] ?>" name="submit">test </a>

</form> 
</body>
</html>
Last edited by madzmar25 on Sat Apr 23, 2011 12:47 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Get Radio Button value using php $_GET

Post by Christopher »

You are submitting your form with the POST method -- the values of the form will be put in to $_POST. If you want to use GET then set method="get" in the form.
(#10850)
madzmar25
Forum Newbie
Posts: 2
Joined: Sat Apr 23, 2011 12:27 am

Re: Get Radio Button value using php $_GET

Post by madzmar25 »

i tried it and used the get in the form instead of post but still i cant retrieve the value... please help
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: Get Radio Button value using php $_GET

Post by Gopesh »

hi,Instead of using a href in form tag,use submit button here is the code.

Code: Select all

<?PHP

if (isset($_GET["submit"])) {
echo $_GET["myradio"];
}

?>

<html>
<head>
</head>
<body>
<form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="get">
One <input type="radio" name="myradio" value="one">
Two <input type="radio" name="myradio" value="two">
<!--<a href="<?php echo $_SERVER["PHP_SELF"] ?>" name="submit">test </a>-->
<input type="submit" name="submit" value="submit" />

</form>
</body>
</html>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Get Radio Button value using php $_GET

Post by Christopher »

Yes, you need it to be a submit button. There is also the problem that if you submit a form with the Enter key in IE you don't get the submit field. A hidden field might be a good idea:

Code: Select all

<?PHP

if (isset($_GET["issubmitted"])) {
echo $_GET["myradio"];
}

?>

<html>
<head>
</head>
<body>
<form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="get">
<input type="hidden" name="issubmitted" value="yes"/>
One <input type="radio" name="myradio" value="one"/>
Two <input type="radio" name="myradio" value="two"/>
<input type="submit" name="submitbutton" value="Submit"/></a>
</form> 
</body>
</html>
(#10850)
Post Reply