Article 5SHDH Can't get values from object for dynamic dropdown list

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!latest?d=yIl2AUoC8zA latest?i=ry57fY84nxI:_uUu4qKyBp8:F7zBnMy latest?i=ry57fY84nxI:_uUu4qKyBp8:V_sGLiP latest?d=qj6IDK7rITs latest?i=ry57fY84nxI:_uUu4qKyBp8:gIN9vFw
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments