Okay this is kind of a noob question but I have been searching and searching and can not find out how to do this. I can not get the values of my radio buttons useing php.
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['doyoubuy'])) {
echo "You have selected :".$_POST['doyoubuy'];
}
}
?>
</body>
</html>
Thank you!!!
You are checking if (isset($_POST['submit'])) {
But your submit button is not actually named...
So just name it:
<input type="submit" value="submit" name="submit">
Then your code for the radios should work fine.
The action should be the current page. In your case is ""
I suggest this HTML and PHP for your consideration:
<html>
<body>
<form method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST) && $_POST != NULL) {
if ( $_POST['doyoubuy'] == 'Yes' || $_POST['doyoubuy'] == 'No') {
$answer = $_POST['doyoubuy'];
echo "You selected :". $answer;
}
}
?>
</body>
</html>
What needs to be tested is whether the form was submitted. If so, provided that each field bears a name in the form, then a value of some kind will be associated with every field, which obviates the need to test if the submit button is set. Also, if a user submitted a form but neglected to fill in, for example, a text input, then that field's value would still be set; it would be set to an empty string.
It is inadvisable to trust a posted form value and consider it safe to use. In this case, I test to be sure that the answer is what I'm expecting, just in case someone might have spoofed the form and changed the answer to something other than what I intended.
By default, if one leaves out the ACTION attribute of the form, its value is the url of the page containing the form.
Related
I have a problem of getting the value of an input element. Thanks in advance for help! This is the sample code:
<form action="#" method="POST">
<input name="X" value="3" />
<?php
//question: how can I put the value of input in a variable
//$_POST['X']; isn't applicable since I am in the same form.
?>
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input name="X" value="3" />
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['X']) == true){
echo $_POST['X'];
}
?>
First of all submitting a form on its own page is a bad practice because whenever you reload the page the form will submit everytime.
Best approach is that you set the action attribute of form to another file or link.
fileOne.php (where your form is located):
<form action="anotherfile.php" method="POST">
<input name="X" value="3" />
<button type="submit" name="submitForm">Submit</button>
</form>
Then in anotherfile.php you will do this:
$check = $_POST["submitForm"];
If(isset($check))
{
$myInputValue = $_POST["X"];
header("Location: fileOne.php/?val".$myInputValue);
}
Then in fileOne.php you can get the variable and its value which is sent via link in the browser like this:
$getInputValue = $_GET["val"];
Now you can echo out the $getInputValue variable wherever you want.
If the the information is not sensitive this is the best approach you got. But if it is, try saving that value in the session and retrieving in the file where you want. I hope it helps. Typed code via app so it might throw an error. But i think this code will work fine. Good luck!
I have a problem with my form. I need it to redirect user to different pages basing on which radio button was selected. User has to choose one of two options and click next, and according to his choice, page should redirect him to other page.
Here is the code as it looks for now
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio2" value="Firma"/>Company
</br>
<input type = "submit", class = "buttonStyle2", value=""/>
</form>
</center>
</fieldset>
and then php code
if(isset($_POST['Company'])
header("Location: http://myaddress.com/company.php");
Big thanks in advance for your help
Here is one way to achieve this.
Sidenote: Make sure you're not outputting before header. Consult this page on Stack about possible Headers already sent..., should this occur and making sure error reporting is set/on.
Otherwise, PHP will fail silently.
if(isset($_POST['radio1']) && ($_POST['radio1']) == "Osoba fizyczna"){
header("Location: http://www.example.com/non_company.php");
}
elseif(isset($_POST['radio1']) && ($_POST['radio1']) == "Firma"){
header("Location: http://www.example.com/company.php");
}
else{
header("Location: http://www.example.com/redirect_to_home.php");
}
Nota: The else would be if the person did not make a choice and simply clicked on submit without making a selection.
while using radio buttons of the same group name in your form:
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
Note about
<input type = "submit", class = "buttonStyle2", value=""/>
remove the commas
<input type = "submit" class = "buttonStyle2" value=""/>
Since HTML source in FF will reveal No space between attributes in red/as an error.
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
</br>
<input type = "submit" class = "buttonStyle2" value=""/>
</form>
</center>
</fieldset>
and
if ( isset($_POST['radio1']) ) {
$filename = $_POST['radio1'] . "php";
header("Location: http://myaddress.com/".$filename);
}
Might be even better to set up an array with allowed values and check if radio1 is in that array.
In brief: is it possible to have multiple submit buttons on a page, and to detect on the receiving page which submit button was clicked?
For example, imagine a page that displays a random movie poster. There are two buttons: "I liked it", "I hated it." When either button is clicked, the form is submitted via POST to itself, where the user's like/dislike is noted in the database before displaying a new random poster.
My instinct was to write
<form action="thispage.php" method="post">
<input type="submit" name="like" value="I liked this." />
<input type="submit" name="dislike" value="I hated this." />
</form>
And then, on the receiving page,
if ($_POST['like'] == 1) ...
But that didn't work. Nor did
if ($_POST['submit'] == "like") ...
So I'm not sure what to do. Can anyone offer me a tip?
Option one is the typical beginners way. You check if your POST array exists, then you check the stored value if it does exist.
Checking if it exists, and checking the exact value prevents output on initial page load.
<?php
if(isset($_POST['action']) && $_POST['action'] == 'like') {
echo "You like it!";
} elseif (isset($_POST['action']) && $_POST['action'] == 'hate' {
echo "You hate it :(";
}
?>
<form action="thispage.php" method="post">
<input type="submit" name="action" value="like" />
<input type="submit" name="action" value="hate" />
</form>
OR....
The switch/case allows you to run a number of predetermined 'answers' against the value of the POST['action'] var.
You can also have a default value if none of the conditions are met.
Read more here: http://www.php.net/manual/en/control-structures.switch.php
<?php
switch($_POST['action']) {
case "like":
echo "You like it";
break;
case "hate":
echo "You hate it";
break;
}
?>
<form action="thispage.php" method="post">
<input type="submit" name="action" value="like" />
<input type="submit" name="action" value="hate" />
</form>
Yes, it is possible. You have the right idea. However, your checks are wrong in your if statements. Rather than checking for a specific value, since the submit buttons have different names you can simply check for their presence in the POST data.
if (isset($_POST['like'])) {
If you wanted to check for the specific value, you would use something like:
if (isset($_POST['like']) && $_POST['like'] === 'I liked this.') {
As you are just learning, I recommend getting familiar with debugging techniques. The easiest in this case to verify the data you get from the form would be to use print_r($_POST).
Try this
<form action="" method="post">
<input type="submit" name="like" value="I liked this." />
<input type="submit" name="dislike" value="I hated this." />
</form>
<?php
if(isset($_POST['like']) ){
echo "User says: ". $_POST['like'];
}
if(isset($_POST['dislike']) ){
echo "User says: ". $_POST['dislike'];
}
?>
I have one page with a simple post form
<form name="click" action="UserOrders.php" method="post">
<input type="hidden" name="amount" value="<?php echo $total ?>">
<input type="image" src="/Templates/images/UpdateButton.png" name="submit">
</form>
and the only thing I want to check is if it submits.. In the other page "UserOrders.php" I just wrote
<?php
if ($_POST['submit']){
echo $_SESSION['ID'];
}
It seems to me irregular that it doesn't work and I would like another set of eyes to check it out.
(if i put the echo $_SESSION['ID'] outside the brackets, it works.)
An input type="image" only defines that image as the submit button and not as an input that can carry over a value to the server. source
An alternative way to see if the form was submitted is to check $_SERVER['REQUEST_METHOD']
if ('POST' === $_SERVER['REQUEST_METHOD']) {
// submitted
}
I agree with the previous answer. You are not actually putting any information to the form and therefore there is nothing for the new form to react to. $_POST['submit"] is looking for data that was sent and not for the method of sending.
There's no actual submit button. You need a
<form action="UsersOrders.php" method="POST">
<input type="whatever you want" name="input">
<input type="submit" name="submit">
</form>
Now if you go into UsersOrders.php and do what you did before:
if($_POST['submit']) {
echo $_POST['input'];
}
You will actually get some input.
I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}