SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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>
    i am making this code short.please tell me why values are not getting passed to next page?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You never set your session variables in the first script.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    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              "";
         


    Isn't it always true?

    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.

  4. #4
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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.

  6. #6
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'];

    }
    second page:

    echo $_SESSION['myname'];
    please help me.
    or
    <div class="row">
    <label for="name">MyName/label>
    <input type="text" name="myname" size="25" value="<?=$_SESSION['myname'];?>" readonly />
    </div>
    not working

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    And you have session_start(); on the first line in both pages?

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i do have session on the both pages. still not getting.

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    On the first line? Hmm, I don't know.
    Can you post the complete code of both pages?

  10. #10
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>


    addfriend_preview.php
    <?
    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>

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    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.
    At the top of your scripts - when dealing with incoming form values, whether GET or POST, var_dump() them before you run any conditions over them.
    PHP Code:
    <?php

    var_dump
    ($_POST) ;
    ...that way you can see what PHP has gathered up, and you are not left guessing what it ought to have.

    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

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $_SERVER['REQUEST_METHOD'] == "POST"
    I never use that to check if a form is submitted.

    do var_dump( $_SERVER ) too ...

  13. #13
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Never mind... made a mistake...
    Last edited by guido2004; Apr 21, 2009 at 06:00. Reason: made a dumb answer

  15. #15
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  16. #16
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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 all

  17. #17
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by deepson2 View Post
    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?
    I thought you commented out the if, always executing the redirect. But you commented out the whole block of code, including the redirect. So my answer made no sense

  18. #18
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  19. #19
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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
    addfriend_preview.php
    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>

  20. #20
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •