0

class email is an array of email addresses. How do i get the array in jQuery and then send the data with AJAX to send.php along with class title and message?

    <input type='text' size='30' class='email' value='[email protected]' />
    <input type='text' size='30' class='email' value='[email protected]' />
    <input type='text' size='30' class='email' value='[email protected]' />
    <input type='text' size='30' class='title' value='testing title' />
    <input type='text' size='30' class='message' value='testing message' />

<script type="text/javascript">
$(function() {
var title = $('.title').val();
var message = $('.message').val();

var dataString = 'title=' + title + '&message=' + message;
$.ajax({
    type: "POST",
    url: "send.php",
    data: dataString,
success:function () {


}

});
});
</script>

send.php

<?php


  $email = $_POST['email'];
  $title = $_POST['title'];
  $message = $_POST['message'];

foreach($email as $value) {

//send email

}
?>

3 Answers 3

1

You can do it more simply that Felix King's answer, using serialize(), if you name your inputs:

<form id="the-form">
    <input type='email' size='30' name="email[]" value='[email protected]' />
    <input type='email' size='30' name="email[]" value='[email protected]' />
    <input type='email' size='30' name="email[]" value='[email protected]' />
    <input type='text' size='30' name='title' value='testing title' />
    <input type='text' size='30' name='message' value='testing message' />
</form>
$.ajax({
    type: "POST",
    url: "send.php",
    data: $("#the-form").serialize(),
    success: function() {

    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

havent tested this but it is a massive time saver. I've been using longer methods to get values, when all i had to do is give the form an id and use serializearray?
@user892134: I meant to put serialize. Edited.
1

This should do it:

var data = {
    title: $('.title').val(),
    message: $('.message').val(),
    email: $('.email').map(function(){ return $(this).val(); }).get()
};

$.ajax({
    type: "POST",
    url: "send.php",
    data: data,
    success:function () {

    }
});

data.email will contain an array of email addresses. jQuery takes care to encode the data properly. See jQuery.param.

1 Comment

so $email = $_POST['email']; will contain an array?
0

Won't this work?:

var datastring = $('#yourform').serialize();

1 Comment

Not until the input elements have names.

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.