[SOLVED] Can't get values from object for dynamic dropdown list
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can't get values from object for dynamic dropdown list
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);
});
}) // done
After two days messing around I haven't found the key to making this work yet.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.