0

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!

4 Answers 4

2

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];
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.