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.
Related
I am just starting to learn php and when I run my php file, no matter the browser, i get a 404 error. What is my code missing that is causing this? This file is supposed to display a message after a user submits a 'contact me' form.
<!DOCTYPE html>
<html lang="">
<head>
<title>Contact Form Submitted</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
Thank you, <?php echo $_POST["name"]; ?> for your comments<br>
You stated in your test Message:<br>
<?php echo $_POST["message"]; ?>
We will respond to you at <?php echo $_POST["email"]; ?> when we make updates to the site.
</p>
</body>
</html>
this code works. see this is firefox: https://i.imgur.com/YeBFt9B.png
<?php
// var // variable value // default value
$php_path = $_SERVER['PHP_SELF'] ?? "";
$name = $_POST["name"] ?? "name";
$message = $_POST["message"] ?? "message";
$email = $_POST["email"] ?? "email";
?>
<!DOCTYPE html>
<html lang="">
<head>
<title>Contact Form Submitted</title>
</head>
<body>
<form action="<?= $php_path ?>" method="POST">
<p>
Thank you, <?= $name ?> for your comments<br>
You stated in your test Message:<br>
<?= $message ?>
<br><br>
We will respond to you at <?= $email ?> when we make updates to the site.
</p>
</form>
</body>
</html>
if you don't specify a default value on your parameters from another form you could run into an unexpected error. This version is better ;)
I'm sure you get this error because you have a typo in one of your urls
First of all use htmlspecialchars in:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
you prevent XSS attack if user enter url like: http://www.example.com/test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E
If you are getting 404 error it's probably becouse your action of the form on contact me page don't point to right script.
If you have two separate files, one for form and second for thanks message you have to make sure than the urls are correct.
// form.php
...
<form action="thanks-message.php">
...
</form>
...
// thanks-message.php
<?php
$name = $_POST["name"] ?? "Joe";
$message = $_POST["message"] ?? "Lorem ipsum...";
$email = $_POST["email"] ?? "joe#example.com";
?>
...
<p>
Thank you, <?= $name ?> for your comments<br>
You stated in your test Message:<br>
<?= $message ?>
<br><br>
We will respond to you at <?= $email ?> when we make updates to the site.
</p>
...
form element is not necessary on this page
<!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']
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">
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.
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" .