-2

i have created a AJAX request , where i have sent data to process.php page , this page will process this data and then will create a JSON object that will be sent as AJAX response. Now i need to create one second JSON object in order to sent as AJAX response, that is possible? , if is possible , how i can to manage this two objects separatelly?

The code that i have is:

$(document).on('change', '#seleccio_test,#seleccio_test2', function(){

    var capaTEST;
    var pagina="show_test_prova.php";
     $.ajax({
                 async:false,    
                 cache:false,   
                 dataType:"json",        
                 type: "POST",   
                 url: pagina,
                 data: "dades_test="+obtenirTest(),
                 success:  function(obj1){ 

                    //alert(JSON.stringify(resultat));
                        var idtest=0;
                        $.each(obj1, function(key, value) {//object 1 that i retrieve from process.php

                            if(key !=0){

                                for (i = 0; i < value.length; i++) { 
                                    alert("Key:" + key + " /value: " + value+" /index: "+idtest);

                                    var llistat_test="";
                                    llistat_test +="<div id='"+value+"'>";
                                    llistat_test+="<ol><li><label for='test' id='etiquetalabel'>"+value[i]+"</label>";
                                    llistat_test+="<input type='checkbox' id='$capaCONTENT"+idtest+"name='test[]' value='"+value[i]+"'";
                                    llista_test+="onclick=' onclick='show_content($seleccio_content,$id_capa,$pagina,$idtest,$valor_org,$valor_eval,$valor_test)';>";
                                    llistat_test+="</div> <ol> <div style='display: none;'> </div></ol></li></ol> ";

                                    var contenido= $("#"+key);
                                    var elementBuscado=$("#"+value);

                                     //if(!contenido.find(elementBuscado).length){
                                        //no exist
                                        contenido.append(llistat_test);

                                        //}
                                    idtest++;
                                        }       
                                    }

                                });

                            },
        beforeSend:function(){},
        error:function(jqXHR, textStatus, errorThrown ){}
    })

})  

Regards.

riztak
  • 55
  • 2
  • 8

3 Answers3

1

Yes, place the two objects together into a single larger JSON object. Either an array of two, or a Map/Hash/Object with key+value pairs would work fine.

I don't see your PHP code, but supposing you decided to return an array of two objects, your client code would look like

success:  function(array){
   obj1 = array[0]
   obj2 = array[1]
  ... 

JSON objects can contain an unlimited amount of inner arrays or objects.

Example JSON object: {"key","value"}

Example JSON array of two objects: [ {"key","value"}, {"key","value"} ]

700 Software
  • 85,281
  • 83
  • 234
  • 341
  • first of all, i have cretaed an dynamic array with this: `$array_container[$CapaEnviar][] = $fila['test_name'];` and in second place i have encoding it using this: ` echo json_encode($array_container,true);`, so how i can modify this in order to achieve what i want? – riztak May 09 '16 at 18:16
  • somebody has an idea ? – riztak May 09 '16 at 18:29
  • 1
    See [vahid's answer](http://stackoverflow.com/a/37122644/463304) Where he says `$var1` you would say `$array_container`. This makes `$array_container` to be your first JSON object. In place of `$var2` you would place your second JSON object. – 700 Software May 09 '16 at 18:31
  • 1
    I would recommend creating a very small JSON test page to play with JSON and make sure you understand how you can create new objects within other objects. – 700 Software May 09 '16 at 18:32
  • I have created an array like this: `$array_D1[$CapaEnviar][] = $fila['test_name']; $array_D2[]=$seleccio_content; $array_D2[]=$id_capa; $array_D2[]=$pagina; $array_D2[]=$idtest; $array_D2[]=$valor_org; $array_D2[]=$valor_eval; $array_D2[]=$valor_test; $array_enviar=array('data1' => $array_D1, 'data2' => $array_D2);echo json_encode($array_enviar,true);` ,but i could not retrieve the data from jquery `*alert(obj.data2); or *alert(obj.data2[0]);` – riztak May 09 '16 at 19:39
  • @riztak, I would suggest a new question at this point. That means you should first mark an answer here as correct, and then make a new question. This question here was "how to do it". Your new question would be "I'm getting an error". – 700 Software May 09 '16 at 20:04
  • It can help a lot if you create an [SSCCE](http://sscce.org/) of both your JavaScript and PHP. Be sure to check the **Console** of your browser for JavaScript Errors or the presence of a 500 Server Error. – 700 Software May 09 '16 at 20:04
0

As you echo json_encode($var) for your response, you can use array instead of variable like this:

echo json_encode(array('data1' => $var1, 'data2' => $var2));

And in js or jquery part you can access to them by obj.data1 and obj.data2

Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88
-1

have a look at multipart/form-data. haven't used it, but I think it is what you want.

HTTP multipart response using Perl or PHP

Community
  • 1
  • 1
Andrei Neagu
  • 898
  • 8
  • 25