Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i've this two fields:

<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">

And I need to send to a php page with an ajax call.

I've this function that i use in many scripts

$("#sendSms").click(function(){
    var text = $("input[name=smsText]").val();
    var recipients = $("input[name=recipients]").val();
     var datastr ='text=' + text +'&recipients=' + recipients;
    $(over).appendTo('#box');
    $.ajax({
        type: "POST",
        url: "send-result.php",
        data: datastr,
        cache: false,
        success: function(data){
            $('#box').html(data);
        }
    });
    return false;
});

Please, i need help to modify my function to send both "smsText" and array recipients[] to other php page via Ajax...

Thank you very much!

share|improve this question
    
Hi, if u want to use 'post' method u must form the argument list like data: { name: "John", location: "Boston" }...hope it will help u... $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }).done(function( msg ) { alert( "Data Saved: " + msg ); }); see the examples here –  Ayyappan Sekar Nov 28 '12 at 15:02

5 Answers 5

up vote 1 down vote accepted

Replace your following code:

var recipients = $("input[name=recipients]").val();
var datastr ='text=' + text +'&recipients=' + recipients;

for this one:

var datastr = '';
$("input[name='recipients[]']").each(function() {
    datastr += '&recipients[]=' + $(this).val();
});
datastr ='text=' + text + datastr;

that should do what you want and cause PHP to create the array variable $_POST['recipients'] with all your values in it.

share|improve this answer
    
This line datastr += '&recipients[]=' + $(this).val(); would concatenate &recipients[]= several times, thats not required. –  Teena Thomas Nov 28 '12 at 15:01
    
@coder1984 You are wrong, it's required if you want PHP to create an array variable for all the individual recipients[] values. –  Nelson Nov 28 '12 at 15:18
    
@Nelson: perfect! This did the trick! Thank you very much to you and to the all others! –  sineverba Nov 28 '12 at 18:40

Have a look at jQuerys functions .serializeArray() and .serialize()

share|improve this answer

Try:

var datastr = '&recipients[]=';
var arr = [];
$("input[name='recipients[]']").each(function() {
  arr[] = $(this).val();
});

datastr ='text=' + text + datastr + arr;
share|improve this answer
    
Your code is invalid javascript Error: Problem at line 10 character 7: Expected an identifier and instead saw ']'. arr[] = $(this).val(); –  Nelson Nov 28 '12 at 15:16

If the fields are contained within a form, you can use jQuery's serialize() method to convert the fields into a string to send via Ajax

<form id="sms-form">
    <input type="text" name="smsText" value="text sms to send to all">
    <input type="text" name="recipients[]" value="3471234567">
    <input type="text" name="recipients[]" value="3359876543">
    <input type="text" name="recipients[]" value="3201472583">
</form>

$("#sendSms").click(function(){
    var datastr = $("form#sms-form").serialize();
    $(over).appendTo('#box');
    $.ajax({
        type: "POST",
        url: "send-result.php",
        data: datastr,
        cache: false,
        success: function(data){
            $('#box').html(data);
        }
    });
    return false;
});
share|improve this answer

Try

html

 <form name='ohForm' action="#">

     <input type="text" name="smsText" value="text sms to send to all">
     <input type="text" name="recipients[]" value="3471234567">
     <input type="text" name="recipients[]" value="3359876543">
     <input type="text" name="recipients[]" value="3201472583">
     // and others components

 </form>

javascript

          $("#sendSms").click(function(){
                var form = $("form[name='ohForm']");
                var datastr = form.serialize();
                $(over).appendTo('#box');
                $.ajax({
                    type: "POST",
                    url: "send-result.php",
                    data: datastr,
                    cache: false,
                    success: function(data){
                     $('#box').html(data);
                    }
               });
        return false;
    });

php

         print_r($_POST['data']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.