LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2021, 08:13 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Rep: Reputation: 12
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.

Please help!
 
Old 11-30-2021, 10:55 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,883
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
Tried JSON.parse on JavaScript side? https://developer.mozilla.org/en-US/...cts/JSON/parse
 
Old 12-02-2021, 05:27 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 443

Original Poster
Rep: Reputation: 12
Thanks much.

Works fine.

R
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to style Browser's HTML Dropdown List using Linux GTK+ 2.0 themes riteshjagga Linux - General 4 07-08-2014 11:52 AM
using Universal USB Installer - can I select U12.04.2 from their dropdown list if... RedBullion Linux - Newbie 1 08-31-2013 04:45 PM
Search result MYSQL using dropdown list koscek Programming 2 11-02-2007 06:42 AM
wine fonts small dropdown list invisible in STELLA bristi Linux - Software 1 06-22-2005 06:07 AM
where is majorcool list dropdown facility pudhiyavan Linux - Software 1 06-09-2004 03:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration