So I'm currently in the process of trying to populate a select option list with some SQL using ajax and php. I've tried various different pieces of code however I still can't seem to crack it. Here is the ajax from the actual page its self...
$.ajax ({
url:'orderentry_ajax.php',
cache:false,
data: {'request': 'getCounty', 'County': County},
dataType: 'json',
async: false,
success: function(data)
{
$('#errMsg').html(data.errMsg);
if(data.numRecs>0)
{
//divStr = divStr + data.custName + data.contactName + data.contactNumber + data.contactEmail;
countyStr = countyStr + "<select>";
for (var i=0; i<data.dataArray.length; i++)
{
countyStr = countyStr +
"<option value='data.dataArray[i].County'>" +
"Please Select" + data.dataArray[i].County + "</option>";
}
countyStr = countyStr + "</select>";
$('#Countys').html(countyStr);
}
}
//countyStr = countyStr + data.dataArray[i].County +
});
As far as I'm concerned I did a similar exercise except I was populating the options list with another table, I've made the two pieces of ajax and php identical and it still doesnt seem to want to work. Here is the php from the ajax page....
if (trim($request) =='getCounty')
{
//product update
$County = $_REQUEST['County'];
$errMsg = "";
$con = mysqli_connect('127.0.0.1', 'root', 'c0mplex', 'HRDatabase');
//Check if connect..
if (mysqli_connect_errno($con))
{
$errMsg = 'Could not connect to Database.' . mysqli_connect_error();
}
else
{
// passed record for submit
$qryStr = "SELECT * FROM county WHERE `county` = $County";
//echo $qryStr;
$result = mysqli_query($con, $qryStr);
if (mysqli_error($con))
{
//echo (mysqli_error($con));
$errFlg=1;
$errMsg="Error during update, please try again. " . mysqli_error($con);
}
else
{
while ($row = mysqli_fetch_array($result))
{
$County = $row['county'];
$rowing = array();
$rowing['county'] = $County;
$dataArray[] = $rowing;
}
$numRecs = mysqli_num_rows($result);
}
}
mysqli_close($con);
//to test error :
// $errMsg="testing error";
$info ->dataArray = $dataArray;
$info ->numRecs = $numRecs;
$info ->errMsg = $errMsg;
$info ->County = $County;
echo json_encode($info);
//echo $msg;
}
The select option list has an ID on it of 'Countys' just to give a heads up. Any help would be greatly appreciated guys.
Cheers