Carrying a Submitted Name Variable across multiple php pages? - php

On my first page: page1.php I have an input where you type your desired name which I then want to be carried over to more than one page, so far I can get it to page2.php but on page3.php the code fails here is my code
page1.php:
<form action="page2.php" method="post">
Name: <input type="text" name="username" />
<input type="submit" name="submit" value="Submit" />
\
page2.php: (after 5 seconds the page redirects to page3.php)
<?php
echo $_SESSION['username'] = $_POST['username'];
?>
<form action="page3.php" method="post">
<input type="hidden" name="username" />
page3.php:
<?php
echo $_SESSION['username'] = $_POST['username'];
?>
These lines work on page2.php but not here which is what I can't seem to fix

Instead of giving you a fish, I'll teach you to fish:
echo $_SESSION['username'] = $_POST['username'];
This statement is echoing the value ASSIGNED to $_SESSION['username']
= Assignment
== Comparison
=== Comparison (Identical)

On page3.php is not working because you pass no value. So:
Instead of:
<input type="hidden" name="username" />
Go with:
<input type="hidden" name="username" value="".$_SESSION['username']."">
Or:
<input type="hidden" name="username" value="".$_POST['username']."">
Now it passes the value and you should get the value on page3.php. One warning is that users can edit your value with DEV tools so I suggest to pass values differently.

Related

POST multiple session variables with form, output on subsequent pages

I've been through 20 or so different posts and I can't seem to get this right piecing together questions and answers for different aspects of what I'm trying to accomplish.
I have a form the user can fill out. I want that upon submitting the form, the choices made will be used as variables throughout the rest of the website.
My code:
<form action="page1" method="POST">
<input type="text" name="var_1">
<input type="text" name="var_2">
<input type="submit" value="Submit" class="submit" name="submit">
</form>
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['var_1'] = $_POST['var_1'];
$_SESSION['var_2'] = $_POST['var_2'];
}
?>
Next Page:
<?php
session_start();
$var_1 = $_SESSION['var_1'];
$var_2 = $_SESSION['var_2'];
?>
<?php echo $var_1';?>
<?php echo $var_2';?>
This results in blank echos and a repeated error for each variable at the top of the page:
Notice: Undefined index: var_1 in page1.php on line #
Obviously my sessions aren't making it the second page, but I don't know why or what I've done wrong. This has been pieced together from posts about adding multiple sessions, posting sessions, getting sessions.
Either Remove the action from your <form> and give the redirect to page1 in the PHP code because the PHP code below wont work and get redirected to the next page.As a result of which the session will not be set
<form action="" method="POST">
<input type="text" name="var_1">
<input type="text" name="var_2">
<input type="submit" value="Submit" class="submit" name="submit">
</form>
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['var_1'] = $_POST['var_1'];
$_SESSION['var_2'] = $_POST['var_2'];
//redirect
header("Location: page1.php");
}
?>
Or set the session in the next page without the code below
<form action="page1" method="POST">
<input type="text" name="var_1">
<input type="text" name="var_2">
<input type="submit" value="Submit" class="submit" name="submit">
</form>
page1.php
<?php
session_start();
$_SESSION['var_1'] = $_POST['var_1'];
$_SESSION['var_2'] = $_POST['var_2'];
$var_1 = $_SESSION['var_1'];
$var_2 = $_SESSION['var_2'];
?>
<?php echo $var_1;?>
<?php echo $var_2;?>

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

Form won't forward $score to next page

When a user takes a quiz, their score is captured in the value $grade, which displays fine on the quiz's home page. But if I change the form action to forward the user to a new page (grade.php), $grade loses its value.
How can I capture the value and display it on the next page? The values for PreviousURL and user_token display...
<form action="grade.php" method="post" id="quiz">
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token']; ?>" />
<input type="submit" value="Submit Quiz" />
</form>
So I tried this, but it doesn't work...
<input type="hidden" name="grade" value="<?php echo $_SESSION['grade'] ?>" />
on grade.php make sure to start the file with
session_start();
Based on your question, I assume that you did not start the session on grade.php. There is not much information about the file
If you don't start the session the values of $_SESSION get destroyed
You should not save form values in $_SESSION as form values are already in $_POST superglobal.
You can do it like
<form action="grade.php" method="post" id="quiz">
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="<?php echo isset($_POST['user_token']) ? $_POST['user_token'] : '' ; ?>" />
<input type="submit" value="Submit Quiz" />
</form>
So basically it test if there is a value in user_token field it will apply that value other wise it will set to '' (nothing). If you are not familiar with the ternary operator syntax please refer to http://php.net/manual/en/language.operators.comparison.php
Updated Part:
// Your form page goes like
<?php session_start();
print_r($_SESSION); // If there is a session your form should show the value of user_token
// Rest of the script + html
?>
// grade.php
<?php session_start();
// do a print_r to see if you are receving session variables by
print_r($_SESSION);

