我正在尝试通过AJAX提交表单以将我的数据插入数据库,当我按下提交按钮时数据没有插入..
$invoice_number = rand(0, 99999);
$TransactionInsert = array(
'date' => $this->input->post('funds_arrival_date'),
'description' => 'Payment against invoice #' . '<a href="/reservations/database/edit/' . $this->input->post('reservations') . '">' . $this->input->post('reservations') . '</a>' . ' via ' . '"' . $this->input->post('paid_to') . '"' . ' method',
'product' => $this->input->post('reservations'),
'credit' => $this->input->post('actual_amount'),
'credit_cur' => $this->input->post('currency_name'),
'credit_cur_val' => (int)$this->input->post('currency_value'),
'debit' => 0,
'parent_invoice' => $invoice_number
);
$this->db->insert('transactions', $TransactionInsert);
jQuery
//Finish the process, POST AJAX...
$('button[name=submit-transaction]').live("click", function (e) {
var a = getTransactionValues();
e.preventDefault();
$.ajax({
url: '/transactions/submit',
type: 'POST',
data: {
reservations: a.reservations,
amount: a.amount,
currency_value: a.currency_value,
currency_name: a.currency_name,
actual_amount: a.actual_amount,
actual_remaining: a.actual_remaining,
funds_arrival_date: a.funds_arrival_date,
paid_to: a.paid_to,
checkbox: a.checkbox
},
success: function (data) {
location.reload();
},
});
});
}
/*
* Gathering values and getting them
*/
function getTransactionValues() {
var o = {};
o.parent_id = $('.dbase_id').val();
o.reservations = [];
$('#custom-headers option:selected').each(function (i, selected) {
o.reservations[i] = $(selected).val();
});
o.amount = $('input[name=amount-price]').val();
o.currency_value = $('input[name=currency-value]').val();
o.currency_name = $('.currency_appendto option:selected').html();
o.actual_amount = $('input[name=actual-amount]').val();
o.actual_remaining = $('input[name=actual-remaining]').val();
o.funds_arrival_date = $('input[name=funds-arrival]').val();
o.paid_to = $('.paidto option:selected').html();
o.checkbox = $('.multi-transaction:checked').map(function () {
return this.value
}).get();
return o;
}
我不知道页面没有向我显示什么错误,并且数据未提交给数据库
答案 0 :(得分:1)
我可能错了,但似乎你的o.reservations = [];
部分正在返回一些东西,在你的PHP代码中你没有循环它并尝试插入一个你需要循环的批量$this->input->post('reservations');
这个或访问第一个/第二个密钥..无论你需要插入什么$this->input->post('reservations')[0];
尝试:
$TransactionInsert = array(
'date' => $this->input->post('funds_arrival_date'),
'description' => 'Payment against invoice #' . '<a href="/reservations/database/edit/' . $this->input->post('reservations')[0] . '">' . $this->input->post('reservations')[0] . '</a>' . ' via ' . '"' . $this->input->post('paid_to') . '"' . ' method',
'product' => $this->input->post('reservations')[0],
'credit' => $this->input->post('actual_amount'),
'credit_cur' => $this->input->post('currency_name'),
'credit_cur_val' => (int)$this->input->post('currency_value'),
'debit' => 0,
'parent_invoice' => $invoice_number
);
答案 1 :(得分:1)
我也看到url中的ajax调用中的基本url不正确,我将分享我通常使用的ajax调用
我假设您使用提交方法
调用事务控制器 $("#your_html_item_id").bind("click",function()
{
var target_url = '<?php echo(base_url()."transactions/submit") ; ?>';
$.ajax
(
{
url : target_url,
type: "POST",
cache: false,
data:
{
key1: value1,
key2: value2,
key3: value3
},
success: function(data)
{
alert(1);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("error during database query");
}
});
});
如果您看到一个消息框为1,则表示ajax调用成功
答案 2 :(得分:0)
您应该检查您的php错误日志和数据库错误日志。
如果您不知道在哪里找到它......您可以谷歌搜索:)