PHP post method - php

my code for welcome.php is as beow:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
and the form :
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
It should give the output my name and email id when I fill the form with my name and emailid .
but it is giving me the below error running the php file
Welcome
( ! ) Notice: Undefined index: name in C:\wamp\www\PHP Learning\welcome.php on line 4
Call Stack
# Time Memory Function Location
1 0.0000 236440 {main}( ) ..\welcome.php:0
Your email address is:
( ! ) Notice: Undefined index: email in C:\wamp\www\PHP Learning\welcome.php on line 5
Call Stack
# Time Memory Function Location
1 0.0000 236440 {main}( ) ..\welcome.php:0
corresponding to that when I run html file which is supposed to give me the correct out, it only shows the
in the address bar I get this:
LH/PHP%20Learning/welcome_get.php?name=shailesh+kumar&email=shailblack%40gmail.com
Not Found///
The requested URL /PHP Learning/welcome_get.php was not found on this server.
Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80
Please suggest where i m missing to execute the post method.

You are accessing POST variables directly but your form is submitted with the GET method.
By the way, you shouldn't ever use globals in the way that you have done in your example.

change your php file to
<html>
<body>
Welcome <?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?><br>
Your email address is: <?php echo isset($_POST["email"]) ? $_POST["email"] : ''; ?>
</body>
</html>
When script is executed for the 1st time, the $_POST variable is empty array. Thats why you have got errors.
Then as others told you - change your method to post.

You method you use is get.
You must change that to post.
For more information, you can read this article.

you're using <form action="welcome_get.php" method="get"> to perform a GET request, but use $_POST['name'] to get that value.
so try the example below
welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
and the form:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>

Your script is requesting the URL welcome_get.php but you named your file welcome.php - you need to change one of them. Your code is currently searching for a file that doesn't exist.
If you want to post use method="POST" not method="get".
When you load the page your script looks for $_POST['name'] and $_POST['email']. What happens when you don't post any data? Well as you've seen you get errors.
Check that the variables exist before presenting them as you cannot guarantee their existence.
if(isset($_POST['email']) {
echo $_POST['email'];
}
This will check to see if $_POST['email'] exists. If it does, it will echo its value, if it doesn't then it will show nothing.

Change
<form action="welcome_get.php" method="get">
to
<form action="welcome_get.php" method="post">
as you are posting the values..Read more here
and again, you dont have a file named welcome_get.php (i understood from the error you got). It should be welcome.php. so replace ypur <form> tag as follows
<form action="welcome.php" method="post">

Related

Get variable from Post Data (PHP)

I really don't know why my code isn't working! Let me clear it by example..
I got a file named 'index.html' example...
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="test">
<input type="submit">
</form>
</body>
</html>
And of course the form action file test.php .. example..
<?php
$something = ($_POST["test"]);
// from here I have some PHP codes and this "something" variable will be process in this codes and will print out something spacial..
?>
Now example If I post "Hello, It's not working" then the output will show a spacial design.
But Instead process, it's just printing out whatever I submit in that form.
But when I manually add the variable to "something" and if I execute the "test.php" . Example..
$something = "Hello, It's not working";
Then it works perfectly..
And yes. Also tried GET method.. It's still same as POST.
This is my first question here..
Thanks for any help and suggestions!
First Convert, index.html to php and follow this code:-
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" value="" name="test">
<input name="submit" value="submit" type="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$something = $_POST["test"];
}
?>
That's correct _POST:
<?php
//NO ($_POST["test"])
$something = $_POST["test"];
?>

PHP file in WAMP isn't accepting any value from html's GET or POST methods

<!DOCTYPE HTML>
<html>
<body>
<form action="testrun.php" method="GET">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
and testrun.php is
<html>
<body>
Welcome: <?php echo $_GET["name"]; ?><br><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
and this is the output i'm getting after typing 'xyz' in Name and 'xyz#abc' in Email text box:
Welcome
Your email address is:
NOTE: i have enabled 'rewrite_module' in Apache>Apache Modules>
I'm struck with this and donno where the problem might be. Can anyone help me out?
1) You have to change the method of getting the mail field like this:
Your email address is: <?php echo $_GET["email"]; ?>
Because you are using the GET method for your form.
2) Maybe your WAMP server is not running PHP. Put this in your body to make sure it's working:
<?php echo "PHP is working !" ?>
3) Try starting WAMP as administrator, or restart WAMP (or even your computer, just in case)
Your method is a GET
<form action="testrun.php" method="GET">
and trying to return a POST
Change to:
<?php echo $_GET["email"]; ?>
Try declaring the variables name and email on your testrun.php file i.e
<?php $name = $_GET["name"]; $email = $_GET["email"]; .....*rest of the code* ? >
Also, decide whether you are going to use POST or GET. From your form, you are using method get. Use the same in your action file and avoid mixing the two different methods.
Try to give a name and value to the input submit button.
<input type="submit" value="submit" name="submit">
Use a get method for email like $_GET['email']

