我想在dbign中插入codeigniter。以基本方式完成,但想使用ajax。我也使用了带引导程序的主题。这是我的代码:
<input name="c_address" type="text" class="form-control">
<input name="c_phone" type="text" class="form-control">
<input name="c_mail" type="text" class="form-control">
<?php echo form_open('customer_add/add_new'); ?>
<button name="btn-send" id="btn-send" type="submit" class="btn btn-round btn-primary btn-lg btn-block">save</button>
<?=form_close();?>
我的脚本:
<script>
$(function() {
$('#btn-send').on('click', function () {
var value = {
'c_user': $('#c_user').val(),
'c_phone': $('#c_phone').val(),
'c_mail': $('#c_mail').val(),
};
$.ajax({
type: 'POST',
url: 'customer_add/add_new',
data: value,
cache: false,
success: function (result) {
console.log(result);
alert(result);
}
})
});
});
</script>
我的 add_new函数:
<?php
public function add_new(){
if($this->session->userdata('login') == true){
$config = array(
array(
'field' => 'c_address',
'label' => '"آدرس"',
'rules' => 'trim'
),
array(
'field' => 'c_phone',
'label' => '"شماره تماس"',
'rules' => 'trim|required'
),
array(
'field' => 'c_mail',
'label' => '"آدرس ایمیل"',
'rules' => 'trim|valid_email',
)
);
if($this->form_validation->run() == true)
{
$this->load->model('customers_model');
$add['is_add'] = $this->customers_model->add_customer();
echo $add;
}
else{
$this->load->view('home/customer_add_view');
}
}
else{
redirect('auth');
}
}
?>
如果不是ajax,它会很好用!我说,即使我通过拨打按钮的ID来编写脚本,它也不起作用。但是如果我写一个像
这样的函数function notify(){
alert('note');
}
然后把它放在按钮的onclick =&#34;通知&#34;,它工作。我不知道id选择器或jquery代码是否有问题。
我甚至无法通过按钮的ID进行简单的提醒。是因为引导程序吗?
编辑:
@DFriend:这是我的整个add_new函数:
public function add_new(){
if($this->session->userdata('login') == true){
$config = array(
array(
'field' => 'c_address',
'label' => '"آدرس"',
'rules' => 'trim'
),
array(
'field' => 'c_phone',
'label' => '"شماره تماس"',
'rules' => 'trim|required'
),
array(
'field' => 'c_mail',
'label' => '"آدرس ایمیل"',
'rules' => 'trim|valid_email',
)
);
if($this->form_validation->run() == true)
{
$this->load->model('customers_model');
$add['is_add'] = $this->customers_model->add_customer();
echo $add;
}
else{
$this->load->view('home/customer_add_view');
}
}
else{
redirect('auth');
}
它不是ajax很好用!我说,即使我通过拨打按钮的ID来编写脚本,它也不起作用。但是,如果我写一个像
这样的函数function notify(){
alert('note');
}
然后把它放在按钮的onclick =&#34;通知&#34;,它工作。我不知道id选择器或jquery代码是否有问题。
我的剧本:
$('#btnbtn').on('click', function () {
alert('hi');
)};
我的按钮:
<div class="form-group">
<button id="btnbtn" type="submit" class="btn btn-round btn-primary btn-lg btn-block">ذخیره</button>
</div>
在检查控制台错误中:
传递给getElementById()的空字符串。
问题是什么?