Solved

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

Posted on 2014-01-02
3
493 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
3 Comments
 
LVL 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now