html - 1MB worth of drop-down list data, needs to be downloaded and displayed dynamically -


so i've got website 2 html drop-down lists- 1 list of states, other list of cities/towns in selected state. of right now, have file each state, , have options listed such:

<option value="44.729932, -72.381758">albany</option> <option value="44.976728, -73.30257">alburg</option> 

the value city/town's coordinates. can't have data downloaded @ once (well guess it's 1mb , i'm serving page on 130kb/s connection...) i'd way have each town/city file downloaded once state name has been selected... made these files under assumption this:

<select name="state" method="get" id="state">      <option value="citiestowns/alabama.html">alabama</option>      <option value="citiestowns/alaska.html">alaska</option> </select> <select id="cityortown" name="cityortown"> <?php $state = $_get['state'];  include("$state"); ?> </select> 

but when this, state drop down list , blank drop down list next it... heeeeeelp! :(

you'll want use ajax this. thats technique using php , javascript allows page new data server without page reload. allow page initally states server , relevant cities , data php script.

so you're going need

  1. first html page. can static or php generated, need javascript once state selected states drop-down city drop down generated.

    a- original html page state drop down

    b- javascript described above

  2. a php script. page never gone 'directly' (you wouldnt type url browser likely), target of server request.

    input - parameter indicating state

    output - dropdown cities belonging state , values.


you don't state form data in (im guessinng/hoping relational db? document db wouldnt bad, csv pretty tough) ill try paint rough sketch of php script describe in step 2.

getcities.php

<?php  $state = $_get['state']; $cityarray = getcityarray($state); //i won't delve details here, takes database calls  echo "<select name='cityselect'>"; foreach($cityarray $cur) {     list($x,$y,$name) = array($cur['x'].$cur['y'],$cur['name']); //this make next line simpler.  php extract function can similar     echo "<option value='$x,$y'>$name</option>"; } echo "</select>";  ?> 

you can see how generates select/option dropdown required elements.

the javascript responsibly calling getcities.php?state='ny' , putting output element of page (likely div)

look ajax basics how that. libraries jquery can simplify ajax.


edit - in response comments

so on page can like

<select name='state' id='stateselect' onchange='loadcities()'>     <option value='' select='selected'>please select state</option>     <option value='alabama.html'>alabama</option>     options each state here </select>  <select name='city' id='cityselect'> //blank initially.  gets filled when pick state </select>  <script type='text/javascript'>    function loadcities()   {     $('#cityselect').load($('#stateselect').val()); //this assumes files in same place.  otherwise might need put path here   } </script> 

basically, if format describe, need load contents of html file <select> call cityselect above. can done entirely javascript, , use of jquery library pretty trivial.


Comments