SitePoint Sponsor |
|
User Tag List
Results 1 to 20 of 20
-
Apr 21, 2009, 03:26 #1
seesion value is not passing through pages
Hi..
very strenge issue. i am not getting session value across the pages. what i am doing is asking user to fill up a form and when he submits his/her form on the next page he/she can see the fields which are filled and when he submits on confirm data will go to the database. its so simple but i am not getting session values so everything is stucked. so please anyone here can tell me what i am missing here.?
1)addfriend.php
PHP Code:<?php
session_start();
ob_start();
if($_SERVER['REQUEST_METHOD'] == "GET")
{
// $id = $_POST['id'];
$myname = "";
$friend1 = "";
}
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// $id = $_POST['id'];
$myname = $_POST['myname'];
$friend1 = $_POST['friend1'];
}
if($_POST['submit'] == "submit"){
header("Location:addfriend_preview.php");
}
?>HTML Code:<html> <head> <body> <form action="<?=$PHP_SELF;?>" method="post" name="productFrm"> <div class="row"> <label for="name">MyName:</label> <input type="text" name="myname" size="25" value="" /> </div> <h4>My friends</h4> <div class="row"> <label for="friend1">1</label> <input type="text" name="friend1" size="25" /> </div><div class="row"> <span class="col6"><input type="submit" name="submit" value="submit" class="button_style" /></span> <span class="col4"><input type="reset" value="Reset" class="button_style" /></span> </div> <div class="row"> <span class="col6"><input type="submit" name="submit" value="submit" class="button_style" /></span> <span class="col4"><input type="reset" value="Reset" class="button_style" /></span> </div> </form> </div> </div>
addfreind_preview.php
PHP Code:session_start();
//ob_start();
echo $_SESSION['myname'];
echo $_SESSION['friend1'];
echo 'Welcome to page #2<br />';
if($_SERVER['REQUEST_METHOD'] == "GET"){
echo $_SESSION['myname'];
echo $_SESSION['friend1'];
}
else if($_SERVER['REQUEST_METHOD'] == "POST")
{
if($_POST['submit'] == "submit")
{
// $id = $_POST['id'];
$myname = $_SESSION['myname'];
$friend1 = $_SESSION['friend1'];
}
}
?>
HTML Code:<html> <head> <body> <link rel="stylesheet" href="friends.css" type="text/css" /> <div id="wrapper"> <div class ="header"> <img src="images/header.jpg" alt="Logo" /> </div> <div class="rightcol"> <form action="<?=$PHP_SELF;?>" method="post" name="productFrm"> <div class="row"> <label for="name">MyName:</label> <input type="text" name="myname" size="25" value="<?=$_SESSION['myname'];?>" readonly /> </div> <h4>My friends</h4> <div class="row"> <label for="friend1">1</label> <input type="text" name="friend1" value="<?=$_SESSION['friend1'];?>" size="25" /> </div><div class="row"> <span class="col4"><input type="submit" name="submit" value="submit" class="button_style" onClick="javascript:return fmvalidate();" /></span> <span class="col4"><input type="reset" value="Reset" class="button_style" /></span> </div> </form> </div> </div> </body> </html>
-
Apr 21, 2009, 03:41 #2
You never set your session variables in the first script.
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 03:42 #3
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
PHP Code:if($_SERVER['REQUEST_METHOD'] == "POST")
{
// $id = $_POST['id'];
$myname = "";
$friend1 = "";
}
It is in effect setting all your vars to ""; when any form is posted.
Using var_dump is the best way to go.
If you used var_dump( $POST ) before and after every condition then you'd spot this yourself.
-
Apr 21, 2009, 03:56 #4
guido2004, we have to set session on the both the pages right?
cups, i am sorry that method was GET. still not getting what is the use of var_dump here.could you please explain.
do you find anything wrong in this code?
-
Apr 21, 2009, 04:58 #5
Yes, but you never set the session variables. At least not in the piece of code you posted here.
You'll have to do $_SESSION['myname'] = $_POST['myname']; somewhere in the first script. Likewise for the other variables. Otherwise in the second script $_SESSION['myname'] doesn't exist.Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 05:16 #6
guido2004, still its not working. forget about the text area,simple echo is not working. what you thing why its happing?
first page:
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// $id = $_POST['id'];
$_SESSION['myname'] = $_POST['myname'];
$friend1 = $_POST['friend1'];
}
echo $_SESSION['myname'];
or
<div class="row">
<label for="name">MyName/label>
<input type="text" name="myname" size="25" value="<?=$_SESSION['myname'];?>" readonly />
</div>
-
Apr 21, 2009, 05:21 #7
And you have session_start(); on the first line in both pages?
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 05:31 #8
yes i do have session on the both pages. still not getting.
-
Apr 21, 2009, 05:42 #9
On the first line? Hmm, I don't know.
Can you post the complete code of both pages?Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 05:48 #10
addfriend.php:
<?php
session_start();
//ob_start();
if($_SERVER['REQUEST_METHOD'] == "GET")
{
// $id = $_POST['id'];
$myname = "";
$friend1 = "";
}
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// $id = $_POST['id'];
$_SESSION['myname'] = $_POST['myname'];
$friend1 = $_POST['friend1'];
}
//this is somthing i am trying.
/* if($_POST['submit'] == "submit"){
header("Location:addfriend_preview.php");
}*/
?>
<html>
<head>
<body>
<form action="addfriend_preview.php" method="post" name="productFrm">
<div class="row">
<label for="name">MyName/label>
<input type="text" name="myname" size="25" value="" />
</div>
<h4>My friends</h4>
<div class="row">
<label for="friend1">1</label>
<input type="text" name="friend1" size="25" />
</div><div class="row">
<span class="col6"><input type="submit" name="submit" value="submit" class="button_style" /></span>
<span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
</div>
</form>
</div>
</div>
<?
session_start();
ob_start();
//atleast this should work
echo $_SESSION['myname'];
echo $_SESSION['friend1'];
echo 'Welcome to page #2<br />';
if($_SERVER['REQUEST_METHOD'] == "GET"){
echo $_SESSION['myname'];
echo $_SESSION['friend1'];
}
else if($_SERVER['REQUEST_METHOD'] == "POST")
{
if($_POST['submit'] == "submit")
{
// $id = $_POST['id'];
$myname = $_SESSION['myname'];
$friend1 = $_SESSION['friend1'];
}
}
?>
<html>
<head>
<body>
<link rel="stylesheet" href="friends.css" type="text/css" />
<div id="wrapper">
<div class ="header">
<img src="images/header.jpg" alt="Logo" />
</div>
<div class="rightcol">
<form action="<?=$PHP_SELF;?>" method="post" name="productFrm">
<div class="row">
<label for="name">MyName/label>
<input type="text" name="myname" size="25" value="<?=$_SESSION['myname'];?>" readonly />
</div>
<h4>My friends</h4>
<div class="row">
<label for="friend1">1</label>
<input type="text" name="friend1" value="<?=$_SESSION['friend1'];?>" size="25" />
</div><div class="row">
<span class="col4"><input type="submit" name="submit" value="submit" class="button_style" onClick="javascript:return fmvalidate();" /></span>
<span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
</div>
</form>
</div>
</div>
</body>
</html>
-
Apr 21, 2009, 05:48 #11
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
still not getting what is the use of var_dump here.could you please explain.
PHP Code:<?php
var_dump($_POST) ;
This is especially true when checking a condition and forking your code, the whole empty, "", 0, null assessment is fraught with problems.
So, similarly use var_dump( $_SESSION ) before, during and after code forks to retain some sanity.Last edited by Cups; Apr 21, 2009 at 05:49. Reason: added quote
-
Apr 21, 2009, 05:52 #12
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
$_SERVER['REQUEST_METHOD'] == "POST"
do var_dump( $_SERVER ) too ...
-
Apr 21, 2009, 05:57 #13
thank you Cups, var_dump is very useful.i put it on the next page of where i am supposed to get session value and see what i got.
if i add myname as deepson2 in first page
i got
array(3) { ["myname"]=> string(5) "deepson2" ["friend1"]=> string(0) "" ["submit"]=> string(6) "submit" }
is that means session is working properly? i mean here value is getting passed than why its not coming in my code?
-
Apr 21, 2009, 05:58 #14
Never mind... made a mistake...
Last edited by guido2004; Apr 21, 2009 at 06:00. Reason: made a dumb answer
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 06:03 #15
guido2004, i am sorry i didnt get what you mean. and as i said i was just trying that code. could you please tell me again what did you actually mean with your last post?
-
Apr 21, 2009, 06:06 #16
Let's see, the form in addfriend.php sends its values to addfriend_preview.php. So setting the session variables in addfriend.php makes no sense.
I guess the var_dump you did is of the $_POST variable, right?
So in addfriend_preview.php the form values are in the $_POST variable. And the form in this page sends the values to this page itself, so they'll always be in $_POST.
My guess is you don't need session variables at allGuido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 06:08 #17Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 06:14 #18
guido2004,
I have lots of field to add in this form. and I am supposed to add value in my first page that is addfriend.php . and then i have to pass these value in next page that is addfriend_preview.php. so i think i need session values here. so user can see his/her values and make updation and when he/she finally click on confirm,it ll goes in to database. so you really dont think that i dont need session here ?
yaah, i am checking $_post's value in var_dump.
-
Apr 21, 2009, 07:43 #19
How about this?
addfriend.php
PHP Code:<html>
<head>
<body>
<form action="addfriend_preview.php" method="post" name="productFrm">
<div class="row">
<label for="name">MyName/label>
<input type="text" name="myname" size="25" value="" />
</div>
<h4>My friends</h4>
<div class="row">
<label for="friend1">1</label>
<input type="text" name="friend1" size="25" />
</div><div class="row">
<span class="col6"><input type="submit" name="submit" value="submit" class="button_style" /></span>
<span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
</div>
</form>
</div>
</div>
PHP Code:<?
$myname = '';
$friend1 = '';
if($_POST['submit'] == "submit")
{
$myname = $_POST['myname'];
$friend1 = $_POST['friend1'];
}
?>
<html>
<head>
<body>
<link rel="stylesheet" href="friends.css" type="text/css" />
<div id="wrapper">
<div class ="header">
<img src="images/header.jpg" alt="Logo" />
</div>
<div class="rightcol">
<form action="<?=$PHP_SELF;?>" method="post" name="productFrm">
<div class="row">
<label for="name">MyName/label>
<input type="text" name="myname" size="25" value="<?=$_POST['myname'];?>" readonly />
</div>
<h4>My friends</h4>
<div class="row">
<label for="friend1">1</label>
<input type="text" name="friend1" value="<?=$_POST['friend1'];?>" size="25" />
</div><div class="row">
<span class="col4"><input type="submit" name="submit" value="submit" class="button_style" onClick="javascript:return fmvalidate();" /></span>
<span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
</div>
</form>
</div>
</div>
</body>
</html>Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Apr 21, 2009, 21:30 #20
good morning guido2004,
you made my day! its working fine! thanks a lot! now for first page i ll do java script validation and for the next page will do php validation.
still have to work on session.because here on the first page we are not using php stuffs like passing id and all. so if i have to pass it i have to use session right? for now problem is solved.
thanks a lot!
Bookmarks