We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 54308
    • 9 Posts
    I have a form that has a state and county field, and a hidden field where the destination email address will be submitted (there are many other fields besides these.)
    The initial page load should default the state to Nebraska.
    The county field should be limited to just counties in the selected state and can default to the 1st one in the list. (Stored procedure will provide the appropriate sort order.)

    I made a snippet that contains the following (there will be two columns returned and anywhere from 1-254 records): (Is this how I would return a recordset inside a PHP array?)
    ...
    $sql = "{call Web_GetCountyData4State(?)}";
    $state = $_REQUEST['usState1'];
    $params = array(array(&$state, SQLSRV_PARAM_IN));
    $stmt = sqlsrv_query($conn, $sql, $params);
    
    if ($stmt === false) {
        echo "Error in Code at getCountyData_step1";
        die( print_r( sqlsrv_errors(),true));
    }
    else {
        $counties = array();
        while ($row = sqlsrv_fetch_array($stmt)) {
            $counties[]=$row;
        }
    }
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    
    $countyarray=json_encode($counties);
    return $countyarray;
    


    Portions of the HTML/Javascript/jQuery chunk
    <select name="usState1" id="stateInput1" class="form-control required" value="[[!+fi.usState1]]" placeholder="State">
    [[!getStates]]
    </select>
    <select name="county1" id="county1" class="form-control required" value="[[!+fi.county1]]" placeholder="county">
    <!--Need help with javascript/jquery to populate the drop-down on intial page load (where state="Nebraska")-->
    </select>
    <input type="hidden" name="ToEmail" value="">

    ...
    $('#stateInput1').change(function(e) {
    // how do I populate the county drop-down?
    });
    $('#county1').change(function(e) {
    //How do I get the array value for salesman email for the selected county into the hidden ToEmail value?
    });[/i]
      • 54308
      • 9 Posts
      I ended up creating 50 different county dropdowns. One for each state. I hide all but the one for the default state on page load and the set up an onchange event handler to hide the current dropdown and show the dropdown for the selected state. Values in the county dropdowns contained the state-abbrev_county-name_salesman-email-address. When a county is selected, the value is parsed into some hidden form fields for county and email address.

      Seems like there should have been a data driven way to do this but fortunately, this data doesn't change often. I'll periodically have to replace the county dropdown chunks when salesmen change.