This question already has answers here:
When submitting a GET form, the query string is removed from the action URL
(13 answers)
Closed 4 years ago.
I have a php file, in it the code is bellow:
<?php
if (isset($_GET['action']) && $_GET['action'] == 'view') {
echo "get";
print_r($_GET);
} else {
}
?>
<html>
<form method="get" action="<?php echo ($_SERVER['PHP_SELF'] . '?action=view&id=4') ; ?>">
<input type="text" />
<input type="submit" />
</form>
</html>
If I click the submit, why the echo "post"; and print_r($_POST); do not output to the screen?
EDIT01
I changed my code to bellow:
<?php
if (isset($_GET['action']) && $_GET['action'] == 'view') {
echo "if ";
var_dump($_GET);
} else {
echo "else ";
var_dump($_GET) ;
}
?>
<html>
<form method="get" action="?action=view&id=4">
<input type="text" name="username" />
<input type="submit" />
</form>
</html>
however, it do not go through the if, it go else.
the screen output is like this:
Take a look at this code $_GET['action']. This code will seek the tag with name=action. Since there is no tag with name=action, your code will enter else instead of if.
To solve this, fix your input submit tag into this.
<input type="submit" name="action" />
Related
I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question
Im trying to validate a single textbox on the same page using PHP but am unable to get the errors to show. When i click submit and there is nothing in the textbox it works by not letting it go to the next page but it does not show the errors. Here is my code.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['IGN'] == ""){
$errors="Please enter a IGN";
}
if (isset($errors)){
echo $errors;
}
}
?>
<form method="post" action="Queston1.php">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>
I have tried it with and without the if (isset($errors)) to see if that was the problem but both times i get the same result.
Can anyone see or know how to fix this?
You should use jQuery or Javascript to validate before submitting the form.
But instead of
if($_SERVER['REQUEST_METHOD'] == 'POST'){
Try
$errors = '';
if(isset($_POST['IGN'])){
if($_POST['IGN'] == ''){
$errors .= 'Please Enter a IGN';
}
}
if($errors!=''){
echo $errors;
}
Try something like this:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['IGN'] == ""){
echo "Please enter a IGN";
}
else {
header('Location: question.php');
}
}
?>
<form method="post" action="">
<label for="IGN" class="questionText">IGN (In Game Name)</label><br />
<input type="text" name="IGN" /><br />
<input type="submit" name="start" value="start" />
</form>
I'm trying to redirect the user to another webpage depending on which radio button they have checked.
Here is the relevant code:
<form action="sample.php" method="post">
<input name="survey" type="radio" value="Yes" /> Yes
</br>
<input name="survey" type="radio" value="No" /> No
</form>
<?
if ($_POST['survey'] == "Yes")
{
header('Location: http://localhost/survey.php');
}
else if ($_POST['survey'] == "No")
{
header('Location: http://localhost/survey.php');
}
?>
For some reason or another I get an error within my if statement. That does not recognize 'survey' as a valid index. How Am I failing to do something to link my form to the php code?
Your warning is caused by the fact that when you load the page using GET (a normal request), $_POST['survey'] is not set.
You could change your conditions by adding a isset($_POST['survey'] ) && in front of every time you use it or you could put the whole code in a block that checks if a post was made like:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if ($_POST['survey'] == "Yes")
{
header('Location: http://localhost/survey.php');
}
else if ($_POST['survey'] == "No")
{
header('Location: http://localhost/survey.php');
}
}
else
{
// output html
}
Either way you would have to put this in front of your html as you cannot use header if the headers have already been sent (stuff has already been outputted to the browser).
Think about how forms work:
The first time you visit your page, the form is not submitted. Yet, your if/else is acting as though it were. That's what's causing the error - $_POST['survey'] doesn't exist the first time.
Write your scripts properly - do all potential form processing before rendering HTML:
<?php
if (isset($_POST['submit'])) {
// handle the form
}
?>
<!doctype html>
<html>
<head>
<title>Blah</title>
</head>
<body>
<!-- code -->
</body>
</html>
That will allow you to check if you've submitted the form to itself, and potentially use a header() to redirect the user without running into those pesky "Headers already sent" errors.
Try a simple print_r() statement to see if $_POST has any contents at all. Put this at the top of the page:
print_r($_POST);
Also, be sure that you're loading the results page via the form. If you just type the URL of the page it will not have any POST data sent with it.
The first time you load your file sample.php there is no POST data, therefore there's no index 'survey'.
You need to nest it in another if statement or modify it the following:
<form action="sample.php" method="post">
<input name="survey" type="radio" value="Yes" /> Yes
</br>
<input name="survey" type="radio" value="No" /> No
</form>
<?
if (isset($_POST) && $_POST['survey'] == "Yes")
{
header('Location: http://localhost/survey.php');
}
else if (isset($_POST) && $_POST['survey'] == "No")
{
header('Location: http://localhost/survey.php');
}
?>
I am using a different php file for the checking.
<html>
<body>
<form action="another.php" method="POST">
<label>You are: </label> Male <input type="radio" name="male"> Female <input type="radio" name="female"><br/><br/>
<input type="submit" name="submit" value="GO">
</body>
</html>
*****another.php******
<?php
if (isset($_POST['submit']))
{
if(empty($_POST['male']) && empty($_POST['female']))
{echo "select gender";}
elseif (empty($_POST['female'])) //means Male is checked
{
$gend="Male";
echo $gend;
}
elseif(empty($_POST['male'])) //Means Female is checked
{
$gend="Female";
echo $gend;
}
elseif(empty($_POST['male']) || empty($_POST['female'])== false) //Not required if you can disable one when another is checked
{echo"please select only one";}
}
?>
I try to login with one PHP file, without any HTML files. So when I'm successfully logged in I want to hide the HTML post button and the textboxes.
Here's my code..:
<?php
$showHTML = true;
if ($showHTML) { ?>
<h1> Bitte einloggen! </h1>
<form action="test2.php" method="POST">
<input name="user" type="test"><br>
<input name="pass" type="password"><br>
<input type="submit" value"Login">
</form>
<?php
}
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( $_POST['user'] == "test" && $_POST['pass'] == "a123" ) {
echo "user = test";
$showHTML = false;
}
}
Of course that doesn't hide the HTML code again, because its already executed I think.
Is there any way to hide the HTML output again?
Of course that doens´t hide the HTMLCode again, because its already executed I think.
Correct
Is there any way to hide the HTML output again?
Move the test so it appears before the HTML you (don't) want to output.
Short: put the HTML under the PHP.
Long: Use a decent login system. Use sessions.
You can't use a server side variable to control the already rendered html content.
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( $_POST['user'] == "test" && $_POST['pass'] == "a123" ) {
echo "user = test";
}
} else { ?>
<h1> Bitte einloggen! </h1>
<form action="test2.php" method="POST">
<input name="user" type="test"><br>
<input name="pass" type="password"><br>
<input type="submit" value"Login">
</form>
<?php } ?>
On basis of the information that the user fills in my form, I want to execute some PHP code. So after the form is submitted, it should have control on the same page and thereafter it should execute the PHP code (and not before pressing submit button). I have used <input type="hidden" value=1 name="hid"/>. When the user clicks the submit button, the value is changed to 0. But its not working. so solution please..
Is this similar to what you are looking for ?
<?php
if (!isset($_POST["submit"]) ) {
if ($_POST["hid"] == 0 ) {
echo "hid is not 0. display form.";
}
?>
<html>
<head>
<script type="text/javascript">
function check_valid() {
document.getElementById("hid").value = 0;
}
</script>
</head>
<body>
<form method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return check_valid();" >
<input type="hidden" id="hid" name="hid" value="1" />
<input type="submit" value="submit" name="submit"/>
<!-- form elements go here -->
</form>
</body>
</html>
<?php
} else {
echo "hid is now 0, execute the php code";
}
?>
EDIT: added <input type="hidden" name="hid" value="1" /> for clarity. Thanks to andre_roesti for the suggestion