PHP - button without submit button
test.php
<form action="test2.php" method="post">
Q1: <br />
<input type="radio" name="q1" value="true" />T<br />(Correct Answer)
<input type="radio" name="q1" value="false" />F<br />
Q2: <br />
<input type="radio" name="q2" value="true" />T<br />
<input type="radio" name="q2" value="false" />F<br />(Correct Answer)
<input type="submit" value="Score" />
</form>
test2.php
<?php
//process code from test.php
?>
I want to get the value of the radio buttons from each question and check if it is true or false. I try this in test2.php:
(1) if($_POST['name'])
โ get error (undefined index: name)
(2) if($_POST['submit'])
โ get error (undefined index: submit)
(3) if(isset($_POST['name'])
โ no error but nothing happened
How can I solve it?
source to share
You need to set the name :
<input name="submit" type="submit" value="Score" />
and get a value like this:
$_POST['q1'];
$_POST['q2'];
The name attribute is what you send to POST/GET
the PHP script.
if(isset($_POST['submit']))//don't forget to check using isset()
{
/*other variables*/
$radio_value1 = $_POST['q1'];
$radio_value2 = $_POST['q2'];
}
source to share
You called a radio button like q1
and q2
, but trying to get access to it with a name name
. Instead, if($_POST['name'])
you need to do like
if($_POST['q1'])
and
if($_POST['q2'])
Also to check if the form is submitted or not, you can try this code.
if(isset($_POST))
Whether you use if($_POST['submit'])
to validate the form submission or not, submit
will be the name of the submit button. Therefore, you need to set the property name
to send.
<input name="submit" type="submit" value="Score" />
source to share