I want to get the data from radio button when I submit it, but I don't know why I can't get the value from radio it.
My code
<?php
$choice = $_GET['choice'];
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="POST">
<table align="center">
<tr><td>Please select</td></tr>
<tr><td><input type="radio" name="choice" value="0">aaaa</td></tr>
<tr><td><input type="radio" name="choice" value="1">bbbb</td></tr>
<tr><td><input type="radio" name="choice" value="2">cccc</td></tr>
<tr><td><input type="radio" name="choice" value="3">dddd</td></tr>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
<?php echo"$choice";?>
</form>
</body>
</html>
Use $_POST
Instead of $_GET
to get data from form. Or you can use $_REQUEST
also.Replace $_GET['choice']
with $_POST['choice']
or $_REQUEST['choice'].
You are using $_GET
, but the form is posted. Use $choice = $_POST['choice']
.
Your form is POST
but you're looking for it in $_GET[]
. Look for it in $_POST['choice']
instead.
The method
of your form element should match the variable you access for the form data:
<form method="post">
// Need to use the $_POST[] array to access values
<form method="get">
// Use the $_GET[] array to access values
$_GET is typically used for url strings. Usually you will want the method of your form to be post
.
If your form has attribute method set to POST, than you should use _POST global array like this - <?php $choice = $_POST['choice']; ?>