Values input in html form not taken by php - 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>

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

How to pass a Variable through a session

I know there are other posts about this, but I still can't seem to see where the code is incorrect.
I'm trying to pass a variable from one page to another via session: Below are the two pages; excuse some of the variable names as I was just plotting them in quickly.
main.php
<?php
session_start();
session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
result.php
<?php
session_start();
echo $_SESSION['itemId'];
session_destroy();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Previously I didn't have the unset in there. However, it hasn't made a difference.
The value is getting stored in the session, its just not passing it through to the other page.
Main page should looks like this:
<?php
session_start();
//remove session_unset();
if(isset($_POST['submit']))
{
$_SESSION['itemId'] = $_POST['firstName'];
echo "name = " . $_SESSION['itemId'];
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="result.php" method="POST">
First name:
<input type="text" name="firstName" placeholder="First Name">
<br>
<input type="submit" name="submit">
</form>
remove session_unset(); line after start_session()
and why r u destroying session in result page.
if you want to destroy then
store session value in variable
$id = $_SESSION['itemId'];
echo $id;
this code will help u

Im trying to use the $_GET method to obtain whatever the user has typed in a form and then using it in an if statement. It has come up with an error

this is the code for my html page in the site:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<html>
<body>
<form action="Untitled-4.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
</body>
</html>
and this is my code for the "Untitled-4.php" file:
<html>
<body>
<?php
if ($_GET["name"] == "tom"){
echo "hello you are special";
}
?>
</body>
</html>
this is the error i am getting:
"undefined index on line 7 of the php file"
I am trying to use the GET method to take whatever the user types in the "name" box to be used in the if statement that echoes "you are special" if the user types in "tom". can anyone tell me what the problem here is.
(the question stack overflow is saying is a possible duplicate is a completely different question)
Check if the variable is set first:
<html>
<body>
<?php
if (isset($_GET["name"])) {
if ($_GET["name"] == "tom"){
echo "hello you are special";}
} else {
echo "you are not special";
}
?>
</body>
</html>
for some reason i just tried it again with no changes to my code and it appears to be working i did not actually have to do anything.

PHP code doesn't print any value

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">

PHP and form handling

I have been working with PHP and MySQL for about two months and I recently began using forms. I have a main form-processing file:
if($_POST['checkforpothole'] == "yes"){
echo "thank you for your help"."<br/>";
echo "please enter in a latitude and longitude"."<br/>";
include 'FormForGettingLatAndLong.php';
$Lat = $_POST['latitude'];
$Long = $_POST['longitude'];
include 'MySQLSample.php';
echo addCoord($Lat, $Long);
include 'MySQLSample.php';
die();
}
if($_POST["checkforpothole"] == "no"){
echo "here is the table"."<br/>";
include 'MySQLSample.php';
die();
}
?>
The form I am attempting to access is as follows:
<?php
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="FormProcessing.php" method="post">
Latitude: <input type="text" name="latitude"><br>
Longitude: <input type = "text" name = "longitude"><br>
<input type="submit">
</form>
</body>
</html>
Here is the checkforpothole form:
<?php
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="FormProcessing.php" method="post">
Add a new pothole? (yes or no): <input type="text" name="checkforpothole"><br>
<input type="submit">
</form>
</body>
</html>
I am having trouble accessing latitude and longitude in this form, which is in a separate file. Is there a way to access an index from a second form after accessing from another form initially. Please let me know if there is a problem in the first program, specifically with the first if statement.
First of all, I think the logic of your code needs a bit of revision.
if($_POST['checkforpothole'] == "yes"){
echo "thank you for your help"."<br/>";
echo "please enter in a latitude and longitude"."<br/>";
include 'FormForGettingLatAndLong.php';
$Lat = $_POST['latitude'];
$Long = $_POST['longitude'];
include 'MySQLSample.php';
echo addCoord($Lat, $Long);
include 'MySQLSample.php';
die();
}
if($_POST["checkforpothole"] == "no"){
echo "here is the table"."<br/>";
include 'MySQLSample.php';
die();
}
?>
As i concluded from your code it appears that you want to get additional information using a second form when user submitted the first form. and you're trying to do so by including the second form when you are processing the first form and then immediately reading the values of second form:
include 'FormForGettingLatAndLong.php';
$Lat = $_POST['latitude'];
$Long = $_POST['longitude'];
but this way, you won't have the Longitude and Latitude! because you just posted the second form to user to get that data! and when she posts back the second form, execution of your code will start from beginning (in a separate process). working with forms is not like conventional input procedures (like old "cin<<" , "fscanf()" or "System.console().readLine()"). instead you should print the second form and wait for user to submit that.
so you can do what you want using these three files:
index.html:
<!DOCTYPE HTML>
<html>
<body>
<form action="firstFormProcessing.php" method="post">
Add a new pothole? (yes or no): <input type="text" name="checkforpothole"><br>
<input type="submit">
</form>
</body>
</html>
firstFormProcessing.php:
<?php
if($_POST['checkforpothole'] == "yes"){
echo "thank you for your help"."<br/>";
echo "please enter in a latitude and longitude"."<br/>";
echo <<<END
<!DOCTYPE HTML>
<html>
<body>
<form action="secondFormProcessing.php" method="post">
Latitude: <input type="text" name="latitude"><br>
Longitude: <input type = "text" name = "longitude"><br>
<input type="submit">
</form>
</body>
</html>
END;
die();
}
if($_POST["checkforpothole"] == "no"){
echo "here is the table"."<br/>";
include 'MySQLSample.php';
die();
}
?>
secondFormProcessing.php:
<?php
echo "now i have Latitude=$_POST[latitude] and Longitude=$_POST[longitude]";
?>
P.S. it's my first Answer on stackoverflow. i hope this will help you and by the way sorry for my bad English.
I would take 2 files instead of messing on one page, this is how I would do it.
If this helps you then don't forget to mark the question as answered.
index.php
<?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
if(isset($_POST['checkForPothole'])) {
if($_POST['checkForPothole'] == true) {
header("Location: addPole.php");
}
}
}
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="index.php" method="post">
Add a new pothole? (yes or no): <input type="text" name="checkForPothole"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
addPole.php
<?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
if(isset($_POST['latitude']) && isset($_POST['longitude'])) {
if(!empty($_POST['latitude']) && !empty($_POST['longitude']) {
require_once("MySQLSample.php");
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
addCoord($latitude, $longitude); //guessing that this is a function in the mysqlsample file
}
}
}
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="FormProcessing.php" method="post">
Latitude: <input type="text" name="latitude"><br>
Longitude: <input type="text" name="longitude"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Categories