HTML printing textbox values - php

I want users to fill out a signup like so:
<h1>Enlistment Form</h1>
<form name="input" action="http://spartancorps-gil.comyr.com/Enlistment.php" method="get">
<p>First Name?: </p>
<input type="text" name="FirstName" value="" />
<p>Last Name?: </p>
<input type="text" name="lastname" value=""/>
<input type="submit" value="Submit" />
</form>
It sends to a server with this php:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$Firstname = $_POST['FirstName'];
print ($Firstname);
$Lastname = $_POST['lastname'];
print ($Lastname);
</head>
</html>
For some reason it isn't printing out these values. What am I missing here?

Because you are using method="GET" in your form and in second file you are using $_POST for getting that values:
Do like this: Use method="POST" in you form:
Example:
<form name="input" action="http://spartancorps-gil.comyr.com/Enlistment.php" method="POST">
and use do like this:
echo $_POST['FirstName'];
echo $_POST['LastName'];
Read about "superglobals" in PHP.
http://php.net/manual/en/language.variables.superglobals.php

Please change your code to this and try...
Please change the method of form to post and close the php tag
<form name="input" action="http://spartancorps-gil.comyr.com/Enlistment.php" method="post">
<?PHP
$Firstname = $_POST['FirstName'];
print_r($Firstname);
$Lastname = $_POST['lastname'];
print_r($Lastname);
?>

You submitting the form with GET, but trying to read $_POST. So either change method to POST in your form, or use $_GET to read submitted values.
Make sure that url in action points to correct php file.
Close php tag after print ($Lastname);

try this one, just close your php tag.
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$Firstname = $_POST['FirstName'];
echo $Firstname;
$Lastname = $_POST['lastname'];
echo $Lastname;
?>
</head>
</html>

You have to change "print" to "echo" like this:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$Firstname = $_POST['FirstName'];
echo $Firstname;
$Lastname = $_POST['lastname'];
echo $Lastname;
</head>
</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

two forms on different pages connected to the same php

I have these three pieces of code which are supposed to be 2 different forms that send the data to the php. But when I am done with filling in the second form the data of the first form is gone. I can't use 2 different php files because I have to send the data to planyo using file_get_contents.
1st file, testform 1:
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform1">
<label for="naam">naam:</label>
<input type="text" name="naam"><br>
<input type="hidden" name="action" value="form1">
<input type="submit" value="form1">
</form>
</body>
</html>
2nd file, testform 2:
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform2">
<label for="achternaam">achternaam:</label>
<input type="text" name="achternaam"><br>
<input type="hidden" name="action" value="form2">
<input type="submit" value="form2">
</form>
</body>
</html>
test.php file:
<?php
$firstname = "";
$lastname = "";
switch($_POST['action']) {
case 'form1':
$firstname = $_POST['naam'];
echo "form 1 gelukt";
header("Location: http://localhost:8081/greenjoy/testform2.html");
break;
case 'form2':
$lastname = $_POST['achternaam'];
echo "form 2 gelukt";
echo "$firstname" . "$lastname";
break;
}
?>
Modify your files like this:
testform1.html: no change.
testform2.html, change it into a php file (testform2.phtml):
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform2">
<label for="achternaam">achternaam:</label>
<input type="text" name="achternaam"><br>
<input type="hidden" name="action" value="form2">
<input type="hidden" name="naam" value="<?php echo $_GET['firstname']; ?>">
<input type="submit" value="form2">
</form>
</body>
</html>
test.php:
<?php
$firstname = "";
$lastname = "";
switch($_POST['action']) {
case 'form1':
$firstname = $_POST['naam'];
echo "form 1 gelukt";
header("Location: http://localhost:8081/greenjoy/testform2.html?firstname=$firstname");
break;
case 'form2':
$firstname = $_POST['naam'];
$lastname = $_POST['achternaam'];
echo "form 2 gelukt";
echo "$firstname" . "$lastname";
break;
}
?>
This demonstrate the concept, but you should add code to verify if $_GET['firstname'] is set (and react if not).
The idea is to carry the firstname value with you when going to testform2.
Ensure your application flow does not allow a user go to testform2 before going to testform1. This could be done by: if $_GET['firstname'] is not set when calling testform2, redirect the user to testform1 automatically.
Remember that each call to test.php is indendant of the other one. This is why the value set when processing testform1 is not available in testform2.
Another method that could be used is to set the value from testform1 into a session or a database and retrieve it when accessing testform2. More complicated.
Ask yourself if exposing the parameter to testform2 is a security risk or not. If yes == use sessions or database! If not, you can use what I propose.
Another solution if you want to hide your $_GET values, you could encrypt the parameters.

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>

not understanding why this code is not working

im new to sessions but from what i see it complicated to apply <input> with them. can you please look at this code and tell me why its not working. i had it working earlier then it died on me. the function of the program is to fill out a form and have it verified for legit information using regular expressions, i just need help with getting the sessions to save the data.
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>regex</title>
</head>
<body>
<?php
$fname = $_REQUEST['fname'];
$fname = $_SESSION['fname'];
print<<<form
<form method="post" action="">
<input type ="text"
name="fname"
value="">
<input type ="submit">
</form>
form;
$_SESSION['fname'] = $fname;
print $_SESSION['fname'];
?>
</body>
</html>
You are reading $fname from $_REQUEST, then overwriting it with the value from $_SESSION, then putting it back to $_SESSION. So far, it should work as designed :) What are you trying to do? If you want to set the $_SESSION variable with the value received through $_REQUEST, leave out the second "$fname=" line.
Timothy,
Change your code so that it checks if the session/request is empty or not
Something like:
if(isset($_REQUEST['fname'])){
$fname = $_REQUEST['fname'];
}else if(isset($_SESSION['fname'])){
$fname = $_SESSION['fname'];
}
Try this:
if (!isset($_SESSION['fname'])) {
$_SESSION['fname'] = ''; // default value
}
if (isset($_POST['fname'])) {
$_SESSION['fname'] = $_POST['fname'];
}
print<<<form
…
form;
print $_SESSION['fname'];

Categories