0
首先,我想將數據從數據庫顯示到VIEW一個表中,然後如果有更改,用戶將只需雙擊表格並輸入新的值。 here is the screenshot:如何使用ajax將新jQuery值傳遞給CI控制器
我與顯示和編輯,我嘗試接下來要做的是保存新的數據/值,以便我可以將它傳遞給控制器來完成。
這裏是我的數據庫獲取數據,並使用jquery單擊按鈕前進它顯示錶代碼..
<script type="text/javascript">
var leads = Array();
var lead_count = 0;
<?php $count = 0; foreach($agent_leads as $info):?>
leads[<?php echo $count; ?>] = {"fullname": "<?php echo $info['fullname'] ?>",
"gender" : "<?php echo $info['gender'] ?>",
"address": "<?php echo $info['address'] ?>",
"city" : "<?php echo $info['city']; ?>",
"state" : "<?php echo $info['state']; ?>",
"zipcode" : "<?php echo $info['zipcode']; ?>",
"email": "<?php echo $info['email'] ?>"
};
<?php $count++; endforeach; ?>
// console.log(lead_count)
// console.log(leads[lead_count])
if(lead_count == 0)
{
var append = '';
append += '<tr>';
append += '<td><div contenteditable>'+leads[lead_count].fullname+'</div></td>';
append += '<td><div contenteditable>'+leads[lead_count].gender+ '</div></td>';
append += '<td><div contenteditable>'+leads[lead_count].address+'</div></td>';
append += '<td><div contenteditable>'+leads[lead_count].city+ '</div></td>';
append += '<td><div contenteditable>'+leads[lead_count].state+ '</div></td>';
append += '<td><div contenteditable>'+leads[lead_count].zipcode+'</div></td>';
append += '<td><div contenteditable>'+leads[lead_count].email+ '</div></td>';
append += '<td><input type="text" id="status"/></td>';
append += '<td><input type="number" id="qty"/></td>';
append += '<td><input type="text" id="comment"/></td>';
append += '<td><button class="btn btn" id="sub" type="submit">Submit</button></td>';
append += '</tr>';
$('#leads_info').html(append);
}
$(document).on('click', '#move_on', function(){
var order = Number($(this).attr('data-order')) + 1;
var append = '';
append += '<tr>';
append += '<td>'+leads[order].fullname+'</td>';
append += '<td>'+leads[order].gender+'</td>';
append += '<td>'+leads[order].address+'</td>';
append += '<td>'+leads[order].city+'</td>';
append += '<td>'+leads[order].state+'</td>';
append += '<td>'+leads[order].zipcode+'</td>';
append += '<td>'+leads[order].email+'</td>';
append += '<td><input type="text" id="status"/></td>';
append += '<td><input type="number" id="qty"/></td>';
append += '<td><input type="text" id="comment"/></td>';
append += '<td><button class="btn btn-success" id="sub" type="submit">Submit</button></td>';
append += '</tr>';
$(this).attr('data-order', order);
$('#leads_info').html(append);
});
那麼這裏是我的AJAX代碼..
$(document).on('click', '#sub', function(){
var infos = Array();
infos['i'] = {
lead:leads[lead_count],
status: $('#status').val(),
qty: $('#qty').val(),
comment: $('#comment').val()
}
$.ajax({
url: window.location + '/sales_report',
type: 'POST',
data: infos,
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('error');
},
success: function (result) {
console.log('success');
}
});
});
但是當我點擊提交傳遞所有的數據到控制器我得到「錯誤」在console.log()
我是新來jquery和ajax。我不太瞭解如何將數組從視圖傳遞到控制器,但是我對如何從模型 - >控制器獲取數據知道的很少。如何將表中的新值傳遞給控制器?
是的,我已經做到了!但我只是把這個代碼'console.log('error');'所以我會知道它無法通過,但字面上我不知道爲什麼它無法通過。如何獲取錯誤消息? – justAbeginner
@justAbeginner console.log(textStatus);的console.log(errorThrown);的console.log(XMLHttpRequest的); – grinat
我得到這個錯誤'對象{readyState的:4,setRequestHeader:阿賈克斯/ jqXHR.setRequestHeader(),getAllResponseHeaders:阿賈克斯/ jqXHR.getAllResponseHeaders(),getResponseHeader:阿賈克斯/ jqXHR.getResponseHeader(),overrideMimeType:阿賈克斯/ jqXHR.overrideMimeType(),abort:.ajax/jqXHR.abort(),done:jQuery.Callbacks/self.add(),失敗:jQuery.Callbacks/self.add(),進度:jQuery.Callbacks/self.add (),狀態:.Deferred/promise.state(),13更多...}' – justAbeginner