Retrieve $_POST for two pages

I need to save my submit form data like:Name for two pages...
For some reason the $_POST only saves data for the "action" page, but cannot be retrived after the action page.
Here's my code:
HTML (form):
<html>
<body> <form name="input" action="staff.php" method="post">
Username: <input type="text" name="Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the next page after submiting and it works... (staff.php)
<html>
<?php
session_start();
echo "You have choosen". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>
</html>
Ok and after age submiting Name and Age stop working... (staff2.php)
Here's the code:
<?php
session_start();
echo "You have choosen".
$_POST['Name']; //it does't show Name.. Please help!
$_POST['Age']; // it doesnt't show this either..
?>
Obviously, there is nothing wrong on the first page. So don't change anything.
The second page. The post works. Then add a hidden input to preserve it and carry it on the next one:
<?php
echo "You have chosen: ". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="hidden" name="Name" value="<?php echo $_POST['Name']; ?>" /> <!-- this one -->
<input type="submit" value="Submit">
</form>
On the third and final page. Properly concatenate the variables:
echo 'You have chosen: <br/>';
echo $_POST['Name'] . '<br/>'; // this should carry the hidden input you set on the last page
echo $_POST['Age'];
//^^ you forgot the echo
as you have a session running pass them as session variables.
$_SESSION['name'] = $_POST['name'];

Store values from multiple form in $_POST

I am trying to store values from multiple forms in the $_POST variable. The forms are on different pages and from what I can understand from the test I did, when I submit values from a form, they "overwrite" the values that where already in $_POST.
Here is an example of what I am trying to do :
Page 1 - form 1
<?php
session_start();
?>
<form method="post" action="page2.php">
First name : <input type="text" name="firstName" required ><br/><br/>
Last name : <input type="text" name="lastName" required ><br/><br/>
<input type="submit">
</form>
Page 2 - form 2
<?php
session_start();
?>
<form method="post" action="page3.php">
Age : <input type="text" name="age" required ><br/><br/>
City : <input type="text" name="city" required ><br/><br/>
<input type="submit">
</form>
Page 3 - results
<?php
session_start();
echo $_POST['firstName'].'<br/>';
echo $_POST['lastName'].'<br/>';
echo $_POST['age'].'<br/>';
echo $_POST['city'].'<br/>';
?>
The last page shows me only 'age' and 'city'. The values from the first form on page 1 are now undefined. Here is the an example of the result I get :
Notice: Undefined index: firstName on line 4
Notice: Undefined index: lastName on line 5
65
New York
in page2.php
put this
<input type="hidden" name="firstname" value="<?=$_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?=$_POST['lastname']?>">
You can also use session variables like this:
//Page 1
<?php
session_start();
$_SESSION['user'] = array();
$_SESSION['user']['firstName'] = $_POST['firstName'];
$_SESSION['user']['lastName'] = $_POST['lastName'];
?>
//Page 2
<?php
session_start();
$_SESSION['user']['age'] = $_POST['age'];
$_SESSION['user']['city'] = $_POST['city'];
?>
//Result
<?php
session_start();
echo $_SESSION['user']['firstName'];
echo $_SESSION['user']['lastName'];
echo $_SESSION['user']['city'];
echo $_SESSION['user']['age'];
?>
As I can see in the code above, you are trying to post the data to two different files.
<form method="post" action="page2.php">
This has action page as page2.php.
<form method="post" action="page3.php">
Here you are submitting data to page3.php
And you are trying to access
echo $_POST['firstName'].'<br/>';
echo $_POST['lastName'].'<br/>';
in page3.php which you have passed to page2.php. Definitely this won't work as you can access the values submitted through a form in the action page of the form only.

Categories