www.webdeveloper.com
Results 1 to 5 of 5

Thread: not executing php code when back is pressed

  1. #1
    Join Date
    Sep 2004
    Posts
    14

    not executing php code when back is pressed

    Hi everyone,

    I am working on a web page with php which populates 3 (multiple select) drop down lists from a database then uses javascript to "join" the lists, eg "select country" then "select city" then "select surburb" etc, so the lists have relational content.

    The only information which is of interest from this is the selection in the third list. When the user presses submit and then back, I would like the list selections to remain (there is other information in the form which already does remain, only the list selection does not.)

    The problem is that the php code re-fetches the information from the database which re-populates the lists thus changing the selected value(s). The positions in the list remain selected, however the content of the list has changed, therefore selecting the incorrect values.

    Is there a way to either not execute the php code, or store the selected values so that javascript can re-select the correct ones?

    Thanks.

  2. #2
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    You could store the values in session variables. To be performed by PHP, this would have to done in the page they submit to, and you would have to submit all 3 values (if you're not already) so that they could all be remembered.

    If you don't know anything about PHP session handling: http://ca3.php.net/manual/en/function.session-start.php
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  3. #3
    Join Date
    Sep 2004
    Posts
    14
    Thanks for that,

    I set up a session and was able to place values for the selected options into $_SESSION. I can access these variables within the php code however cannot access them within Javascript. I am now attempting to create hidden data fields in HTML using php, which I can do, however it seems that when I check the $_SESSION array to see if a value has been set (using "isset"), I get inconsistent behaviour, code:

    file1:

    PHP Code:
    /* create the hidden values so that javascript can reselect the lists */
    if (isset($_SESSION['project'])) {
        echo 
    "<INPUT TYPE=\"hidden\" NAME=\"h_project\" VALUE=\"".$_SESSION['project']."\">
        <script language=\"JavaScript\"><!--reselect_project(); --></script>"
    ;

    file2:
    PHP Code:
    /* save the drop down list variables in the session data */
    $_SESSION['project'] = $_GET['projects'];
    $_SESSION['libraries'] = get_libraries();
    $_SESSION['plates'] = get_plates_array(); 
    The code in file 2 will always be executed, and the session values will always be set after this code is executed. If I press "back", sometimes the reselect_project() method is called, other times it is not.

    I do not want to make this harder than it should be. All I want to do is access the session variables once they have been set.

    cheers

  4. #4
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    If all you want is for javascript to be able to access the $_SESSION variables, have PHP output a javascript array that contains them:
    Code:
    <script type="text/javascript">
    var sessionVariables=Array();
    <?php
    foreach($_SESSION as $k=>$v) {
      echo "sessionVariables['".$k."']='".$v."';\n";
    }
    ?>
    </script>
    Then you should be able to access them in javascript through the sessionVariables array.

    Of course, the problem may persist if the page is not reloaded upon pressing the back button. In that case you could probably include a meta refresh tag to force a reload every time the page is viewed, or consider storing the values in a cookie. Javascript should be able to see changes in the cookie whether the page is reloaded or not.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  5. #5
    Join Date
    Sep 2004
    Posts
    14
    thanks HaganeNoKokoro,

    I tried both ways. When outputting the session variables in php the page was not reloaded, and even a call to refresh did not reload it I ended up using a cookie, created when submit was pressed, and then checked when back was pressed and the reload called. This works well (except when the user disables cookies and javascript!) Anyhoo, incidentially Mozilla seems to reselect items in drop down lists according to value, not index, so my problem was not a problem at all when I used Mozilla, only IE.

    cheers, Dave

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
  •  
HTML5 Development Center



Recent Articles