Send a PHP array to jQuery

I have array like this:

$offices[$data['Id_Office']] = array($data['Name'], $data['Address'], $data['Telephone']);
echo json_encode($offices);

Now in the jQuery side, I make an AJAX request like this and trying to extract value from returned PHP array:

.
.
   	    $.ajax({
    	    type: "POST",
    	    url: "ajax.php",
    	    //dataType: 'jsonp',
   	    data: "townid="+ town,
    	     success: function(arrayPHP){


$.each(arrayPHP, function (i, msg) {

    	       $("#name").html(msg);
    	       $("#address").html(msg);
    	       $("#telephone").html(msg);
});


		  }
  		  }); 

Firebug show this:

{"2":["agence01","address01","5465460"]}

But in this part no text looks to be appear:

<label for="Name">Name: </label><span id="name"></span><br />
<label for="Address">Address: </label><span id="address"></span><br />
<label for="Telephone">Telephone: </label><span id="telephone"></span>

You haven’t specified which value from the array you wish to output.



$("#name").html(msg[0]);
$("#address").html(msg[1]);
$("#telephone").html(msg[2]);


I had try it, but it doesn’t work tome, its only show this char-carter ‘}’.

In your javascript set the dataType to json otherwise the return will be in plain text which is why its not working correctly.

Now its work, thanks Mr GOPalmer and Mr SgtLegend.

i am new to the forum, I would like to know how did you solve this problem, as I’m finally code?, thank you very much

Read the thread. The original posts, show how to return the array from PHP to JavaScript using json_encode, the rest simply shows how to read the value in JavaScript.

If you have code, post it and we can help you out.

thanks for replying

$msg = array("1111111111", "Pedro");
echo json_encode($msg);
<script type="text/javascript">
 $(document).ready(function(){
        $("#boton").click(function(){
             var campo =  'rut_consulta='+ jQuery("#rut_consulta").attr("value");
       $.ajax({
                 type : "POST",
                 url : "scripts/search_workers.php" ,
                 traditional : true,
                data : campo,
                success : function(arrayPHP){
				
				$.each(arrayPHP, function (i, msg) {

              $("#rut").val(msg);
               $("#nombre").val(msg);
});


			    }
       });
       })
  })
 </script>  
 <td><input name="rut" type="text" id="rut" size="13" maxlength="12" readonly="readonly"/></td>
                <td> <input name="nombre" type="text" id="nombre" size="100" maxlength="100" readonly="readonly"/></td>

The following should work

<script type="text/javascript">  
 $(document).ready(function(){  
	$("#boton").click(function(){  
	 var campo =  'rut_consulta='+ jQuery("#rut_consulta").attr("value"); 
	 $.ajax({  
		type : "POST",  
		url : "scripts/search_workers.php" ,  
		traditional : true,  
		data : campo,  
		success : function(arrayPHP){  
			$("#rut").val(arrayPHP[0]); 
			$("#nombre").val(arrayPHP[1]); 
		}  
	 });  
 })  
})  
 </script>

Personally, I like to give my array indexes names, like so

$msg = array('rut' => "1111111111", 'nobre' => "Pedro");
echo json_encode($msg);

As then you can access the data using the following:

<script type="text/javascript">  
 $(document).ready(function(){  
	$("#boton").click(function(){  
	 var campo =  'rut_consulta='+ jQuery("#rut_consulta").attr("value"); 
	 $.ajax({  
		type : "POST",  
		url : "scripts/search_workers.php" ,  
		traditional : true,  
		data : campo,  
		success : function(arrayPHP){  
			$("#rut").val(arrayPHP['rut']); // if that doesn't work, try arrayPHP.rut, but I believe the original way I wrote it should work
			$("#nombre").val(arrayPHP['nombre']); // if that doesn't work, try arrayPHP.nombre
		}  
	 });  
 })  
})  
 </script>

in both cases shows me:

{“rut”:“1111111111”,“nobre”:“Pedro”}
{“rut”:“1111111111”,“nobre”:“Pedro”}

You may need to tell $.ajax the type of response being returned using

<script type="text/javascript">  
 $(document).ready(function(){  
	$("#boton").click(function(){  
	 var campo =  'rut_consulta='+ jQuery("#rut_consulta").attr("value"); 
	 $.ajax({  
		type : "POST",  
		url : "scripts/search_workers.php" ,  
		traditional : true,  
		data : campo,  
		type : "json", // added this line
		success : function(arrayPHP){  
			$("#rut").val(arrayPHP['rut']); // if that doesn't work, try arrayPHP.rut, but I believe the original way I wrote it should work
			$("#nombre").val(arrayPHP['nombre']); // if that doesn't work, try arrayPHP.nombre
		}  
	 });  
 })  
})  
 </script>

Likewise, you could also set the mime type of your json output to tell jQuery what to use

$msg = array('rut' => "1111111111", 'nobre' => "Pedro");
header('content-type: application/json');
echo json_encode($msg);  

thank you very much!, I am really very grateful, had the problem yesterday and finally I could fix it, thanks cpradio!!!
greetings from Chile

Oh, one last question as I can assign values ​​to input vector, because if I use:

$ (“# rut”). html (arrayPHP.rut);
$ (“# name”). html (arrayPHP.nombre);

<div id=“rut”> </ div> <div id=“nombre”> </ div>

works perfect, but if I use (val):

$ (“# rut.”) val (arrayPHP.rut);
$ (“# name”). val (arrayPHP.nombre);

<td> name=“rut” <input type=“text” size=“13” id=“rut” maxlength=“12” readonly=“readonly”/> </ td>
<input type=“text” name=“name” <td> id=“name” size=“100” maxlength=“100” readonly=“readonly”/> </ td>

no load in the input data, thanks again

and fix it, thank you very much, the problem was that I had more elements in the html with the same id, thanks.