Form always submits as GET even with POST method - php

I am having a problem with my form submission. The code is below. When I submit, it is submitting as GET and not as POST, even though my method is post. I have tried method="POST" and method="post". What am I missing here?
<!doctype html>
<html>
<head>
<title>Test</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<?php
echo "POST: ";
print_r($_POST);
echo " GET :";
print_r($_GET);
?>
<form role="form" name='test' method=“post” action="test2.php">
<label for="name">Name</label>
<input name="name" type="text" />
<input type="submit" name="submit" />
</form>
</div>
</body>

Related

Why HTML form isn't sending any data

I am trying to get data with php, so here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" required><br>
<input type="submit" value="Submit" name="submit"><br>
</form>
<?php
print_r($_POST);
if(isset($_POST['submit']))
{
print_r($_FILES);
}
?>
</body>
</html>
It always prints an empty array, meaning $_POST is always empty. Could you please help me
Note: it does receive a data if you submit only the file (name is empty)

A problem with processing data from html form with PHP

I'm just about to learn php and that is my first try to process data from an html form. I was writing this php code using some articles from the Internet and it seems to be correct but after entering all the data and than pressing "submit" nothing is happening. I mean just a white browser screen.
Here is my code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
<form method="post" action="form__2.php">
<label for="surname">Surname</label>
<input type="text" name="surname" placeholder="Johnson" required> <br>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Carl" required> <br>
<label>Gender:</label>
<label for="male">M</label>
<input type="radio" name="male" checked>
<label for="female">F</label>
<input type="radio" name="female"> <br>
<label for="education">Education:</label>
<select id="education" name="education" required>
<option value="less-basic">Less than basic</option>
<option value="basic">Basic</option>
<option value="mid-school">Middle-school</option>
<option value="high-school">High-school</option>
<option value="bachelor">Bachelor's or equivalent</option>
<option value="master">Master's or equivalent</option>
<option value="doctor">Doctorate or equivalent</option>
</select> <br>
<label for="courses">Enroll in courses</label>
<input type="checkbox" name="courses" required> <br>
<label>Was you dealing with our company later?:</label>
<label>Yes</label>
<input type="radio" name="company" value="yes" checked>
<label>No</label>
<input type="radio" name="company" value="no"> <br>
<input type="submit" name="submit">
</form>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
<?
if ( count($_GET) > 0 ) // checking if the form sends any data
{
$name = htmlspecialchars($_GET['name']); // getting name from GET-values
$surname = htmlspecialchars($_GET['surname']); // getting surname from GET-values
if ( strlen($_GET['name']) >= 1 && strlen($_GET['surname']) >= 1) // if the name/surname has more than 1 letter, then..
{
echo "Dear " . $surname . " " . $name . ". We are great to have you signed for our courses. We are hoping for "; // msg
if ( $_POST['company_yes']=='yes' ) // if radiobutton is checked
{
echo "continuing our partnership."; // msg
} else
{
echo "our future partnership."; // msg
}
} else
{
echo "Your name is too short";
}
}
?>
</body>
</html>
You didn't check if a Post happened, and why call to GET request if you send a post request?
A GET request is data that passes through the URL -> https://www.something.net?data=hi
$_GET['data'] will give "hi" value.
POST is data that is passed mostly through forms with method="POST"
To get the data of the form change the form__2.php to the following:
<?php
if(isset($_POST['submit']){ //the name of the submit button
echo $_POST['education'];
echo $_POST['female'];
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
</body>
</html>

PHP isset function is not working to print data

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form method="POST">
<input type="text" name="text">
<input type="button" value="Check" name="submit">
</form>
<p>
<?php
if (isset($_POST['submit'])){
$a=$_POST['text'];
echo "You have written" . $a;
}
?></p>
</body>
</html>
I am using PHP isset function here in this html form, but don't know why it is not echoing anything. It should print the text written inside the input box. Please help me why this is not working?
To submit the form, you need to use submit type for button, not button:
<input type="submit" value="Check" name="submit">
Tested, changing the type resolves your issue.

500 Error When Attempting to Echo Form data in PHP

I am getting an error page from the server when trying to view my test.php page. Everything on the page loads fine if I remove the php tags.
I am getting an "http error 500" with the following code:
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<?php
echo "<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value='".date()."'>
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>";
?>
</body>
</html>
Or even strip out most of the echo/print
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value="<?php print(date()); ?>">
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>
</body>
</html>
```
<?php
echo '<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value="'.date().'">
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>';
?>
```
this should work.
Use the following:-
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<?php
echo "<form>
<input type=\"hidden\" name=\"uid\" value=\"Anonymous\">
<input type=\"hidden\" name=\"date\" value='".date()."'>
<textarea name=\"Message\"></textarea><br>
<button type=\"submit\" name=\"submit\">Comment</button>
</form>";
?>
</body>
</html>

Form not using the file in action attribute

I have a very simple HTML form that is supposed to send information to the file written in action attribute via GET but somehow it's transfering the information back to index.php:
index.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form acton="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>
process_form.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>
The bizarre aspect is that when I submit the form, the URL shows that it is not even using process_form.php:
http://127.0.0.1/Learning/?username=test&email=x%40test.com&submit_btn=Submit
If I manually change the URL to include process_form.php it seems to be working fine and I get the results I am looking for:
http://127.0.0.1/Learning/process_form.php?username=test&email=x%40test.com&submit_btn=Submit
On my development computer, I'm running EasyPHP 14.1 local WAMP server and thought it might be the root of the problem so I uploaded the files to my website that is running newest PHP on Apache, but the problem still exists there.
What am I doing wrong?
you have a typo error in action; you have given acton. Should be like this:
<form action="process_form.php" method="get">
First thing - you have a typo:
<form action="process_form.php" method="get">
^
The second thing - in my opinion the best method of handling forms is using POST method, not GET, so I would change it to:
<form action="process_form.php" method="post">
and in process_form.php I would use $_POST instead of $_GET
After digging around your question,
Index.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form action="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>
process_form.php
<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>
Note: If you will not specify form method, By default it will take GET method. So please make sure action should be perfect.
Above code just copy and paste, it should work perfect.
Ask me for further clarification.
Thanks,
Gauttam

Categories