I have a form that is using jQuery and Ajax to post the form data, but cannot get it to send the values of inputs with the same name and square brackets.
The inputs look like this and will have different values:
<input type="text" name="service[]" value="Value 1">
Here is the var for getting the value:
var service = $("input[name=service\\[\\]]")
Then the var for collecting the data to send via Ajax looks like this:
var data = 'name=' + name.val() + '&email=' + email.val() + '&phone=' + phone.val() + '&service=' + service.val() + '&brief=' + encodeURIComponent(brief.val());
Then in the PHP script for sending the mail includes the following...
$service = $_POST['service'];
$body .= implode(' | ', $service);
So my question is, how do I get the values of the inputs with the name name=service[] and send them via Ajax.
Thanks in advance :)
Not an exact answer to your question, but there is an easier way to do this.
Wrap your input-fields in a form and make an on-submit-event. Return this event false so it's not actually submitted.
Here's the trick. Before you return the event false, do the ajax-request. Give the form an id and use the seralize-method to make a json-string.
[...]
data: $('#formIDGoesHere').serialize(),
[...]
If you post this to your PHP file as POST-variables, all your values are not available like they normally would be in $_POST.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With