1

I am new at developing. This is some code I wrote below. I have a dynamic drop down list with 3 selections. The first selection is Country and then provinces and then cities. I used a database for values because of the size of data. I am able to run the below code if I set $coun_val='Canada' and COUNTRY_VAL='Canada'(one of the database values) and see the correct provinces. However I want to use $coun_val=$_REQUEST['Country'] and use that in the sql query as well, however the value seems to be null and nothing populates.

Note: $_REQUEST['Country'] is used in the first select in an if statement to set the selected value and works.

<div class="fieldsarea"><select id="select_province"  class="searchfields" name="select_province">  
<option class="" value="">--Select Province/State--</option>
<?php
  $coun_val=$_REQUEST['Country'];  
  $query1 = "SELECT * FROM dynamicprovs WHERE COUNTRY_VAL=mysqli_real_escape_string($conn,$coun_val);
  $result1 = mysqli_query($conn,$query1) or die(mysqli_error($conn));
  while ($record = mysqli_fetch_array($result1)) {
        $prov_val=$record['PROVINCE_VAL'];
        if($_REQUEST['province']==$prov_val){
        echo "<option class='" . $coun_val . "'value='" . $prov_val ."'selected='selected'>'". $prov_val ."'</option>";
        }else
        {
        echo "<option class='" . $coun_val . "'value='" . $prov_val ."'>'". $prov_val ."'</option>";
        }
    }

?>
</select>

</div>
julesdev1
  • 13
  • 3
  • 1
    You have a syntax error in the line that declares `$query1`. – steven Feb 16 '16 at 23:35
  • I don't know if you already did. But read this to learn how to submit the form on change of the first (and second) select field: http://stackoverflow.com/questions/7231157/how-to-submit-form-on-change-of-dropdown-list – steven Feb 16 '16 at 23:48

1 Answers1

1

You have three ways to achieve this:

  1. Submit the page after each selection is made. This way, it would be possible to pass the selected value as a $_REQUEST variable and make your 2nd and 3rd select actually work.

  2. Grab all values and store them in a JavaScript arrays, and populate the 2nd and 3rd select values dynamically from them.

  3. Create AJAX handlers for 1st and 3rd requests, so that on each "change" event they would send a request to the PHP backend to retrieve the values for 2nd and 3rd selects.

Either of these 3 ways have benefits (1st being the simplest, 2nd being the fastest in the browser, 3rd being the most elegant and resource-saving), so it's up to you to decide which one to choose.

Plus, as mentioned by @steven, you have an error in your query: instead of

$query1 = "SELECT * FROM dynamicprovs WHERE COUNTRY_VAL=mysqli_real_escape_string($conn,$coun_val);

it should say

$query1 = "SELECT * FROM dynamicprovs WHERE COUNTRY_VAL='". mysqli_real_escape_string($conn,$coun_val) ."'";
Sergey Vidusov
  • 1,342
  • 1
  • 7
  • 10
  • I think as a newby he should beginn with the first approach and it seems to be the case that he already did. Maybe we shoud point him more concrete to do a submit on change of the first select field. Maybe he already did but we don't know because he didn't post the whole code. – steven Feb 16 '16 at 23:46