Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You'll probably want to use AJAX for this. Thats a technique using PHP and Javascript that allows your page to get new data from the server without a page reload. This will allow your page to initally get only the states from a server and then get the relevant cities and their data from a PHP script.</p> <p>So you're going to need</p> <ol> <li><p>First HTML page. Can be static or PHP generated, but will need some javascript so that once a state is selected from the states drop-down the city drop down will be generated.</p> <p>a- The original HTML page with state drop down</p> <p>b- The javascript described above</p></li> <li><p>A PHP script. This page will never be gone to 'directly' (you wouldnt type its URL into your browser most likely), but it will be the target of a server request.</p> <p>Input - Parameter indicating which state</p> <p>Output - The dropdown with the cities belonging to that state and their values.</p></li> </ol> <hr> <p>You don't state what form your data is in (im guessinng/hoping a relational DB? Document DB wouldnt be bad, but CSV would be pretty tough) but ill try to paint a rough sketch of the PHP script I describe in step 2.</p> <p>getcities.php</p> <pre><code>&lt;?php $state = $_GET['state']; $cityArray = getCityArray($state); //i won't delve into details here, but probably takes some database calls echo "&lt;select name='citySelect'&gt;"; foreach($cityArray as $cur) { list($x,$y,$name) = array($cur['x'].$cur['y'],$cur['name']); //this is just to make the next line simpler. PHP extract function can do something similar too echo "&lt;option value='$x,$y'&gt;$name&lt;/option&gt;"; } echo "&lt;/select&gt;"; ?&gt; </code></pre> <p>You can see how this generates the select/option dropdown with only the required elements.</p> <p>The javascript will be responsibly for calling something like <code>getcities.php?state='NY'</code> and putting the output into an element of your page (likely a div)</p> <p>Look up Ajax basics for how to do that. Libraries like jQuery can greatly simplify the Ajax.</p> <hr> <p>EDIT - in response to your comments</p> <p>So on your page you can do something like</p> <pre><code>&lt;select name='state' id='stateSelect' onchange='loadCities()'&gt; &lt;option value='' select='selected'&gt;Please select a state&lt;/option&gt; &lt;option value='alabama.html'&gt;Alabama&lt;/option&gt; OPTIONS FOR EACH STATE HERE &lt;/selecT&gt; &lt;select name='city' id='citySelect'&gt; //blank initially. gets filled when you pick a state &lt;/select&gt; &lt;script type='text/javascript'&gt; function loadCities() { $('#citySelect').load($('#stateSelect').val()); //this assumes files in same place. otherwise you might need to put a path here } &lt;/script&gt; </code></pre> <p>Basically, if the format is as you describe, all you need to do is load the contents of a certain HTML file into the <code>&lt;select&gt;</code> i call <code>citySelect</code> above. This can be done entirely by javascript, and with the use of the jQuery library it is pretty much trivial.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload