The following form does not carry form variables when it is submitted using quto javascript after 5 seconds.
Normal submission works fine.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<SCRIPT LANGUAGE="JavaScript"><!--
setTimeout('document.test.submit()',5000);
//--></SCRIPT>
</head>
<body>
<form name="test" id="form1" method="post" action="auto2.php">
<p>
<INPUT TYPE=checkbox NAME="test" VALUE="option1"> Option1
<INPUT TYPE=checkbox NAME="test" VALUE="option2"> Option2
<INPUT TYPE=checkbox NAME="test" VALUE="option3"> Option3
</p>
<p><input type="submit" name="submit_test" value="Submit_test" />
</p>
</form>
</body>
</html>
---------------------------------------------------------------
output page:
--------------------------------------------------------------
<?php
if(isset($_POST['submit_test'])){
$variable=$_POST['test'];
echo $variable;
}
?>
Any help is highly appreciated.. thank you.
You are testing whether the submit_test button value is set:
if(isset($_POST['submit_test'])){
Submitting the form automatically won't set the value, so your test fails.
You should test for some other form field, like a hidden element.
Unless a checkbox is checked, you wont get any response (blank or otherwise)
What you should have instead is
<input type=hidden name="test" value="">
<INPUT TYPE=checkbox NAME="test" VALUE="option1"> Option1
<INPUT TYPE=checkbox NAME="test" VALUE="option2"> Option2
<INPUT TYPE=checkbox NAME="test" VALUE="option3"> Option3
That way you get a default value when you post your form
+1 to Pekka's answer.
If you do the following it will check to see that the page was posted to without requiring that a variable was set in the post.
if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }
You should probably also put in some form of validation to make sure it came from where you expected it to.
Related
I have a question about radio buttons in html and linking into php. And i dont know how to link it without the name part because i need the name to be all 1 otherwise you can select every radiobutton. My second question is how do i link the button into php.
This is my HTML code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="" name="pepperoni">Pepperoni<br>
<input type="radio" value="" name="ananas">Ananas<br>
<input type="radio" value="" name="ansjovis">Ansjovis<br>
<input type="radio" value="" name="broccoli">Broccoli<br><br>
<input type="submit" value="" name="Bestellen" value="Bestellen"><br>
</form>
</body>
</html>
So how do I say when pepperoni is selected echo "You ordered pepperoni pizza."
This is my PHP Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$pep = $_POST["pepperoni"];
$ana = $_POST["ananas"];
$ans = $_POST["ansjovis"];
$bro = $_POST["broccoli"];
?>
</body>
</html>
You can give the radio buttons all the same name with different values. So you can select 1.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="Pepperoni" name="type">Pepperoni<br>
<input type="radio" value="Ananas" name="type">Ananas<br>
<input type="radio" value="Ansjovis" name="type">Ansjovis<br>
<input type="radio" value="Broccoli" name="type">Broccoli<br><br>
<input type="submit" name="Bestellen" value="Bestellen"><br>
</form>
</body>
</html>
And in PHP you can read
<?php
echo $_POST['type']; //Pepperoni, ananas(pineapple?) etc.
?>
Good luck!
Radio buttons let a user select ONLY ONE of a limited number of choices, There for your radio button will need to have one name attribute for that particular groups of items. Then will have different values.
Your form should look like this :
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="pepperoni" name="pizzaType">Pepperoni<br>
<input type="radio" value="ananas" name="pizzaType">Ananas<br>
<input type="radio" value="ansjovis" name="pizzaType">Ansjovis<br>
<input type="radio" value="broccoli" name="pizzaType">Broccoli<br><br>
<input type="submit" value="Bestellen" name="Bestellen" value="Bestellen"><br>
</form>
</body>
</html>
as you can see above all the radios have the same name attribute pizzaType
Then on pizza.php you need to execute the script when the submit button is clicked, then check if an option was selected, if option was selected then return the choice user selected, if they did not return a message that the user must atleast select an item.
your pizza.php should look like:
<?php
if(isset($_POST['Bestellen'])){ //button clicked
if(isset($_POST['pizzaType'])){//check if choice is checked
echo "You ordered ".$_POST['pizzaType']. " pizza";
}else{
echo "Please select an option";
die();
}
}
?>
NB: For your own good you will also need to learn how to validate and sanitize user inputs. This is very important, you must always treat form submissions as if they from a very dangerous hacker, which it's very important to filter and sanitize the user input before using it.
If you choose a pizza you can just pick 1. So you have to give every radio the Name pizzaName for example and for value you can use the Name (pepperoni). If you press submit then you can just read the values with the PHP File like this:
$chosenPizza = $_POST['pizzaName'];
echo $chosenPizza;
And the output will be pepperoni!
Instead of having different names, make all your names the same and enter what you have as names now, as values. Then get the value by using $val = $_POST["yourvalue"];
eg:
<input type="radio" value="ananas" name="samenameforall">
$val = $_POST["samenameforall"];
You've misused the radio tag attributes a bit here. The type of pizza should be the value, not the name of the radio-button. So if you change the name to type, you can use the following line in PHP to (safely) echo out the user's choice:
// If we have something submitted, process the form,
if (isset ($_POST['order'])) {
// Pepperonies are special!
if ($_POST['type'] == 'pepperoni') {
$output = "Congrats! You ordered pepperoni!";
} else {
// Use of htmlspecialchars () to prevent XSS attacks.
$output = "You ordered ".htmlspecialchars ($_POST['type'], ENT_QUOTES, 'UTF-8')." pizza.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
<fieldset>
<legend>Wat op pizza:</legend>
<input type="radio" id="pepp" value="pepperoni" name="type"><label for="pepp">Pepperoni</label>
<input type="radio" id="anna" value="ananas" name="type"><label for="anna">Ananas</label>
<input type="radio" id="ansj" value="ansjovis" name="type"><label for="ansj">Ansjovis</label>
<input type="radio" id="brocc" value="broccoli" name="type"><label for="broc">Broccoli</label>
</fieldset>
<fieldset>
<input type="submit" name="order" value="Bestellen">
</fieldset>
</form>
<?php
// Only show output if there is something to show.
if (!empty ($output)) {
echo "<p>$output</p>";
}
?>
</body>
</html>
Note the use of htmlspecialchars () here, as it's used to prevent XSS attacks.
When working with radio buttons, you should group them with the same name of the radio element. Take a look at this:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$toppings = filter_input(INPUT_POST, "toppings");
?>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="pepperoni" name="toppings">Pepperoni<br>
<input type="radio" value="ananas" name="toppings">Ananas<br>
<input type="radio" value="ansjovis" name="toppings">Ansjovis<br>
<input type="radio" value="broccoli" name="toppings">Broccoli<br><br>
<input type="Submit" value="Bestellen">
<?php
echo "<br>";
if($toppings != "") {
echo "You ordered $toppings pizza. ";
}
?>
</form>
</body>
<input type="radio" value="pepperoni" name="type">Pepperoni<br>
<input type="radio" value="ananas" name="type">Ananas<br>
<input type="radio" value="ansjovis" name="type">Ansjovis<br>
<input type="radio" value="broccoli" name="type">Broccoli<br><br>
do like this .
My HTML code is as follows:
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
</form>
And my PHP is simply
$temp = $_GET["interval"];
I already have another form of buttons and a select that I am successfully retrieving input from, what am I doing wrong here?
To answer some questions, installs.php is the name of the single file that I am writing this in. There are no other elements called 'interval'.
EDIT:
When I attempted to remove all other code in order to post it here, the call stack disappeared. It seems as though the issue may be elsewhere? I will update when I find the problem.
EDIT 2:
I was mistaken about the earlier problem, I have successfully pared down the code with the problem still persisting. Now the entirety of installs.php is as such:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
</form>
</body>
</html>
<?php
$temp = $_GET["interval"];
?>
Check if variable exists :
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
</form>
<?php
if ( isset( $_GET["interval"] ) ) // <====================
$temp = $_GET["interval"];
?>
index.php
<html>
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
<input type="submit" name="SEND"> <!-- ADD THIS LINE -->
</form>
</html>
installs.php:
<?php
$temp = $_GET["interval"];
echo $temp;
?>
This work fine in my webserver
try $_REQUEST['interval']; instead
In this code in php, don´t show the values of radio buttons, echo don´t work
<html>
<head>
<title>Probando radio</title>
</head>
<body>
<form method="POST">
<input type="radio" name="sexo" value="m" checked>Mujer
<input type="radio" name="sexo" value="h">Hombre
</form>
<? php
$sexo = $_POST['sexo'];
// print ($sexo);
echo $sexo;
?>
</body>
</html>
specify the form action and add a submit button. upon submit, $_POST['sexo'] should contain your result (m or h)
This is not a recommended fix, but it will work:
Change:
<form method="POST">
To:
<form action="#" method="post">
NOTE: Technically method should be lower case. It's an XHTML requirement. You would be penalized by W3C mobileOK validation.
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.
I must have a problem somewhere with formatting my code but cant find where,
This is the form I place the search bar in:
<form>
<?php include_once("searchbar.php"); ?>
</form>
This is my search bar code:
<form method= "post" action = "search.php">
<input type="text" name="search_input" placeholder="Search Designer Sofas..." />
<input type="submit" value="Search" />
</form>
And this is the beginning of the search results page
<!DOCTYPE html>
<?php
//retrieves the specified element from the URL
$search_input = $_POST["search_input"];
include_once("connection_to_db.php");
//isset function checks if a variable is set
if(!isset($search_input))
{
print("Nothing to search !");
return;
}
//take the books with the specific name
$query_products = "SELECT * FROM XXX WHERE name LIKE '%".$search_input."%'";
$query_product_result = mysql_query($query_products)
or die(mysql_error());
$num_rows_products = mysql_num_rows($query_product_result);
?>
<html lang="en">
<head>
Now what happens is that when I click on the search button the address changes in the url to the search query but the page doesnt, does it mean that it is sending to it but not receiving anything?
It looks like you have nested <form> elements. This is how your form is rendering:
<form>
<form method= "post" action = "search.php">
<input type="text" name="search_input" placeholder="Search Designer Sofas..." />
<input type="submit" value="Search" />
</form>
</form>
The second form (the one with the action) will be ignored. Remove the <form> wrapper where you include searchbar.php.
You're also wide open to SQL injection attacks. Consider using mysqli prepared statements.
enter = sign for action
<form method="post" action="search.php">
<input type="text" name="search_input" placeholder="Search Designer Sofas..." />
<input type="submit" value="Search" />
</form>
and try to remove form tags from
<?php include_once("searchbar.php"); ?>