?
Solved

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

Posted on 2014-01-02
3
Medium Priority
?
510 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 111

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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 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