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>
Related
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
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 the following form on "test.php".
<?php
session_start();
if(isset($_POST['ph']))
if(isset($_POST['submit']))
$_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
The "order.php" looks like this:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
echo ($_SESSION['ph']);
?>
The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.
I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.
Change the new value to SESSION ON your order.php page like below:-
<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
$_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
echo ($_SESSION['ph']);
}
?>
Also change test.php code like this:-
<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
I have the following code in two files separately
file one.php
<HTML>
<BODY>
<FORM ACTION="two.php" METHOD="POST">
Age: <INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="OK">
</FORM>
</BODY>
</HTML>
file dos.php
<HTML>
<BODY>
<?PHP
print ("The age is: $age");
?>
</BODY>
</HTML>
the age variable is not recognized, someone knows fix?
It's not recognized because you don't create it. Variables don't magically appear in PHP1. You need to get that value from the $_POST superglobal:
<HTML>
<BODY>
<?PHP
$age = $_POST['age'];
print ("The age is: $age");
?>
</BODY>
</HTML>
1 Anymore. They used to when register_globals existed. But that has been obsolete since long before you started coding.
Your trying to access the value of age from a page( dos.php) but you posting it to (two.php) and your missing $_POST['age'].
one.php
<HTML>
<BODY>
<FORM ACTION="two.php" METHOD="POST">
Age: <INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="OK">
</FORM>
</BODY>
</HTML>
two.php
<HTML>
<BODY>
<?PHP
$age = $_POST['age'];
print ("The age is: $age");
?>
</BODY>
</HTML>
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>