Populating A Dropdown Box, Based On The Selection Of Another Dropdown Box Using Php, Mqsql And Ajax
I am trying to populate a drop down box, based on a selection from another drop down box. Here is what I have for php code:
Solution 1:
May be you are using the SUBSTRING_INDEX
function in the wrong way.
In your 1st SELECT
you might not be getting any output or may be blank rows.
According to the website -
SUBSTRING_INDEX(str,delim,count)
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.
Solution 2:
This is how I solved my issue using JavaScript and Ajax:
$("#area").change (function(){
$("#acctRecords tbody tr").remove();
$('#label').show();
$('#label2').show();
$('#location').show();
$('#section').show();
$.ajax({
type: "POST",
async: true,
url: "area.php",
dataType:"json",
data: ({area: $('#area').val()}),
success: function(data) {
$('select#location').empty();
$('select#location').append('<option value="0">Select Option</option>');
//Populate options of the second dropdownfor(var x = 0; x < data.id.length; x++) {
$('select#location').append('<option value="'+data.id[x]+'">'+data.location[x]+'</option>');
} //end of each function
} // end of success
}); // end of ajax call
}); // end of area change function
This posts to my HTML of:
echo"<labelfor='location'id='label'>Select location:</label>";
echo"<selectid='location'name='location'>";
echo"</select><br />";
Works perfectly!
Post a Comment for "Populating A Dropdown Box, Based On The Selection Of Another Dropdown Box Using Php, Mqsql And Ajax"