0

I want to access an array in J-query returned by PHP script via Ajax call

Ajax call for getting result from PHP script:

    $.ajax({
               url: "http://localhost/WCPM/index.php/demand/check_demand_status",
               type: 'GET',
               cache: true,
               data: {
                Meter_Group_Name: $(this).val()
               },
               success: function(data) {
               if(data["exists"]==1){
                 alert("Request already in pending for this Group");
                 $('#Meter_Group_Name').attr('selectedIndex',0);    
               }
              $("select#meter_number").html(data["option"]);
            }
    });

Response of Ajax Call from Php script:

     Array
        (
            [exists] => 1
            [option] => <option value=''>Please Select Meter</option>
<option value='5'>222000</option>
    <option value='6'>101010</option><option value='7'>34500A</option>
    <option value='13'>A00001</option><option value='14'>A11149</option>
    <option value='15'>000123</option><option value='16'>A00003</option>
    <option value='17'>A00002</option>
        ) 

Html Select Tag where option field to be store

   <select name="Meter_Numbers" id="meter_number"></select>

How to check data["exists"]==1 in jquery code and populate select in html with data["option"]?

1
  • Try returning the array as a json object using json_encode() Commented Jul 1, 2013 at 11:42

1 Answer 1

1

You can use json_encode() here like,

in js ajax function

Add type:

$.ajax({
     url:'',
     data:'',
     dataType:'json',//add this line

Refer datatype in ajax

In PHP Script use json_encode function like,

echo json_encode($yourarray);
return;
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.