Solved

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

Posted on 2014-01-02
3
507 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

623 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