1

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!

Nelson Benítez León
51.1k8 gold badges70 silver badges84 bronze badges
asked Nov 28, 2012 at 14:54
1
  • 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 Commented Nov 28, 2012 at 15:02

5 Answers 5

1

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.

answered Nov 28, 2012 at 14:59
Sign up to request clarification or add additional context in comments.

3 Comments

This line datastr += '&recipients[]=' + $(this).val(); would concatenate &recipients[]= several times, thats not required.
@coder1984 You are wrong, it's required if you want PHP to create an array variable for all the individual recipients[] values.
@Nelson: perfect! This did the trick! Thank you very much to you and to the all others!
1

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

answered Nov 28, 2012 at 14:57

Comments

0

Try:

var datastr = '&recipients[]=';
var arr = [];
$("input[name='recipients[]']").each(function() {
 arr[] = $(this).val();
});
datastr ='text=' + text + datastr + arr;
answered Nov 28, 2012 at 15:04

1 Comment

Your code is invalid javascript Error: Problem at line 10 character 7: Expected an identifier and instead saw ']'. arr[] = $(this).val();
0

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;
});
answered Nov 28, 2012 at 15:05

Comments

0

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']);
answered Nov 28, 2012 at 15:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.