Saving selection in drop-down lists in PHP after page reload?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dshan@web.de

    Saving selection in drop-down lists in PHP after page reload?

    Hi all,

    I can't find an answer to my quesiton anywhere.

    Given a drop-down list with USA states and other text fields I pass
    this information for further processing via PHP. If any of the required

    fields is empty, the PHP script would return an error and reload the
    form page asking to fill out those fields. All entered information is
    saved... except for the drop-down list selection. I tried to write a
    Javascript function which is echoed by PHP, but interpolation of PHP
    variables in the Javascript section gives a syntax error message in
    Javascript.

    The PHP script returns, e.g., an "AL" string for Alabama. Of course, I
    don't want to write PHP code for each option to make it selected.

    Would appreciate any reply to my question.

    Thanks!

    Dshan

  • Geoff Berrow

    #2
    Re: Saving selection in drop-down lists in PHP after page reload?

    Message-ID: <1150760275.219 250.264370@u72g 2000cwu.googleg roups.com> from
    dshan@web.de contained the following:
    [color=blue]
    >The PHP script returns, e.g., an "AL" string for Alabama. Of course, I
    >don't want to write PHP code for each option to make it selected.[/color]


    The way I do it requires some setup, but you only do it once,
    First you create an array of states and codes

    e.g.

    $states= array("AL"=>"Al abama","TX"=>"T exas","VA", "Virginia",...) ;

    then use PHP to create your select box

    Untested.

    <select name='state'>
    <?php
    foreach($states as $key=>$value){
    if(isset($_POST[$key]) &&$_POST[$key]==$value){
    $selected="sele cted";
    }
    else{
    $selected="";
    }
    echo"<option value='$key' $selected >$value</option>\n";

    }
    ?>


    </select>


    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • dshan

      #3
      Re: Saving selection in drop-down lists in PHP after page reload?

      Thank you, Geoff,

      You did it! I made some changes to your code, it works fine!

      Geoff Berrow wrote:[color=blue]
      > Message-ID: <1150760275.219 250.264370@u72g 2000cwu.googleg roups.com>[/color]

      Comment

      • Geoff Berrow

        #4
        Re: Saving selection in drop-down lists in PHP after page reload?

        Message-ID: <1150775120.459 960.323490@i40g 2000cwc.googleg roups.com> from
        dshan contained the following:
        [color=blue]
        >Thank you, Geoff,
        >
        >You did it! I made some changes to your code, it works fine![/color]

        Did I make a mistake?
        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        Working...