Can't get values from object for dynamic dropdown list
by pizzipie from LinuxQuestions.org on (#5SHDH)
Hi,
In trying to create dropdown list from 'mysql' database records I can't get the values from PHP json_encoded string. Following is pertinent bits of the code and some results.
Code:================= PHP select.php ===============
=========== rows[] =============
<pre>Array
(
[0] => Bank
[1] => Linux
[2] => Map
[3] => Insurance
[4] => Utilities
[5] => Buy/Sell
[6] => Travel
[7] => Misc
[8] => Internet
[9] => Computer
[10] => Health
[11] => Gov
)
// create array of arrays
for($i=0; $i<count($rows); $i++) {
$cats[$i]=array("Category" => strtolower($rows[$i]), "Name" =>$rows[$i]);
}
echo json_encode($cats);
======================= JAVASCRIPT ==================
var request=$.ajax({
url: "select.php",
type: "POST",
data: {category:"category"},
datatype: "json"
});
// ==================== REQUEST DONE ==================
request.done(function(retval) { // json string
var cats=retval.split(','); // convert to object
alert(typeof cats+"\n\n"+cats+"\n");
======== RESULT:ALERT SHOWS THIS ====================
type cats .. object
[{"Category":"bank","Name":"Bank"},
{"Category":"linux","Name":"Linux"},
{"Category":"map","Name":"Map"},
{"Category":"insurance","Name":"Insurance"},
{"Category":"utilities","Name":"Utilities"},
{"Category":"buy\/sell","Name":"Buy\/Sell"},
{"Category":"travel","Name":"Travel"},
{"Category":"misc","Name":"Misc"},
{"Category":"internet","Name":"Internet"},
{"Category":"computer","Name":"Computer"},
{"Category":"health","Name":"Health"},
{"Category":"gov","Name":"Gov"}]
===================== CREATE OPTIONS ============================
var ddlCategory = $(".category");
$(cats).each(function () {
var option = $("<option />");
alert(this.name); // =================== shows 'UNDEFINED' =====
//Set Name in Text part.
option.html(this.Name);
alert(this.Category); // ================== shows 'UNDEFINED' =====
//Set Category in Value part.
option.val(this.Category);
//Add the Option element to DropDownList.
ddlCategory.append(option);
});
}) // doneAfter two days messing around I haven't found the key to making this work yet.
Please help!
In trying to create dropdown list from 'mysql' database records I can't get the values from PHP json_encoded string. Following is pertinent bits of the code and some results.
Code:================= PHP select.php ===============
=========== rows[] =============
<pre>Array
(
[0] => Bank
[1] => Linux
[2] => Map
[3] => Insurance
[4] => Utilities
[5] => Buy/Sell
[6] => Travel
[7] => Misc
[8] => Internet
[9] => Computer
[10] => Health
[11] => Gov
)
// create array of arrays
for($i=0; $i<count($rows); $i++) {
$cats[$i]=array("Category" => strtolower($rows[$i]), "Name" =>$rows[$i]);
}
echo json_encode($cats);
======================= JAVASCRIPT ==================
var request=$.ajax({
url: "select.php",
type: "POST",
data: {category:"category"},
datatype: "json"
});
// ==================== REQUEST DONE ==================
request.done(function(retval) { // json string
var cats=retval.split(','); // convert to object
alert(typeof cats+"\n\n"+cats+"\n");
======== RESULT:ALERT SHOWS THIS ====================
type cats .. object
[{"Category":"bank","Name":"Bank"},
{"Category":"linux","Name":"Linux"},
{"Category":"map","Name":"Map"},
{"Category":"insurance","Name":"Insurance"},
{"Category":"utilities","Name":"Utilities"},
{"Category":"buy\/sell","Name":"Buy\/Sell"},
{"Category":"travel","Name":"Travel"},
{"Category":"misc","Name":"Misc"},
{"Category":"internet","Name":"Internet"},
{"Category":"computer","Name":"Computer"},
{"Category":"health","Name":"Health"},
{"Category":"gov","Name":"Gov"}]
===================== CREATE OPTIONS ============================
var ddlCategory = $(".category");
$(cats).each(function () {
var option = $("<option />");
alert(this.name); // =================== shows 'UNDEFINED' =====
//Set Name in Text part.
option.html(this.Name);
alert(this.Category); // ================== shows 'UNDEFINED' =====
//Set Category in Value part.
option.val(this.Category);
//Add the Option element to DropDownList.
ddlCategory.append(option);
});
}) // doneAfter two days messing around I haven't found the key to making this work yet.
Please help!