0

im trying to submit form throgh ajax. my form contain two textbox with same name to get it as an array value. im trying to send this array textbox values with Ajax. the code i have tried is given below.

form

<input name="name"  value="name"/>
<input name="email[]"  value="[email protected]"/>
<input name="email[]"  value="[email protected]"/>
<input name="msg"  value="message"/>

Ajax script

$.ajax({
        url: "sendmail.php",
        cache: false,
        dataType: "json",
        type: "POST",
        data: {
            name: $('#ame').val(),
            email: $('[name="email[]"]').serialize(),
            msg: $('#msg').val()

        },

sendmail.php

$c_email = $_POST['email'];
foreach ( $c_emails as $cmail) {
echo $cmail
}
0

3 Answers 3

1

You should use .map()

email : $('[name="email[]"]').map(function () {
    return this.value;
}).get()
Sign up to request clarification or add additional context in comments.

Comments

1

Try this one and you will get all data of the form on next php script.

var formData = $('form').serialize();
$.ajax({
    url: "sendmail.php",
    cache: false,
    dataType: "json",
    type: "POST",
    data: formData,

In your PHP code

<?php
   $emails = $_POST['email'];
   foreach($emails as $email) {
       echo $email;
   }
?>

Comments

0

jQuery can be a bit picky about quotes. This will build the string you are looking for:

$("div#result").html($("input[name='email[]']").serialize());

This returns the empty string:

$("div#result").html($('input[name="email[]"]').serialize());

http://jsfiddle.net/n5v69bx4/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.