I have been searching for a few hours now on a way to fix this but I have pulled together my php knowledge to get this very small script to not work. I need to make a form that gets submitted which lands you one page one, then on to page two where that information also needs to be displayed.
Form "name" ---> Page one echo name ----> Page Two echo name
I figured I could store this into a session but this doesn't seem to be working.
Here is what I have so far,
form.php
<html>
<body>
<form name="input" action="info.php" method="get">
<input type="text" name="name"/> <br/><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
info.php
<?php
session_start();
$_SESSION['name'] = $_POST['name0'];
$name0 = $_POST['name'];
?>
<html>
<body>
<?php echo $_GET['name']; ?>
Next
</body>
</html>
next.php
<?php
session_start();
$_SESSION['name'] = $_POST['name0'];
?>
<html>
<body>
<?php echo $_GET['name']; ?>
</body>
</html>
I am sure this code is very sloppy but it is all i know. Sorry if the answer is already out there I have looked but maybe I am searching wrong.
info.php
<?php
session_start();
$_SESSION['name'] = $_GET['name'];
?>
<html>
<body>
<?php echo $_SESSION['name']; ?>
Next
</body>
</html>
next.php
<?php
session_start();
?>
<html>
<body>
<?php echo $_SESSION['name']; ?>
</body>
</html>
Try this changes and check if it is working as expected.
form.php Here you use GET:
<form name="input" action="info.php" method="get">
info.php Here you assigned the session variable to a post value (im not even sure where name0 came from):
$_SESSION['name'] = $_POST['name0'];
$name0 = $_POST['name'];
next.php Here you assigned the session variable to the same post value (inexistant):
$_SESSION['name'] = $_POST['name0'];
Change your code to this:
info.php
<?php
session_start();
$_SESSION['name'] = $_GET['name'];
$name0 = $_POST['name'];
?>
<html>
<body>
<?php echo $_SESSION['name']; ?>
Next
</body>
</html>
next.php
<?php
session_start();
?>
<html>
<body>
<?php echo $_SESSION['name']; ?>
</body>
</html>
Related
I have not worked with PHP for a long time. Recently, I have been assigned to a PHP project. I am in a like memory refreshing state now.
My problem is I have two PHP files.
test1.php
test2.php
test1.php
<html>
<body>
<form action="test2.php" method="post">
<?php
$description = "desc";
echo "<button name='.$description.' value='1'>description!</button>"
?>
</form>
</body>
</html>
test2.php
<?php
$answer = $_POST[$description]; //Undefined variable '$description'
echo $answer;
$sqlQuery = "SELECT * FROM StatsData WHERE code_name=$answer";
?>
Output
Warning: Undefined variable $description
Warning: Undefined array key
The problem is, I cannot pass the variable which is in the name tag to test2.php. How can I achieve my result?
I think your approach is not the best one, I would rather define a hidden field in test1.php, which hold the name of button, then in test2.php get it and display the button value
<html>
<body>
<form action="test2.php" method="post">
<input type="hidden" name="btnName" value="<?php echo $description; ?>"/>
<?php
$description = "desc";
echo "<button name='.$description.' value='1'>description!</button>"
?>
</form>
</body>
</html>
Then in test2.php
<?php
$answer = $_POST[$_POST["btnName"]];
echo $answer;
?>
I thought of adding a variable to the name tag and send it to another page and output the value. But, I am wrong. It was all about the value tag
test1.php
<html>
<body>
<form action="test2.php" method="post">
<?php
$description = "1";
echo "<button name='desc' value='$description'>description!</button>"
?>
</form>
</body>
</html>
test2.php
<?php
$answer = $_POST["desc"];
echo $answer;
$sqlQuery = "SELECT * FROM StatsData WHERE code_name=$answer";
?>
With the help of this community, I found out what was my mistake. I have confused between name tag and value tag. That was the reason.
Don't know were is the mistake lies in the code.
// Code in Page1
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test1</title>
</head>
<body>
<FORM METHOD = "POST" action = "Page2.php">
<input type="text" name = "user">
<input type="submit" name = "submit" value = "Submit">
</FORM>
<?php
if (isset($_POST["submit"])){
$_SESSION['user'] = $_POST['user'];
}
?>
</body>
</html>
// Code in Page2
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test2</title>
</head>
<body>
<input type="text" name = "field3" value = <?php echo htmlspecialchars($_SESSION['user']); ?>>
</body>
</html>
I expect the input field-named as "field3" in Page2 to be filled with whatever value provided in field-named as "name" of Page1.
You are posting data to page2 when submitting the form
<FORM METHOD = "POST" action = "Page2.php">
The code which is inside the if statement never executed if (isset($_POST["submit"])){
You have to place if statement on Page2 at the top after session_start to make it work
if (isset($_POST["Submit"])){
$_SESSION['user'] = $_POST['user'];
}
The action page is page2 so you will never have a post action on the page 1 where you have html form. In case of you have POST action that leads to page 1 you still have issues on page2.
and On page2, change it as
value =" <?php echo htmlspecialchars($_SESSION['user']); ?>">
You are missing "
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
I have used a form to send two variables from one page to another. Then I wanted the user to be able to click on a link (staying on the website) then once on the third page the variables to still be able to be used.
First page (which works fine):
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="result.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
<br />
</body>
</html>
Then the second page (which displays the variables correctly):
<html>
<body>
Welcome <?php echo $_POST['name']; ?><br>
Your email address is: <?php echo $_POST['email']; ?>
link
</body>
</html>
Then when you click on the link from the second to the third (below) it doesn't work:
<html>
<head>
<title> Form </title>
</head>
<body>
<?php echo $_POST['name']; ?>
</body>
</html>
Thanks in advance for any help!
You have to put $_POST['name'] and $_POST['email'] in session variables.
One way to do that is:
link
On name.php you will have them as $_GET variables
if you just want to use these values on 3rd page only then better to use the above GET method. Otherwise you can put these values in session
Your second (result.php) page should look like this:
<?php
$_SESSION['name']= $_POST['name'];
$_SESSION['email']= $_POST['email'];
?>
<html>
<body>
Welcome <?php echo $_POST['name']; ?><br>
Your email address is: <?php echo $_POST['email']; ?>
link
</body>
</html>
And you third page (name.php) will work when you put it like this:
<html>
<head>
<title> Form </title>
</head>
<body>
<?php echo $_SESSION['name']; ?>
</body>
</html>
Creating form and trying to carry variables accross two pages to a results page. I have tried $GET and $POST and it works fine from page1.php to results.php, but when I change to $SESSION the variable isn't passed or echo'd on results.php. Here's the php code for page1 which won't even work directly to results! Not sure if there is a problem with my code or possibly the server?
Page 1.php:
<?php session_start();?>
<?php
$name = $_SESSION['name'];
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?php session_start();?>
<html>
<body>
<?php
$name = $_SESSION['name'];
echo $name; ?>
</body>
</html>
Please try executing following code snippet
<?php session_start();?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$name = $_POST['name'];
$_SESSION['name']=$name;
}
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Actually in your code snippet value for $_SESSION['name'] was not set .so I have defined value for session variable with posted value from HTML form
You forgot the closing tag in you form, and you don't have a submit button
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
<input type="Submit" value="Submit">
</FORM>
Then in results.php
<?php session_start();?>
<html>
<body>
<?php
$_SESSION['name']=$_POST['name'];
echo $_SESSION['name']; ?>
<br><br>
page 2
</body>
</html>
Then i created this page2.php
<?php session_start();?>
<html>
<body>
Hi, I am still <?php echo $_SESSION['name'];?>
</body>
</html>
Everything is fine in my side.
I think you inverted your variables at this line :
<?php
$name = $_SESSION['name'];
?>
You probably wanted to do :
<?php
$_SESSION['name'] = $name;
?>
first of all you assign the name variable to session variable
<?php session_start();?>
<?php
$_SESSION['name'] = $name;
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?
session_start();
$name = isset($_POST['name'])?$_POST['name']:'';
if($name){
$_SESSION['name']=$name;
}
?>
<html>
<body>
<?php echo $name; ?>
</body>
</html>
Try....
Page 1.php : <?php session_start();
$_SESSION['name'] = isset($_POST['name']) ? $_POST['name'] : '';
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?php session_start();?>
<html>
<body>
<?php
$name = $_SESSION['name'];
echo $name; ?>
</body>
</html>