Kevin Lloyd Bernal
Kevin Lloyd Bernal

Reputation: 363

$.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!

Upvotes: 0

Views: 5824

Answers (4)

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

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>

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

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
});

Upvotes: 1

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

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];
?>

Upvotes: 2

Leo Nyx
Leo Nyx

Reputation: 696

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

Upvotes: 0

Related Questions