Problems with sessions

Discussion in 'PHP' started by Gravereaper, Jan 17, 2009.

  1. #1
    I have extracted these codes from a complete script of mine, the string $stat will only be "Printed" after the $stat is set and the page is refreshed .. How can we recode the script so that the $stat gets displayed without a need to refresh the page.

    <?php
     
    $stat=$_SESSION['stat'];
    if ( ( $_POST["code"] == $rand ) && ( $_POST["message"] != "" ) ) {
    $_SESSION['stat'] ="Message Submitted";
     }
     
    elseif (( $_POST["message"] == "" ) && ( $stat != "" )) { $_SESSION['stat'] ="No message entered"; }
    elseif (( $_POST["code"] != $rand ) && ( $stat != "" )) { $_SESSION['stat'] ="Wrong verification code"; }
     
    echo $stat;
    ?>
    Code (markup):
    Again, a summary of the problem:
    1)Script sets a value for $stat after checking through an IF condition.
    2)$stat does not gets displayed because the string for $stat was set AFTER the page is loaded.
    3)$stat gets displayed after a page refresh because at that point $stat is set ( previous $stat value, not the new one )
     
    Gravereaper, Jan 17, 2009 IP
  2. PapaCarlo

    PapaCarlo Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First thing - check if your $rand works ..
    Second thing - you don't need to reload the page - PHP works as 1,2,3 .. 1 will add a value, 2 will print it out :)
     
    PapaCarlo, Jan 17, 2009 IP
  3. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It works, I have removed the other statements which were present within the IF condition box but they do get executed along with the setting up for the $stat string. $stat string does still not gets displayed however.
     
    Gravereaper, Jan 17, 2009 IP