Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To populate the form in this manner using JavaScript will require a somewhat roundabout method.</p> <p>Either create a hidden field for javascript to read from to obtain the $location variable's value, or create an ajax call that retrieves it.</p> <p>Next you will write a JavaScript function that returns the appropriate values based on the location. Something like: </p> <pre><code>function get_tours( location ){ var tours = {}; switch locations { case 'Austin': tours = { "City Center": { "text": "City Center" "value": "city_center" } }; break; //etc... } return tours; } </code></pre> <p>If you choose to go with the AJAX method you will need to call this tour option setting function when your AJAX returns with a success.</p> <p>Now that you have the tours in your JavaScript variable you will need to update the elements of the element in the page that was generated by PHP. Something like:</p> <pre><code>function create_tour_options ( tours ){ var select = document.getElementById('tour'); for ( tour in tours ){ tour_option = document.createElement('option'); tour_option.value = tours['tour']['value']; tour_option.text = tours['tour']['text']; select.appendChild('tour_option'); } // Probably more efficient to create a new select object // and replace the existing one instead of adding and // removing individual options. } </code></pre> <p>Additionally if you're not required to use JavaScript, you could simply write a php function to do the above and return your $options_tour variable. Something like:</p> <pre><code>function get_tours( $location='Austin' ){ $tours = array(); switch $location { case 'Austin': $tours = array( "text" =&gt; "City Center" "value" =&gt; "city_center" ); break; //etc... } return $tours; } </code></pre> <p>I believe it would be better to use PHP if you can as the options will be generated on the initial page load and will not have to be inserted afterwards.</p> <p>Also I'm not sure if you're using JQuery or not, so I attempted to use vanilla JavaScript example code.</p>
 

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