我很想知道是否可以从创建表单上的单独表中提取详细信息。
例如,如果我有各种各样的字段,而且其中一个字段是用户列表的下拉列表,我想在其中显示其详细信息(来自表格中的字段)根据我从下拉列表中选择的选项。
我假设它可以通过AJAX实现,但我并不完全确定它是一种创造形式和所有。
有人有任何建议吗?
与选择框相关的html / php的一部分
<div class="box-body">
<select name="bill_to" required class="js-example-basic-single" style="width:100%;" id="bill_to" onchange="invoicedue(event);">
<option></option>
@foreach($customers as $customer)
<option value="{{ $customer->id }}">{{ $customer->customer_name }}</option>
@endforeach
</select>
{{ csrf_field() }}
的JavaScript
<script>
function billtodetails(e){
$('#bill_to').change(function() {
$.ajax({
type:'POST',
url: '/customers/customerDetails',
data: {
'customerID': $('select[name=bill_to]').val(),
'_token': $('input[name=_token]').val(),
},
success: function(data) {
$('.errorTitle').addClass('hidden');
$('.errorContent').addClass('hidden');
if ((data.errors)) {
setTimeout(function () {
$('#createOrigin').modal('show');
toastr.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 added Origin!', 'Success Alert', {timeOut: 5000});
$('#existing_biller_details').append(
'<p>'+data['customer_name']+'</p><p>'+data['billing_address_1']+'</p>';
);
}
},
});
});
});
</script>
根据评论建议编辑版本:
<script>
$(function(){
$('#bill_to').change(function() {
$.ajax({
type:'POST',
url: '/customers/customerDetails',
data: {
'customerID': $('select[name=bill_to]').val(),
'_token': $('input[name=_token]').val(),
},
success: function(data) {
$('.errorTitle').addClass('hidden');
$('.errorContent').addClass('hidden');
if ((data.errors)) {
setTimeout(function () {
$('#createOrigin').modal('show');
toastr.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 added Origin!', 'Success Alert', {timeOut: 5000});
$('#existing_biller_details').append(
'<p>'+data['customer_name']+'</p><p>'+data['billing_address_1']+'</p>';
);
}
},
});
});
});
</script>
使用工作脚本进行更新:
<script>
$(function(){
$('#bill_to').change(function() {
$.ajax({
type:'POST',
url: '/customers/customerDetails',
data: {
'customerID': $('select[name=bill_to]').val(),
'_token': $('input[name=_token]').val(),
},
success: function(data) {
$('.errorTitle').addClass('hidden');
$('.errorContent').addClass('hidden');
if ((data.errors)) {
setTimeout(function () {
$('#createOrigin').modal('show');
toastr.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('Changed Bill To!'+data.customer_name, 'Success Alert', {timeOut: 5000});
$('#existing_biller_details').append(
'<p>'+data.customer_name+'</p><p>'+data.billing_address_1 +'</p>');
}
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Unexpected error.');
}
}
});
});
});
</script>
从响应有效负载返回:
[[{"id":3,"company_name":"Power Equipment Company","first_name":"","last_name":"","account_type":1,"shipping_address_1":"","shipping_address_2":"","shipping_city":"","shipping_state":"","shipping_zipcode":"","billing_address_1":"","billing_address_2":"","billing_city":"","billing_state":"","billing_zipcode":"","primary_phone":"","primary_fax":"","old_account_number":"","website":"","created_at":"2017-11-20 19:41:52","updated_at":"2017-11-20 19:41:52","customer_name":"Power Equipment Company","origin":null,"primary_email":null,"created_by":0}]]
答案 0 :(得分:0)
基于以下评论来自用户@charlietfl的问题(非常感谢他),该脚本已更正为提供错误报告并从报告中提取数据,但重点是作为GET请求而不是POST请求,以及根据上一条评论中的建议添加dataType。
<script>
$(function(){
$('#bill_to').change(function() {
$.ajax({
type:'GET',
url: '/customers/customerDetails',
dataType:'json',
data: {
'customerID': $('select[name=bill_to]').val(),
'_token': $('input[name=_token]').val(),
},
success: function(data) {
$('.errorTitle').addClass('hidden');
$('.errorContent').addClass('hidden');
if ((data.errors)) {
setTimeout(function () {
$('#createOrigin').modal('show');
toastr.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('Changed Bill To!'+data.customer_name, 'Success Alert', {timeOut: 5000});
$('#existing_biller_details').append(
'<p>'+data.customer_name+'</p><p>'+data.billing_address_1 +'</p>');
}
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 500) {
alert('Internal error: ' + jqXHR.responseText);
} else {
alert('Unexpected error.');
}
}
});
});
});
</script>