PHP code doesn't print any value - php

Here i entered name nd email id but when i click on submit button my entered information is blank like below image.
In PHP clicking on the submit button doesn't print the information. I used $GET and $POST, both are not working.
<!DOCTYPE HTML>
<html>
<body>
<form action="php_forms.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

It should be work.Change your action (php_forms.php) file code.I have tried in my server.Hope it will be helped.
<html>
<body>
Welcome <?php echo htmlspecialchars($_POST["name"]); ?><br>
Your email address is: <?php echo htmlspecialchars($_POST["email"]); ?>
</body>
</html>
Read More dealing with html forms in PHP.NET.

Your form uses get method and php_forms.php uses post method. Try
<form action="php_forms.php" method="post">

Related

Reading data using post

Consider following html page:
index.html
<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>
If GET is used for reading the data let's say "name" then following is understandable:
welcome.php
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
When POST is meant for writing the data How is it possible to read the "name" using POST? Why should the following work?
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
First of all add a name in your submit input <input name="submit" type="submit">
Second into welcome.php add if(isset($_GET["submit"])){ $submit = preg_replace('#[^a-z0-9]#i', '', $_GET['submit']); }
and then use Welcome <?php echo $submit; ?>
i suggest to use preg_replace if you want to pass values from page to page

Why is my PHP form not posting data correctly?

I've just started using PHP forms for my website prototype, and it wasn't working so I decided to try to make a basic form example. No fancy CSS, just pure data. This is the code.
Index.html
<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>
welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>
<br />
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
The code works as far as displaying the 'Welcome ___ Your email address is: ____.
It just doesn't display the data entered in the form. For clarification as to whether my PHP works, I have tried just displaying some text and it worked fine. I use the PHP Editor phpDesigner8.
Thanks!

Values input in html form not taken by php

I tried to execute the following html code:
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="listing.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>
And this is the code of the associated php file (listing.php):
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php echo $_POST["user"]; ?><br>
Your email address is: <?php echo $_POST["address"]; ?>
</body>
</html>
I was able to get the form and enter values as shown below:
Form Input
But, when I clicked 'Send Message', it displays only:
Welcome
Your email address is:
Without the values that I entered through the form.
When I tried to run the php file directly from the local host (http://localhost/listing.php), I received these error messages:
Welcome
Notice: Undefined index: user in C:\xampp\htdocs\listing.php on line 7
Your email address is:
Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8
I even modified the php code as follows, but still got the same output:
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php
if(isset($_POST['submit'];)) {
session_start();
$user = $_POST['user'];
echo "$text";}else {echo 'Could not load text!';}?><br>
Your email address is: <?php echo $_POST["address"]; ?>
</body>
</html>
I would really appreciate it if you could give some advice to make it work.
Thanks
if(isset($_POST['submit'];)) should be changed,so you check if address and user is isset. Furthermore you normally don't want ; in your if statements.
Here i have a optimized version for you. the !empty is added so we also check if the inputs are not empty.
if (isset($_POST["name"] && isset($_POST["address"])) {
if (!empty($_POST["name"] && !empty($_POST["address"]) {
// execute code as u would
}
}
Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8
This is caused by the use of the ';' in the if condition.
Remove that and you should be good. for now.
Try this code:
<html>
<head>
<title>Listing Reading input from a form </title>
</head>
<body>
Welcome <?php
if(isset($_POST['submit']))
{
session_start();
$user = $_POST['user'];
echo $user;
echo "<br><br>Your Email Address is: ".$_POST['address'];
}
else
{
echo 'Could not load text!';
}
?>
<br>
<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send" name="submit"></p>
</form>
</body>
</html>
In your second code block $text isn't defined anywhere. Do you mean to have $user?
<html>
<head>
<title>Listing Reading input from a form</title>
</head>
<body>
Welcome
<?php
if(isset($_POST['submit'])) {
session_start();
echo isset($_POST['user']) ? $_POST['user'] : "User";
} else {
echo "User";
}
?>
<br>
Your email address is:
<?php
echo isset($_POST["address"]) ? $_POST["address"] : "Address"
?>
</body>
</html>
Try this and let me know:
<?php
session_start();
if(isset($_POST['submit'])) {
$user = $_POST['user'];
echo "Welcome ".$user;
echo "<br>";
echo "Your email address is: ".$_POST["address"];
}else {
// you may get rid of this block if you like
echo 'Could not load text!';
}
?>
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" name='submit' value="send"></p>
</form>
</body>
</html>

php code wont work and display data

This code is from w3schools which displays the email address and name once the user submits the form. However when I tried copying the code it wouldn't let me display the same output and would only say Cannot POST /welcome.php or it would display the welcome.php code whenever i submit.
I didn't even alter it in anyway but why wont it work? D:
index.html
<!DOCTYPE HTML>
<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>
welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

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