dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP: auto update of select fields

  1. #1
    Join Date
    May 2003
    Posts
    11

    Question PHP: auto update of select fields

    Does anyone know whether it is possible to update a select options field based on previous selections without refreshing the page??

    I know PHP is server-side script but i think i've seen an example before, any ideas??

    Is there a possibility??

  2. #2
    Join Date
    May 2003
    Location
    England
    Posts
    7
    Are you trying to pre-select an option based on a previous CGI script such as reading in a query variable and setting the selected option in the generated HTML, or from a previous select box within the page, such as select country then select city, but download the cities from your CGI app.?

    Option 1;

    $country = $query->param('country');
    @countries = qw{France England Spain Germany};
    print "<select name=country>\n";
    foreach(@countries){
    if($_ eq $country){
    print "<option SELECTED>$_</option>\n";
    }else{
    print "<option>$_</option>\n";
    }
    }
    print "</select>";

    Option 2:

    No this can't be done unless you use javascript and load up a popup window then close it within the javascript or load up data within another frame;

    <html>
    <head>
    <script language="javascript">
    <!--

    function loadCities(){
    var popup = window.open("popup.html", "Loading", "WIDTH=250,HEIGHT=250,TOOLBARS=NO,STATUS=NO,RESIZEABLE=NO");

    document.fred.city.options.length=0;
    if(popup.options){
    for(var i=0;i<popup.options.length;i++){
    document.fred.city.options[i] = new Option(popup.options[i], popup.options[i]);
    }
    }
    popup.close();
    return;
    }

    -->
    </script>
    </head>
    <body>
    <form name="fred">
    <select name="country" onchange="loadCities()">
    <option value=0>Select Option</option>
    <option value=1>France</option>
    <option value=2>England</option>
    <option value=3>Germany</option>
    </select>

    <select name="city">
    </select>
    </form>
    </body>
    </html>


    popup


    <html>
    <head>
    <script>
    <!--

    var options = ['Nice', 'Paris', 'Lens'];

    -->
    </script>
    </head>
    <body>
    Loading....
    </body>
    </html>

  3. #3
    Join Date
    May 2003
    Posts
    11
    actually i'm currently using javascript but i wish to get the data from the database tables and display it as another select field in PHP...

  4. #4
    Join Date
    May 2003
    Location
    England
    Posts
    7
    The code given is only an example of how this works. Instead of using a static HTML page, you would load up another PHP script in which you read more data from the database, then write the data back in the popup window's javascript. You'll always need to use a client side script.

    So, instead of the HTML for popup;

    print <<END_OF_HEADER;
    <html>
    <head>
    <script>
    <!--

    END_OF_HEADER

    #read data from database....

    print "var options = [";
    for( $i=0; $i < scalar(@row); $i++ ){
    print "'" . $row[$i] . "'";
    if( $i < scalar(@row) - 1){
    print ",";
    }
    }
    print "];";

    print <<END_OF_HTML;

    -->
    </script>
    </head>
    <body>
    Loading....
    </body>
    </html>
    END_OF_HTML

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