Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
Suggested Courses

972 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