printing fields is not working while running html form

I am trying to run a simple PHP-HTML code , that prints the entered text-fields
ex.html
<html>
<body>
<form action="exx.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
exx.php
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
When i run the ex.html , and enter random text and click submit
The output is :
Welcome Your email address is:
I wonder why ?
Are you seeing the results displayed in the URL?
www.site.com/exx.php?name=john&email=jdoe#somthing.com
EDIT:
Give this a shot.
<html>
<body>
<?php if($_GET){
echo 'Welcome ' . $_GET['name'] . '</br>';
echo 'Your eamil address is: ' . $_GET['email'];
} else {?>
<form action="" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
<?php
}
?>
</body>
</html>
ALthough slightly different, I just test this and it works.
NOTE: If you are going to ever put this on a live server and or ever have users touching it, make sure you use proper sanitation. If you are building a login script, please use $_POST instead, along with sanitation, and PDO.
Hope this helps.
If you see the GET variables defined in the URL, but they still aren't displaying, then $_GET must be undefined.
If you're using a version of PHP < 4.1.0, the $_GET superglobal would be undefined here. In that case, you would use $HTTP_GET_VARS instead:
exx.php
<html>
<body>
Welcome <?php echo $HTTP_GET_VARS["name"]; ?><br>
Your email address is: <?php echo $HTTP_GET_VARS["email"]; ?>
</body>
</html>
To see what version of PHP you're running, you can use the phpinfo() function for PHP >= 4.0, or you can use the command php --version at the command line in most unix-like operating systems (probably Windows too).
This ought to work for all recent versions of PHP:
<body>
<?php
$name = isset($_GET['name']) ? $_GET['name'] : 'empty';
$email = isset($_GET['email']) ? $_GET['email'] : 'empty';
?>
Welcome <?php echo $name ?><br>
Your email address is: <?php echo $email ?>
As an added bonus, you can use only one echo:
<body>
<?php
$name = isset($_GET['name']) ? $_GET['name'] : 'empty';
$name = isset($_GET['email']) ? $_GET['email'] : 'empty';
echo "Welcome $name <br>Your email address is: $email"
?>
Am sorry guys , turned out to be that am not running the files through localhost
, and instead from the hard-drive it self.
that was the problem.

How to get value to work in php and html tag?

The first file (which is html) has two inputs and the second file (php) is the form to display the data when submitted. I want the php file to print whatever message is typed into the input and submitted.
html file:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome_get.php file:
<html>
<body>
Welcome <?php echo $_GET["name"];?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
I know it is probably very simple but for some reason, whenever I type something into the input and submit, it doesn't display the values I inputted. It just displays only the message in the php file:
Welcome
Your email address is:
Use isset to check the variables exist before you use them. i.e.
<html>
<body>
<?php
if(isset($_GET['name'])){
echo 'Welcome '. $_GET["name"] .'<br>';
}
if(isset($_GET['email'])){
echo 'Your email address is: '. $_GET["email"] .'<br>';
}
?>
</body>
</html>
additionally, it's worth ensuring you have all errors displayed by putting this at the top of your file:
<?php
display_errors(E_ALL);
ini_set('display_errors', 'on');
?>
finally, check you're php install is working correctly, by creating a new file (phpinfo.php) with the following contents:
<?php
phpinfo();
?>
open this in the browser, and check you get the about php page.

Pass form data to another page with php

I have a form on my homepage and when it is submitted, it takes users to another page on my site. I want to pass the form data entered to the next page, with something like:
<?php echo $email; ?>
Where $email is the email address the user entered into the form. How exactly do I accomplish this?
The best way to accomplish that is to use POST which is a method of Hypertext Transfer Protocol https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
index.php
<html>
<body>
<form action="site2.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>
site2.php
<html>
<body>
Hello <?php echo $_POST["name"]; ?>!<br>
Your mail is <?php echo $_POST["mail"]; ?>.
</body>
</html>
output
Hello "name" !
Your email is "whatyou#addedonindex.com" .

Categories