如何使用JQuery

时间:2018-10-07 08:10:29

标签: jquery mysql arrays 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方法传递的数组。 请帮助我修复此插入查询。谢谢。

1 个答案:

答案 0 :(得分:0)

$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);
}

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