Laravel - 来自Checkboxes的AJAX POST选择数组 [英] Laravel - AJAX POST Array of Choices from Checkboxes
问题描述
我有一个我想要修复的AJAX帖子,但是输入上有很多复选框,我就卡住了。
I have an AJAX Post that I'm trying to fix, but with a multitude of checkboxes on the input, I'm getting stuck.
目前,说我就是这样:
<input type="checkbox" name="billToEmail[]" value="email1@email.com">email1@email.com
<input type="checkbox" name="billToEmail[]" value="email2@email.com">email2@email.com
我有一个名为发送通知的按钮,它启动以下脚本:
And I've a button called "Send Notifications", which starts the following script:
<script>
$('.modal-footer').on('click', '.sendNotificationNow', function() {
$.ajax({
type:'POST',
url: '/shipments/sendNotifications',
data: {
'billTo': $('.billToEmail:checked').val(),
'shipTo': $('.shipToEmail:checked').val(),
'shipFrom': $('.shipFromEmail:checked').val(),
'_token': $('input[name=_token]').val(),
},
success: function(data) {
$('.errorTitle').addClass('hidden');
$('.errorContent').addClass('hidden');
if ((data.errors)) {
setTimeout(function () {
$('#sentNotifications').modal('show');
toastr.error('Validation error - Check your inputs!', 'Error Alert', {timeOut: 5000});
}, 500);
if (data.errors.title) {
$('.errorTitle').removeClass('hidden');
$('.errorTitle').text(data.errors.title);
}
if (data.errors.content) {
$('.errorContent').removeClass('hidden');
$('.errorContent').text(data.errors.content);
}
} else {
toastr.success('Successfully Sent Notifications!', 'Success Alert', {timeOut: 5000});
$('div.notificationssent').fadeOut();
$('div.notificationssent').load(url, function() {
$('div.notificationssent').fadeIn();
});
}
},
});
});
</script>
现在,我确定我的问题突然出现在顶部附近,我正在尝试将多个值转换为数据变量。我应该放除.val()以外的东西吗?
Now, I'm sure my issues are popping up near the top, where I'm trying to "translate" the multiple values into the data variables. Should I be putting something besides .val()?
我还有一些这样的字段,我需要使用多个复选框,但如果我能单独为billToEmail获得一些帮助,我肯定我可以修复余数。
I've a few more fields like this that I need to work on with the multiple checkboxes but if I can get some help for the billToEmail alone, I'm sure I can fix the remainder.
推荐答案
首先,你不需要 []
签字。因此,您的复选框html将如下所示:
First, you don't need the []
sign. So, your checkbox html will look like this :
<input type="checkbox" name="billToEmail" value="email1@email.com">email1@email.com
<input type="checkbox" name="billToEmail" value="email2@email.com">email2@email.com
其次,您需要使用foreach将复选框上的选定值推送到javascript数组变量中:
Second, you need to push selected value on checkbox into javascript array variable using foreach :
var billToEmail= [];
$("input:checkbox[name=billToEmail]:checked").each(function(){
billToEmail.push($(this).val());
});
第三,你需要使用 JSON.stringify将javascript数组转换为字符串)
。
billToEmails= JSON.stringify(billToEmail);
然后,传递 billToEmails
变量在AJAX中的数据。所以,它看起来像这样:
Then after that, pass the billToEmails
variable into your data in AJAX. So, it will look like this :
var dataString = "billTo="+billToEmails+"&shipTo="+$('.shipToEmail:checked').val()+"&shipFrom="+$('.shipFromEmail:checked').val()+"&_token="$('input[name=_token]').val();
$.ajax({
type:'POST',
url: '/shipments/sendNotifications',
data: dataString,
为了PHP可以获取数组,您需要先解码 billToEmails
字符串控制器中的json_decode。
In order to PHP can fetch the array, you need to decode the billToEmails
string first using json_decode in your controller.
$variable = json_decode($request->billTo,true);
这篇关于Laravel - 来自Checkboxes的AJAX POST选择数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!