【发布时间】:2017-11-02 16:31:48
【问题描述】:
通过jQuery/ajax将数据插入数据库后,在不刷新页面的情况下从数据库中获取值如何使用codeigniter显示数据库值?
这是我的代码:
脚本:
<script>
$(document).ready(function(){
$("#personal-info").submit(function(e){
e.preventDefault();
var suppid = $("#suppid").val();
var proid = $("#proid").val();
var custid = $("#custid").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/Profile_cntrl/buyer_communication",
data: {suppid:suppid,proid:proid,custid:custid,message:message},
success:function(data)
{
alert('SUCCESS!!');
},
error:function()
{
alert('fail');
}
});
});
});
</script>
控制器:
public function buyer_communication() {
$result1 = $this->Profile_model->fetch_Data($product_id);
$Userid = $this->session->userdata('id');
$result3 = $this->session->userdata('tt');
$data4 = array(
'message' => $this->input->post('message'),
'supplier_id' => $this->input->post('suppid'),
'product_id' => $this->input->post('proid'),
'Customer_id' => $this->input->post('custid'),
'From' => $result3,
);
$this->Profile_model->buyer_insert($data4);
redirect('welcome/buyermessageview?id=' . $product_id);
}
型号:
function buyer_insert($data4) {
$this->db->insert('communication', $data4);
return ($this->db->affected_rows() != 1) ? false : true;
}
表格:
<form class="form-horizontal" method="POST" id="personal-info" role="form" action="#">
<div class="panel-footer">
<div class="input-group">
<input type ="hidden" name="suppid" id="suppid" value="<?php echo $row->supplier_id; ?>" class="form-control" />
<input type ="hidden" name="proid" id="proid" value="<?php echo $row->product_id; ?>" class="form-control" />
<input type ="hidden" name="custid" id="custid" value="<?php echo $row->Customer_id; ?>" class="form-control" />
<input id="message" name="message" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." />
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="submit-p" name="submit-p">Send</button>
<!--<input type="submit" name="submit-p" id="submit-p" value="send" class="btn btn-primary btn-sm" >-->
</span>
</div>
</div>
</form>
【问题讨论】:
-
stackoverflow.com/questions/44305553/… 你们两个在做同一件事吗?最好在同一问题上保留一个问题。并且请您的朋友接受另一个答案,因为它解决了原来的问题。无论如何,您没有正确描述这一点。你的代码出了什么问题?它在什么时候失败?你有什么错误,如果有的话?什么行为是错误的?
-
P.S.为什么,在 ajax 中,当您可以序列化整个表单时,您要手动从表单字段中提取值?
data: $(this).serialize()应该可以代替所有那些重复的变量声明。 api.jquery.com/serialize -
在将值插入数据库同时从数据库中获取值而不刷新页面值后应显示在表单中。
-
表单代码我也只添加了一次参考并建议我
标签: php jquery html ajax codeigniter