Home ajax How to send an array to jquery ajax?

How to send an array to jquery ajax?

Author

Date

Category

Here is a snippet of my code:

$ .ajax ({
    url: 'ajax / ajax.feedback.php',
    type: 'POST',
    data: 'author =' + author + '& amp; mail =' + mail + '& amp; subject =' + subject + '& amp; text =' + text +
      '& amp; checkbox [1] =' + checkbox [1] + '& amp; checkbox [2] =' + checkbox [2] + '& amp; checkbox [3] =' + checkbox [3] + '& amp; checkbox [ 4] = '+ checkbox [4] +' & amp; checkbox [5] = '+ checkbox [5] +
      '& amp; checkbox [6] =' + checkbox [6] + '& amp; checkbox [7] =' + checkbox [7] + '& amp; checkbox [8] =' + checkbox [8] + '& amp; checkbox [ 9] = '+ checkbox [9],
    success: ...

How easy is it to do? Just send an array? (and how to read it later in php? if there are any peculiarities) In Google – it’s not clear – you need to create an object, use json. In practice, nothing comes of it.


Answer 1, authority 100%

http://api.jquery.com/jQuery.post/

there is an example

$. post ("test.php", {'choices []': ["Jon", "Susan"] });

for your case there should be something like this:

$. post ('ajax / ajax.feedback.php',
  {'author': author, 'mail': mail, 'subject': subject, 'text': text, 'checkbox []': checkbox},
  function (html) {success ...
  });

and even easier, you can do this:

$. post ("test.php", $ ("# testform"). serialize ());

on the server, the whole thing will be in the $ _POST array;
it is enough to display it on the screen print_r ($ _ POST) and everything will become clear


Answer 2, authority 33%

Here’s an example using JSON:

var author = document.getElementById ("author"). value;
...
$ .ajax ({
  url: 'ajax / ajax.feedback.php',
  type: 'POST',
  data: ({author: author, id: id, mail: mail}),
  success: function (data) {}
});

data is received via the $ _POST array


Answer 3, authority 33%

As always, there are options:
1. Glue everything into a line with a separator (change all occurrences of the separator in the line to html equivalents) and send everything in one parameter. And on the server do explode (); .
2. Read here – http: //www.simplecoding .org / otpravka-dannyx-v-formate-json-s-pomoshhyu-javascript-i-jquery.html . It seems like everything is written.
Basically, the methods are similar.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions