我有这两个领域:
<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">
我需要发送一个带有ajax调用的php页面。
我有这个功能,我在很多脚本中使用
$("#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;
});
请,我需要帮助修改我的功能,通过Ajax将“smsText”和数组收件人[]发送到其他php页面......
非常感谢!
答案 0 :(得分:1)
查看jQuerys函数.serializeArray()和.serialize()
答案 1 :(得分:1)
替换以下代码:
var recipients = $("input[name=recipients]").val();
var datastr ='text=' + text +'&recipients=' + recipients;
这个:
var datastr = '';
$("input[name='recipients[]']").each(function() {
datastr += '&recipients[]=' + $(this).val();
});
datastr ='text=' + text + datastr;
应该做你想做的事情并让PHP创建包含你所有值的数组变量$_POST['recipients']
。
答案 2 :(得分:0)
尝试:
var datastr = '&recipients[]=';
var arr = [];
$("input[name='recipients[]']").each(function() {
arr[] = $(this).val();
});
datastr ='text=' + text + datastr + arr;
答案 3 :(得分:0)
如果字段包含在表单中,您可以使用jQuery的serialize()方法将字段转换为字符串以通过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;
});
答案 4 :(得分:0)
尝试
<强> 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']);