Codeigniter + Ajax,无法在不刷新页面的情况下多次提交表单



我试图在一个页面中完成简单的任务,并避免在每次使用ajax提交表单时刷新页面。问题是我只能向数据库提交一次更新,其余的都是控制台日志成功,而不更新数据库。

控制器

function index() {
$data['process'] = $this->Debug_model->getProcess();
$this->load->view('debug', $data);
}
function nextLine() {
$this->form_validation->set_rules('id', 'ID', 'required');
if ($this->form_validation->run() == FALSE)
{
redirect('debug','refresh');
} else {
$data = array(
'currentLine' => $this->input->post('currentLine'),
'nextLine' => $this->input->post('nextLine')
);
$id = $this->input->post('id');
$this->Debug_model->goNext($data, $id);
}
}

型号

function goNext($data, $id)
{
$this->db->where('id', $id);
return $this->db->update('tbl_process', $data);
}

Javascript

$(document).ready(function() {
finish();
});

function finish() {
setTimeout(function() {
update();
finish();
}, 200);
}

function update() {
$.getJSON("apitest", function(data) {
$("#currentLine").empty();
$("#nextLine").empty();
$.each(data.result, function() {
$("#currentLine").append(
"" + this['currentLine'] + "-" + this['deskNo'] + ""
);
$("#nextLine").append(
"" + this['nextLine'] + "-" + this['deskNo'] + ""
);
});
});
}
//btn save
$("#btnSave").click(function(e){
e.preventDefault();
var data = $('#callLine').serialize();
$.ajax({
type: 'POST',
url: "<?php echo base_url('debug/nextLine'); ?>",
data: data,
success: function() {
console.log('success');
}
});
});

查看

<h1>Current: <span id="currentLine"></span></h1>
<h2>Next: <span id="nextLine"></span></h2>
<?php foreach ($process->result() as $singleProcess) : 
$tobeCurrent = $singleProcess->currentLine + 1;
$tobeNext = $tobeCurrent + 1;
?>
<form action="" method="post" enctype="multipart/form-data" id="callLine">
<input type="hidden" name="id" value="<?php echo $singleProcess->id ?>" />
<input type="hidden" name="currentLine" value="<?php echo $tobeCurrent ?>" />
<input type="hidden" name="nextLine" value="<?php echo $tobeNext ?>" />
<button id="btnSave" class="btn-a" type="btn">NEXT LINE</button>
</form>
<?php endforeach; ?>

目标是在提交表单时不刷新页面,可以多次刷新,但仍不刷新页面。

不是在点击按钮时检测到事件,在表单CCD_ 1事件上检测它。

$("#callLine").submit(function(e){
e.preventDefault();
var data = $('#callLine').serialize();
$.ajax({
type: 'POST',
url: "<?php echo base_url('debug/nextLine'); ?>",
data: data,
success: function() {
console.log('success');
}
});
});

也将按钮类型更改为submit,而不是btn

<button id="btnSave" class="btn-a" type="submit">NEXT LINE</button>

相关内容

最新更新