Send array to PHP via Ajax

Asked

Viewed 40 times

0

I want to send a vector coming from javascript to PHP. I have the following code but it is not working. How to do?

    info = [];
    info[0] = 'thiago';
    info[1] = 'carlos';

    alert(info[0]);

    $.ajax({
        type: "GET",
        data: {info:info},
        url: "buscar.php",
        success: function(msg){
            console.log(msg);
        }
    });
  • Give a var_dump($_GET) in your php file and a console.log(msg) in your javascript and put here the return;

  • that was the return of the console. <pre class='Xdebug-var-dump' dir='Ltr'> <small>C: wamp64 www Projectjax2 Ex3 fetch.php:13:</small> <b>array</b> <i>(size=1)</i> 'info' <font color='#888a85'font>=&gt;</> <b>array</b> <i>(size=2)</i> 0 <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Thiago'</font> <i>(length=6)</i> 1 <font color='#888a85'>=&gt;</font> <small>string</small> </font color='#cc0000'>'carlos'</> <i>(length=6)</i> </pre>

  • It was the return in Success?

  • Try to recover this info variable like this: $info = json_decode( $_GET['info'] )

  • yes there was a return. However ,when I write an echo on the page where the data should be ,there is no return.

  • And as var_dump as the colleague suggested, what returns?

  • returns nothing !

Show 2 more comments

2 answers

2

Your javascript should look like this:

var info = [];
info[0] = 'thiago';
info[1] = 'carlos';

$.ajax({
    type: "GET",
    data: {info:info},
    url: "buscar.php",
    dataType: "json",
    success: function(msg){
        console.log(msg.info[0]);
    }
});

Your php like this:

echo json_encode($_GET);

To get the data on the server side use so:

$_GET['info'][0];

1


According to your javascript:

 info = [];
    info[0] = 'thiago';
    info[1] = 'carlos';

    alert(info[0]);

    $.ajax({
        type: "GET",
        data: {info:info},
        dataType : 'json', 
        url: "buscar.php",
        success: function(msg){
            console.log(msg);
        }

Your php can do so to receive the vector and follows a foreach for you to test:

<?php
    $teste = $_GET['info'];

    foreach( $teste as $key => $value ){
        echo "{$value}\n";
    }   

?>

And if you return in yours console.log()

You can paste in php simply like this:

<?php
    $teste = $_GET['info'];

    echo json_encode( $teste )  ;

?>

I hope I can help

  • I have one last question. I am passing 2 vectors in this way that you did in ajax , only that there is no return to the console when I write two echo on the php page, one with echo json_encode( $_GET['info'] ) ; and another json_encode( $_GET['info2'] ) ; . What can be done to return the 2 values

  • No need to create two echo json_encode just one and put the two inside, for example: echo json_encode( array("vetor1" => $vetor1, "vetor2" => $vetor2 ) ) there on the console you show so: console.log( data.vetor1 ) and/or console.log( data.vetor2 )

  • ok. Thank you!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.