How do I get the value of a radio button in PHP? - php

I've created a basic website that requires the user to select a radio button. I want a PHP file to retrieve the value of the radio button that was chosen and respond accordingly, but the file does not currently produce any output. What is wrong with the code I am using now? Why can my PHP file not retrieve the radio button value properly?
Index.html:
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
</form>
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
Result.php:
<?php
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
?>

Your are using two separate forms for your general input elements and one consisting of a submit button only.
Include the submit button in the first form and it should work fine:
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>

<form method="post">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page-->
<input type="radio" name="MyRadio" value="Second">Second
</br>
<input type="submit" value="Result" name="Result"> <!--This button opens Result.php-->
</form >
In my php code you can see that the function of isset() that set that when your PHP code run. In your code you mention $radioVal = $_POST["MyRadio"]; where MyRadio is undefined index for PHP. Here when we submit the form then submit call the PHP code without any lag and you also use the double form. This is wrong for this code.
<?php
if (isset($_POST['Result']))
{
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
}
?>

This is the form that's being submitted. It has an action attribute which directs it to Result.php.
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
In order for you to get the data you want in Results.php, you need to add the radio buttons to this form
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br>
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result">
</form>
You're also going to need to change your method to POST if you're going to use the $_POST superglobal
$radioVal = $_POST["MyRadio"];

First of all you are doing it a little wrong. You are using two forms to do the task. Let me tell you how can you do it.
index.html
<form action= "result.php" method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page -->
<input type="radio" name="MyRadio" value="Second">Second
<br/>
<input type="submit" value="Result" name="Result"> <!--//This button opens Result.php -->
result.php
<?php
echo $_POST["MyRadio];
// on new page you will get "First" or "Second", depending on what you have selected on html page
?>

You are using two separate forms for the html code, which means the first form is actually not submitted when you press the button.
You shouldn't need to change the PHP code in result.php, but you should ideally use one form.
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>

Related

SUBMIT button value sent only when clicked

I have the following web form:
<form action="processor.php" method="post">
<input type="checkbox" name="cb" onclick="submit()">
<input type="submit" name="search" value="Search">
</form>
So either clicking the submit button or the checkbox this form is submitted to process.php page. But submit's value named "search" is sent only when button clicked explicitly, not when checkbox is clicked. Unexpectedly for me. I expected that submitting the form with submit() command will send all parameters as well (including submit button's "search" parameter).
So in PHP code I cannot use:
if(isset($_POST['search'])
to test if form was submitted, I have to use:
if($_SERVER['REQUEST_METHOD']=='POST')
Is this normal behaviour of submitt button?
Yes this the correct behavior.
The value of a submit input is only send when activated or clicked, since here you submit the form through the function it's logical.
Check this example:
<form action="edit.php" method="post">
<input type="text" name="name">
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Preview">
</form>
This behavior allow multiple submit action in a form.
__
On checkbox click simulate a click on the submit button instead of using submit()
Yes it's normal. I have made some code for you to make it work:).
Add a class to your checkbox and submit button. And add the js code to your html or separate js file.
<form action="processor.php" method="post">
<input type="checkbox" class="checkbox" name="cb" onclick="submit()">
<input type="submit" class="sub" name="search" value="Search">
</form>
It goes in this function when you click on the checkbox, it is seeing when its enabled and if it's so simulate a click on the submit button.
$(".checkbox").change(function() {
if(this.checked) {
$(".sub").click();
}
});

fetching value with the submit button in HTML?

If the submit button is pressed in HTML I need to send some value with it. How can I do it?
<input type="submit" name="ask" class="tbutton" value="ask" />
if(isset($_POST['ask'])){
// i need to fetch some value that is passed with submit button }
I think you want to fetch value of some input right?
may be this will help
<form method="post">
<input type="text" name="myText" />
<input type="submit" name="ask" class="tbutton" value="ask" />
</form>
<?php
if(isset($_POST['ask'])){
echo($_POST['myText']);
}
?>

Form inside another form with same inputs

Is there a way to send exactly same variables to another page different from action of the form? I tried this structure but the second submit button did not work;
<form name="2" action="page2" method="post" >
<form name="1" action="page1" method="post" >
<input type="radio" name="radio" value="value1" >
<input type="radio" name="radio" value="value2" >
<input type="radio" name="radio" value="value3" >
<input type="submit" value="Submit1">
</form>
<input type="submit" value="Submit2">
</form>
The form 1 shows the information of inputs on the page1 and updates database also.
I want form 2 to show the information of inputs on the page2 only (no update of database).
Is that possible?
You cannot have form inside another form , change your code like this.
<form name="2" action="page2" method="post" >
<input type="radio" name="radio" value="value1" >
<input type="radio" name="radio" value="value2" >
<input type="radio" name="radio" value="value3" >
<input type="submit" value="Submit1" name="submit1">
<input type="submit" value="Submit2" name="submit2">
</form>
Give different name for each submit button.
in php
if(isset($_POST['submit1'])){
// submit1 is pressed
}
if(isset($_POST['submit1'])){
// submit2 is pressed
}
Changing action dynamically.
add class to submit buttons say, class="submit". ANd add id to form say id="my-form"
$(".submit").change(function() {
var action = $(this).val() == "submit1" ? "submit1.php : "submit2.php";
$("#my-form").attr("action", action);
});

passing values from one form to another form with in the same page in php

I have 2 forms named form1 and form2 in a single php page.form1 contains one text box.form2 contains two text boxes with a submit button.I want to validate all the textboxes when ever submit button pressed.But I cannot access the textbox value in form 1.My code is given below.
<html><body>
<form name=form1 method=POST>
<input type=text name=f1_t1>
</form>
<form name=form2 method=POST>
<input type=text name=f2_t1>
<input type=submit name=sub1>
</form></body></html>
<?php
if(isset($_POST['sub1']))
{
$name=$_POST['f1_t1'];
echo $name;}
?>
this code error as undefined variable f1_t1.Can anyone help please?
I suggest putting everything in a single form, and displaying/hiding sections of it as necessary (for example, with jQuery and CSS).
As an example, you could do this:
<form>
<div id="part1">
<input name="t1" type="radio" value="v1" />
<input name="t1" type="radio" value="v2" />
</div>
<div id="part2" style="display: none;">
<input name="t2" type="text" />
<input type="submit" />
</div>
</form>
Only the fields in one form are submitted.
If for some reason you need to get the falues from a second form, you need to do that before posting, that is, on the client side. You could for example use javaScript to read the value of the field from the first form, write it to a hidden field in the second, and then post that to the server.

populate radio button value after submit

I try to implement the follwing code:
<?php
$yesno1="";
$yesno2="";
$option1="";
$option2="";
$option3="";
if(isset($_POST['submit'])){
$yesno1=$_POST['yesno1'];
$yesno2=$_POST['yesno2'];
$option1=$_POST['option1'];
$option2=$_POST['option2'];
$option3=$_POST['option3'];
}
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<input type="radio" name="yesno1" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno1" value="no" style="outline:0;"/>No
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="outline:0;"/>No
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
<input type="submit" value="Submit" name='submit'>
</form>
Once I click submit button, normally the value I seleted in radio and marked in the checkbox are gone, so how can I keep the value even after the click the submit button or refrsh the page using php, javascript is fine as well, would be better if any one can help me with it on both php and javascript since I am not very good on both of them, thanks for kind help:)
An example below
<input type="checkbox" name="option1" value="Milk" <?php echo ($_POST['option1'] == 'Milk' ? 'checked' : '') ?>> Milk<br>
From MDN:
The input (<input>) element is used to create interactive controls for
web-based forms.
Attributes
type
[...]
radio: A radio button. You must use the value attribute to define the
value submitted by this item. Use the checked attribute to indicate
whether this item is selected by default. Radio buttons that have the
same value for the name attribute are in the same "radio button
group"; only one radio button in a group can be selected at one time.
This is a basic HTML question. Learn to walk before you run ;-)

Categories