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.
Related
I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
The website generates the random number from 1 to 100 when accessing the first page(page1.php). And the user will guess the number.
The first page contains
- a text box for accepting a
number from the user, and a submit button.
The second page(page2.php) will be returned to the user if the guess number is too high or too low. And the page shows a message telling the user "Too High" or "Too Low". The page also contains
a button(retry button) that allows the user to go back to the first page(page1.php) to re-enter a new number
a button that allows the user to quit the game.
The third page(page3.php) is returned to the user if the guess is correct. The page displays "Correct", the random number, and the count of tries.
And I have this index.php which is heart for all the pages. And here is the code.
index.php
<?php
$name = '';
$inputnumber = '';
$random = 33; //this is just an assumption to keep it simple
$message = '';
$guesscount = '';
if (isset($_POST['action'])) {
$action = $_POST['action'];
}
if ($action === 'guess') {
$guesscount = $_POST['$guesscount'];
$inputnumber = $_POST['$inputnumber'];
if ($inputnumber == $random) {
$message = "Correct!";
include 'page3.php';
}
if ($inputnumber > $random) {
$message = "Too High";
include 'page2.php';
}
if ($inputnumber < $random) {
$message = "Too Low";
include 'page2.php';
}
}
if ($action === 'retry') {
include 'page1.php';
}
page1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Number Guess: <input type="text" name="$inputnumber" value="<?php if(isset($inputnumber)==1){
echo $inputnumber;}else echo ""; ?>" /><br>
<input type="submit" name="action" value="guess" />
<hr>
Guess Count: <?php echo $guesscount; ?>
</form>
</body>
</html>
page2.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
<input type="hidden" name="$guesscount" value="<?php echo $guesscount;?>"/><br>
<input type="submit" name="action" value="retry" />
<hr>
Guess Count: <?php echo $guesscount;?>
</form>
</body>
</html>
page3.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
Number of Tries: <?php echo $guesscount; ?>
<input type="submit" name="action" value="ok" />
</form>
</body>
</html>
page1.php is the page to load first.
Challenge I have faced is, I couldn't keep the $guesscount stable always. It keeps resetting on me. I have tried session but couldn't resolve it.Please help resolving it.
Thanks in advance.
I don't know why but my gut feeling tells me that the reason why the session is not working for you on other pages is because you do not initiate it ??
So what you have to do is:
index.php
<?php
session_start();
$_SESSION['myVariable'] = 'myVariable';
?>
page1.php
<?php
session_start();
$mySessionVar = $_SESSION['myVariable'];
var_dump($mySessionVar); // <- this should print myVariable
?>
You may get an error saying that $_SESSION is null or not set and to prevent that you can just enclose $_SESSION inside and isset method
if(isset($_SESSION['myVariable']) && $_SESSION['myVariable'] != null) {
$mySessionVar = $_SESSION['myVariable'[;
}
Though I enter data and hit Submit it always echoes the else part always. I know it isn't the type of question to be asked on Stackoverflow but...
<html>
<head>
<title>Sticky Form</title>
</head>
<body>
<form method="POST" action=<?php echo $_SERVER['PHP_SELF'] ?>>
<label for="Name">Name</label>
<input type="text" name="FName">
<input type="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['FName'];
echo "$f_name";
}
else
{
echo "Not set!";
}
?>
</body>
</html>
Change this:
<input type="submit">
to
<input type="submit" name="submit">
P.S: key name in global arrays comes from users input ($POST,$_GET,$_COOKIE), if you want to change its key, you need to change that element's name!
I am currently learning the most basic PHP ever. I have 5 files.
index.php:
<html>
<head>
<title>Budget Calcule</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Put in your: - </h2>
<form action="functions.php" method="post">
<h3>Income</h3>
<label>Salary: <input name="salary" type="text" /></label><br />
<h3>Outgoings</h3>
<label>Living: <input name="living" type="text" /></label><br />
<label>Insurance: <input name="insurance" type="text" /></label><br />
<label>Communication: <input name="communication" type="text" /></label><br />
<label>Loan: <input name="loan" type="text" /></label><br />
<label>Food & Drink: <input name="foodAndDrink" type="text" /></label><br />
<label>Entertaintment / Shopping: <input name="entertainmentOrShopping" type="text" /></label><br />
<label>Transport: <input name="transport" type="text" /></label><br />
<label>Other: <input name="other" type="text" /></label><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
this is my functions.php:
<?php
include('variables.php');
if(!($_POST['Submit'])){
if(isset($_POST['salary'])){
header('Location: output.php');
return $_POST['lon'];
}else{
echo "All fields are required";
}
}
?>
this is my variables.php:
<?php
$salary= $_POST['salary'];
$living= $_POST['living'];
$insurance= $_POST['insurance'];
$communication = $_POST['communication'];
$loan = $_POST['loan'];
$food = $_POST['food'];
$entertaintmentOrShopping = $_POST['entertaintmentOrShopping'];
$transport = $_POST['transport'];
$other= $_POST['other'];
?>
this is my output.php file:
<?php
include('outputFunction.php');
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
and last but not least, this is my outputFunction.php file:
<?php
include('variables.php');
function myText(){
echo "Your salary per month is: " . $_POST['salary'];
}
?>
Now you're thinking "why have he split up his code in different files?" Well first of all, I split the variables from functions.php because I wanted outputFunctions.php to get the variables from variables.php so i could echo my `$_POST['salary']; . The function myText(); outputs the text just fine, but it doesnt output the $_POST['salary'];.
I do not know why it doesnt work, I just wonder if you could be my extra eyes and see if I've done some mistake.
PS! Don't down vote my question just because you think it's stupid. I am having problem with this issue and been working on it for hours without advancing anywhere.
A few things:
You don't need to include a variables.php file. The variables you're accessing are global and you're just creating duplicates that aren't being used. They also go away after the page changes since you're re-declaring them each page load.
You are also trying to call a variable that doesn't exist when you reference $_POST['lon'] instead of 'loan'.
And finally to actually answer your question:
Your myText() function is referencing a variable that is not there anymore.
You need to merge functions.php and outputFunction.php and output.php into one file so the variables aren't lost and all the processing is done without opening a new file each time. I can see your original concept for separated files but an output file is going to be the file to process the input data from the form.
Now in your newly merged output.php, you should have something resembling this:
<html>
<head>
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
echo "Your salary per month is: " . $_POST['salary'];
}
} else {
echo "All fields required.";
}
?>
</body>
</html>
This means only two files - your form page and this page.
A few more tips:
If you want to check if the form was submitted, it has look something like this:
if(isset($_POST['Submit'])){ ... }
Also, you should add a name="" attribute to your submit-Button:
<input type="submit" name="Submit" value="Submit" />
And what is the variables.php for? You don't use any of those variables.
When you redirect the user via header() the data that is stored in the $_POST array gets lost.
You could directly redirect to ouput.php
<form action="output.php" method="post">
And do something like this:
<?php
include('outputFunction.php');
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
<?php
} else {
echo "All field required";
}
}
?>
By the way you can always check what your $_POST contains with print_r($_POST);
This can be very useful for debugging.
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>