-1

In my application , i am having a code like

$(document).ready(function(){
    $("#"+<?=$r['Attribute']['id'];?>).each(function() { 
          type= "<?=$r['Attribute']['type'];?>";
          attribute_id= "<?=$r['Attribute']['id'];?>";
          if(type=="dropdown")
          {
               var ht = $.ajax({
                   type: "GET",
                   url: "http://localhost/FormBuilder/index.php/forms/viewChoices/"+attribute_id,
                   async: false
               }).responseText;


               var myObject = eval('(' + ht + ')');
               alert(myObject);


               // var getchoice=myObject.choices[0]["choice"];
               //alert(getchoice);

                                                    }
        });
     }):

In the above code for each attributeid , i am seeing whether the type of its is a Dropdown then i am retriving its choices from that Ajax get from the Table Choices in the choice, My myObject displays like

   {"choices":[{"choice":"Male"},{"choice":"Female"}]}//for a attribute with the label gender

   {"choices":[{"choice":"BE"},{"choice":"ME"},{"choice":"MBA"}]}//for a attribute with the label qualification

But that getChoice displays only male , and later be since i am using [0]

i want to keep these first alerted values that is male and female in an array And then in the same array later to save only be,me,mba .. Because i want to keep these array values to be displayed in the dropdown box for the respective labels ...How to do so.......Please sugggest me...

1 Answer 1

0

A couple of things that you could improve:

1 . Using each with an id selector.

$("#"+<?=$r['Attribute']['id'];?>).each

Using each doesn't makes sense, since this selector will match a single element, and the element id must be unique.

2 . $.ajax for handling a JSON response

If you are doing an ajax request that returns JSON, the $.getJSON function is the best way to go, you don't have to parse the responce, it will be safely evaluated by jQuery.

3 . Your request is synchronous.

I'm not against synchronous requests, but I think that you don't explicitly need it.

Finally, for your JSON response, you could easily flatten the result, using the $.map function for example:

var data = {"choices":[{"choice":"Male"},{"choice":"Female"}]};
$.map(data.choices, function(i){return i.choice;}); 
// returns ["Male", "Female"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.