SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Send a PHP array to jQuery

    I have array like this:
    Code:
    $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:
    PHP Code:
    .
    .
               $.
    ajax({
                
    type"POST",
                
    url"ajax.php",
                
    //dataType: 'jsonp',
               
    data"townid="town,
                 
    success: function(arrayPHP){


    $.
    each(arrayPHP, function (imsg) {

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


              } 
                }); 
    Firebug show this:
    Code:
    {"2":["agence01","address01","5465460"]}
    But in this part no text looks to be appear:
    Code:
    <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>

  2. #2
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You haven't specified which value from the array you wish to output.

    Code:
    $("#name").html(msg[0]); 
    $("#address").html(msg[1]); 
    $("#telephone").html(msg[2]);
    Last edited by GOPalmer; Jun 21, 2011 at 17:59. Reason: Wasn't clear

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GOPalmer View Post
    You haven't specified which value from the array you wish to output.

    Code:
    $("#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 '}'.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In your javascript set the dataType to json otherwise the return will be in plain text which is why its not working correctly.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  5. #5
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now its work, thanks Mr GOPalmer and Mr SgtLegend.

  6. #6
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SIFE View Post
    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

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ohkai View Post
    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.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for replying

    Code PHP:
    $msg = array("1111111111", "Pedro");
    echo json_encode($msg);

    Code JavaScript:
    <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>

    Code HTML4Strict:
     <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>

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    The following should work
    Code:
    <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
    PHP Code:
    $msg = array('rut' => "1111111111"'nobre' => "Pedro");
    echo 
    json_encode($msg); 
    As then you can access the data using the following:
    Code:
    <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>
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in both cases shows me:

    {"rut":"1111111111","nobre":"Pedro"}
    {"rut":"1111111111","nobre":"Pedro"}

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    You may need to tell $.ajax the type of response being returned using
    Code JavaScript:
    <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
    PHP Code:
    $msg = array('rut' => "1111111111"'nobre' => "Pedro");
    header('content-type: application/json');
    echo 
    json_encode($msg); 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  12. #12
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much!, I am really very grateful, had the problem yesterday and finally I could fix it, thanks cpradio!!!!!!!!!!!!!!!!!!!!!
    greetings from Chile

  13. #13
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  14. #14
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and fix it, thank you very much, the problem was that I had more elements in the html with the same id, thanks.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •