[RESOLVED] problems updating session variables
Results 1 to 5 of 5

Thread: [RESOLVED] problems updating session variables

  1. #1
    Member
    Join Date
    Oct 2007
    Posts
    41

    resolved [RESOLVED] problems updating session variables

    Hi,

    I am having problems updating session variables when using the back button.

    page1.php sets the first set of session variables as expected, I then use header to go to page2.php, where more session variables are set. Once page2.php has been submitted, I use header to go to success.php, which prints the session variables set in page1.php and page2.php. However, if I decide to go back to page2.php and make changes, then go back to page1.php then forward to page2.php, the session variables are not updated. They are only updated if I press the submit button on page2.php. I was wondering how to solve this problem. I do acknowledge that the session variables are doing what they are supposed to be doing, but I just need them to update.

    here is the code for page1.php:

    Code:
    <?php
    session_start();
    $_SESSION['clientname'] = (isset($_POST['name']) ? $_POST['name'] : (isset($_SESSION['clientname']) ? $_SESSION['clientname'] : '')); //name
    $_SESSION['clientcompany'] = (isset($_POST['company']) ? $_POST['company'] : (isset($_SESSION['clientcompany']) ? $_SESSION['clientcompany'] : '')); //company
    
    $N = '';
    $C = '';
    
    if(isset($_POST['submit']))
      {
      
        
      //process the radio buttons
      
    
    	
    
      //process the radio buttons
      if(isset($_POST['name']))
        $N = $_POST['name'];
      else
        $N = false;
    
    if(isset($_POST['submit']))
      
      //process the text-input field
      if(isset($_POST['company']))
        $C = $_POST['company'];
      else
        $C = false;
    	
    
      //echo '<p>Other Choice<br />';
      
        
      //if all entries okay, process information, display message and exit script.
      if($N && $C)
        {
        
    header("Location: page2.php");
    
        }
      }
    ?>
    
    
    <html>
    
    <head>
    <title>Page 1</title>
    <head>
    
    
    
    
    
    
    
    </head>
    
    <body>
    
    
    
    <?php
    
    echo '<h2>Page 1</h2>';
    
    echo '<form name="Form1" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
      
    //display text-input label for name
    if(isset($_POST['submit']) && !$N)
      echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
    else
      echo '<p>Please enter Name [required]<br />';
      
    //display the text-input field
    echo '<input type="text" name="name" maxlength="60" size="60"
                   value="' . $_SESSION['clientname'] . '" /></p>';
    
    
    if(isset($_POST['submit']) && !$C)
      echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
    else
      echo '<p>Please enter Company [required]<br />';
      
    //display the text-input label for company
    echo '<input type="text" name="company" maxlength="60" size="60"
                   value="' . $_SESSION['clientcompany'] . '" /></p>';
    
    
    
    echo '<p><input type="submit" name="submit" value="submit"></p>';
    echo '</form>';
    
    echo '</body>';
    
    echo '</html>';
    ?>
    here is the code for page2.php

    Code:
    <?php
    session_start();
    
    $_SESSION['otherchoice'] = (isset($_POST['choice']) ? $_POST['choice'] : (isset($_SESSION['otherchoice']) ? $_SESSION['otherchoice'] : '')); //radio
    $_SESSION['textchoice'] = (isset($_POST['txt']) ? $_POST['txt'] : (isset($_SESSION['textchoice']) ? $_SESSION['textchoice'] : '')); //text
    
    
    
    
    
    //$_SESSION['otherchoice'] = $_POST['choice']; //radio
    //$_SESSION['textchoice'] = $_POST['txt']; //text
    $OC = '';
    
    if(isset($_POST['submit']))
      {
      	
    
      //process the radio buttons
      if(isset($_POST['choice']))
        $OC = $_POST['choice'];
      else
        $OC = false;
    	
    
        
      if($OC == 'yes' && ($_SESSION['textchoice'] == ''))
      $OC = false;
    else
    $OC = $_POST['choice'];
    
    
    
      //echo '<p>Other Choice<br />';
      
        
      //if all entries okay, process information, display message and exit script.
      if($OC)
        {
        
    header("Location: success.php");
    
        }
      }
    ?>
    
    
    <html>
    
    <head>
    <title>Page 2</title>
    <head>
    
    
    
    
    
    
    
    <script type="text/javascript"> 
    function make_blank()
    {
    
    document.theform.txt.value ="";
    }
    </script>
    
    <script type="text/javascript"> 
    function make_blank2()
    {
    
    document.theform.txt2.value ="";
    }
    </script>
    
    </head>
    
    <body>
    
    
    
    <?php
    
    echo '<h2>Page 2</h2>';
    
    echo '<form name="theform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
      
    
    if(isset($_POST['submit']) && !$OC)
      echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
    else
      echo '<p>What colour would you like? [required]<br />';
     
    
    //if($OC == 'red')
    if ($_SESSION['otherchoice'] == 'red')
    echo '<input type="radio"  name="choice"  value="red" checked onClick="make_blank();this.form.txt.disabled=true;"/>red &nbsp;<br />';
    else
    echo '<input type="radio"  name="choice" value="red" onClick="make_blank();this.form.txt.disabled=true;"/>red &nbsp;<br />';
    
    //if($OC == 'green')
    if ($_SESSION['otherchoice'] == 'green')
    echo '<input type="radio"  name="choice"  value="green" checked onClick="make_blank();this.form.txt.disabled=true;"/>green &nbsp;<br />';
    else
    echo '<input type="radio"  name="choice" value="green" onClick="make_blank();this.form.txt.disabled=true;"/>green &nbsp;<br />';
    
    //if($OC == 'orange')
    if ($_SESSION['otherchoice'] == 'orange')
    {
    echo '<input type="radio"  name="choice"  value="orange" checked onClick="make_blank();this.form.txt.disabled=true;"/>orange &nbsp;<br />';
    
    }else{
    echo '<input type="radio"  name="choice" value="orange" onClick="make_blank();this.form.txt.disabled=true;"/>orange &nbsp;<br />';
    }
    
    
    
    //if($OC == 'other')
    if ($_SESSION['otherchoice'] == 'other')
    echo '<input type="radio" name="choice"  value="other" onClick="this.form.txt.disabled=false;this.form.txt.focus()"checked />Other...Please specify &nbsp;';
    	else
    echo '<input type="radio" name="choice"   value="other" onClick="this.form.txt.disabled=false;this.form.txt.focus();"/>Other...Please specify &nbsp;';
    
    
    
    echo '<input disabled type="text" name="txt" value="' . $_SESSION['textchoice'] .'" />';
    
      
    echo '<br /><input type="submit" name="submit" value="submit">';
    echo '<input type="button" value="back" onclick="history.back(-1)" />';
    
    echo '</form>';
    
    echo '</body>';
    
    echo '</html>';
    ?>
    here is the code for success.php :

    Code:
    <?php
    session_start();
    echo 'What colour would you like? : ';
    
    	if ($_SESSION['otherchoice'] == "other"){
    
    	echo "other";
    	echo "<br>";
    	echo 'Other information is : ';
    	echo $_SESSION['textchoice'];
    	echo "<br>";}
    	else {
    
    	echo $_SESSION['otherchoice'];
    	echo "<br>";}
    
            echo "<br>";	
            echo 'Client Name : ';
            echo  $_SESSION['clientname'];
            echo "<br>";
            echo 'Client Company : ';
            echo  $_SESSION['clientcompany'];
            echo "<br>";
        
    ?>
    Any help would be greatly apprecitaed, Thanks!

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,729
    then forward to page2.php
    So you're expecting moving between cached pages in your browser to update a session file on the server?

    http://cdn1.bigcommerce.com/server39....1280.1280.jpg
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Member
    Join Date
    Oct 2007
    Posts
    41
    Hi dalecosp,

    I think so, how can you do this?

    Thanks

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    What dalecosp is getting at is that you can not process inputs from a form that was not submitted.

  5. #5
    Member
    Join Date
    Oct 2007
    Posts
    41
    Hi Traq,

    OK, I have submitted the form as you suggested and it seems to be working, session variables are now updating. Many thanks to you and dalecosp for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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