简体   繁体   中英

$.post - array from javascript to php

I have this array in javascript:

arr = [1, "string", 3]

And this, my ajax call:

$.post("./ajax_book_client.php",
        {'client_info[]': arr},
    function(data) {
          // some stuffs here
        }
});

Here's the php excerpt:

<?php 
    $arr = $_POST['client_info'];

    // I want to access the array, indexed, like this:
    $arr[0] = 2;
    $arr[1] = "Hello";
    $arr[2] = 10000;

?>

But I get this error:

Uncaught TypeError: Illegal invocation jquery-1.8.3.min.js:2

What's the correct way to do this? Thanks!

Remove } from }); to remove syntax error.

Also no need to use [] with client_info so you can remove it.

Use:

<script>
var arr = [1, "string", 3];
$.post("./ajax_book_client.php",
        {'client_info': arr},
    function(data) {
          // some stuffs here
        }
);
</script>

ajax_book_client.php

<?php 
    $arr = $_POST['client_info'];

    echo $arr[0];
    echo $arr[1];
    echo $arr[2];
?>

extra braces, change:

$.post("./ajax_book_client.php",
        {'client_info[]': arr},
    function(data) {
          // some stuffs here
        }
});

to

$.post("./ajax_book_client.php",
        {'client_info[]': arr},
    function(data) {
          // some stuffs here
       // } <--remove this
});

You have a syntax error in your JS. Remove one } .

Remove client_info[] to client_info and extra brace

Try

<script>
var arr = [1, "string", 3];
$.post("./ajax_book_client.php",
    {'client_info': arr},
    function(data) {
      // some stuffs here
    }
);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM