Solved

Jquery trouble with returned CURL json encoded values when populating a selectlist

Posted on 2014-01-02
3
505 Views
Last Modified: 2014-01-02
I have a curl request that is returning below (I did a var_dump and you can also see the json encoded value below that.

object(SimpleXMLElement)#21 (1) {
  ["trim"]=>
  array(2) {
    [0]=>
    string(1) "S"
    [1]=>
    string(2) "SR"
  }
}
{"trim":["S","SR"]}

Open in new window


The jquery code I'm using to fetch the data looks like this:
<script type="text/javascript" charset="utf-8">

$.post("deals/cbb_trim/",{ year:<?php echo $trade['year'];?>,make:'<?php echo $trade['make'];?>',model:'<?php echo $trade['model'];?>' },
		function(data){
		if((data.trim.length) > 1) {
        var trims = data.trim;
        var options = [];
        for(var i=0;i<trims.length;i++)
        {
            var trim = trims[i];
            options.push("<option value='" + trim[i] + "'>" + trim[i] + "</option>");
        }
        $("#cbb_trim").append( options.join() );
		}
		});
  
</script>

Open in new window


But what is populating my select list seems to be the correct 'length' of the data, but instead of each value, I'm getting more or less each separate word or a letter if it has a space on each side. I've attached a screenshot so you can see what I mean.

screenshot of selectlist issueAny idea what I'm missing?
0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39751270
Please throw in an alert() for the returned data element right after line 4 in the jQuery snippet, thanks.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39751275
replace : options.push("<option value='" + trim[ i ] + "'>" + trim[ i ] + "</option>");
by : options.push("<option value='" + trim + "'>" + trim + "</option>");

Test page : http://jsfiddle.net/378Bp/

var params = { year:'',make:'',model:'' };
//$.post("deals/cbb_trim/", params, function(data){
    var data = {"trim":["S","SR"]};
    if((data["trim"].length) > 1) {
        var trims = data["trim"];
        var options = [];
        for(var i=0;i<trims.length;i++)
        {
            var trim = trims[i];
            options.push("<option value='" + trim + "'>" + trim + "</option>");
        }
        $("#cbb_trim").append( options.join() );
    }
//}); 

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39751281
Thanks, that was it. More or less a brainfart at my end.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JSON help 8 48
How can I split a variable 19 45
Windows 10, 64 bit WAMP - won't start 9 30
How do I init a PHP page jusing JQuery? 8 30
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question