How to pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?

前端 未结 4 546
盖世英雄少女心
盖世英雄少女心 2020-11-30 01:29

How can I pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?

Please show an example of code that would do the

4条回答
  •  时光说笑
    2020-11-30 02:18

    This is fairly straightforward. In your JS, all you would do is this or something similar:

    var array = ["thing1", "thing2", "thing3"];
    
    var parameters = {
      "array1[]": array,
      ...
    };
    
    $.post(
      'your/page.php',
      parameters
    )
    .done(function(data, statusText) {
        // This block is optional, fires when the ajax call is complete
    });
    

    In your php page, the values in array form will be available via $_POST['array1'].

    references

    • jQuery post()
    • jQuery ajax()

提交回复
热议问题