如何使用JQuery将HTML 表单字段作为数组定向传递到CodeIgniter中的数据库



这是我的视图页面:

<form role="form" id="addNewMatter" action="<?php echo base_url() ?>saveNewSupplier" 
method="post">
<input type="text" value="" name="name" id="name" />
<input type="text" value="" name="phone" id="phone" />
<input type="text" value="" name="email" id="email" />
<input type="text" value="" name="address" id="address" />
<script>
$('form').change(function() {
var input = $('form').serializeArray();
var data = "";
$( "#formdata" ).empty();
jQuery.each( input, function( i, input1 ) {
if($(this).attr("name") !== 'formdata'){
data += """ + $(this).attr("name") + ""=>" +  """ + input1.value +  "",";
}
finaldata = data.replace(/,s*$/, "");
$( "#formdata" ).val( finaldata );
//alert($(this).attr('name'));
});
});
</script>
<input type="text" value="" name="formdata" id="formdata" />
</form>

这是我的控制器页面:

function saveNewSupplier()
{
$createdby= $this->vendorId;
$createddate= date('d M Y');
$formdata = $this->input->post('formdata');
$Info = array();
$Info = array($formdata, 'createdby'=>$createdby,
'createddate'=>$createddate);
$this->load->model('user_model');
$result = $this->user_model->saveNewSupplier($Info);
}

这是我的模型页面:

function saveNewSupplier($Info)
{
$this->db->trans_start();
$this->db->insert('tbl_suppliers1', $Info);
$insert_id = $this->db->insert_id();
$this->db->trans_complete();
return $insert_id;
}

插入查询不起作用,它0显示为数组中的第一列,但它应该是我通过 POST 方法从视图传递的数组。 请帮助我修复此插入查询。谢谢。

$this->input->post()会给你一个数组的表单字段,所以你可以在不使用任何javascript的情况下做到这一点。

如果您的tbl_suppliers1表包含列"formdata"、"createdby"、"createddate":

function saveNewSupplier()
{
$createdby = $this->vendorId;
$createddate = date('d M Y');
$form = $this->input->post();
$data = array(
json_encode($form),
'createdby' => $createdby,
'createddate' => $createddate
);
$this->load->model('user_model');
$result = $this->user_model->saveNewSupplier($data);
}

如果您的tbl_supplier有"姓名"、"电话"(等(、"创建日期"列:

function saveNewSupplier()
{
$createdby = $this->vendorId;
$createddate = date('d M Y');
$form = $this->input->post();
$data = $form;
$data['createdby'] = $createdby;
$data['createddate'] = $createddate;
$this->load->model('user_model');
$result = $this->user_model->saveNewSupplier($data);
}

请记住,您的脚本方法或上述示例都不会检查有效字段。

相关内容

  • 没有找到相关文章

最新更新