表单提交在Codeigniter中不起作用

时间:2018-08-24 06:38:12

标签: php ajax codeigniter

在codeigniter框架中,当我选择“添加新客户”时,它将显示公司名称,客户邮件和手机的空白字段,并输入新添加的客户的详细信息,该表格将存储在客户表中,而剩余的潜在客户详细信息则存储在潜在客户中tables.so可以正常工作。enter image description here

当我选择已经存在的客户时,它将直接从客户表中获取客户详细信息并通过ajax显示它。该详细信息仅存储在潜在客户表中,无需存储在客户表中,因为客户详细信息已存储在客户表中。此表单未存储在潜在客户表中。该页面将重定向到空白页面,而不会显示错误enter image description here

表单视图布局:

        <div class="page-head">
      <h2 class="pull-left"><?= $page_title; ?> <span class="page-meta"><?= lang("enter_info"); ?></span> </h2>
    </div>
    <div class="clearfix"></div>
    <div class="matter">
        <div class="container">
            <?php $attrib = array('class' => 'form-horizontal'); echo form_open("lead/add");?>
                <div class="form-group"> 
                    <label for="date"><?= lang("Lead Date"); ?></label>
                    <div class="controls"> <?= form_input('lead_date', '', 'class="form-control date datechange" id="name" maxlength="80"');?></div>
                </div>      
                <div class="form-group">
                    <label for="customer"><?= lang("customer"); ?></label>
                    <?php
                        $cu[""] = lang("select")." ".lang("customer");
                        $cu["new"] = lang("new_customer");
                        foreach($customers as $customer){ 
                            $cu[$customer->id] = $customer->company && trim($customer->company) != '-' ? $customer->company .'('.$customer->block_name.','.$customer->branch_name.')' : $customer->block_name ;
                        }
                        echo form_dropdown('customer', $cu, (isset($_POST['customer']) ? $_POST['customer'] : ($inv ? $inv->customer_id : '')), 'class="customer form-control" data-placeholder="'.lang("select")." ".lang("customer").'" id="customer"');
                    ?>
                </div> 
                <div class="clearfix"></div> 
                <!--<div class="customer_new" id="customerForm" style="display:none;">--->
                <div class="form-group">
                    <label for="details"><?= lang("Company Name"); ?></label> 
                    <div class="controls">
                        <input type="text" name="company" class="form-control" id="company_name">
                    </div>
                </div> 
                <div class="form-group">
                    <label for="details" ><?= lang("Customer Email"); ?></label>
                    <div class="controls">
                        <input type="text" name="email" class="form-control" id="email"> 
                    </div>
                </div> 
                <div class="form-group">
                    <label for="details"><?= lang("Customer Mobile"); ?></label>
                    <div class="controls">
                        <input type="text" name="phone" class="form-control" id="phone"> 
                    </div>
                </div> 
                <!---</div>--->
                <div class="form-group">
                    <label for="price"><?= lang("Lead Type"); ?></label> 
                    <select name="lead_type" class="form-control" id="lead_type">
                    <option value="">Select Lead Type</option>
                    <option value="direct_call">Direct Call</option>
                    <option value="website_email">Website Email</option>
                    <option value="mail_marketing">Mail Marketing<option>
                    <option value="sales_call_team">Sales Call Team</option>
                    <option value="reference">Reference</option>
                    <option value="website_chat">Website Chat</option>
                    </select>
                </div> 
                <div class="form-group">
                    <label for="price"><?= lang("Lead Number"); ?></label>
                    <div class="controls">  
                        <?php 
                            $maxid = 0;             
                            $d=date('Y-m');                     
                            $sql="SELECT * FROM `sim_lead` WHERE `lead_date` LIKE  '%$d%'";  
                            $answer = $this->db->query($sql)->result();
                            //echo $answer[0];
                            $prev_date=count($answer);
                            //print_r($answer);
                            $id=$prev_date+1;                   
                            if($id<10&&$id!=0)
                            {
                                $num="000".$id;
                            }else if($id<100&&$id!=0){
                                $num="00".$id;
                            }else if($id<1000&&$id!=0){
                                $num="0".$id;
                            }else{
                                $num=$id;
                            }    
                        ?>
                        <input type="text" name="lead_no" id="lead_no" placeholder="Lead No" class="form-control setdate"  value="<?php echo date('M'); ?>/<?=$num;?>/<?php echo date("Y",strtotime("-1 year"));?>-<?php echo date('Y'); ?>">
                    </div>
                </div> 
                <div class="form-group">
                    <label for="price"><?= lang("Quantity"); ?></label>
                    <div class="controls"> <?= form_input('quantity', '', 'class="form-control" id="quantity"');?></div>
                </div>
                <div class="form-group">
                    <label for="price"><?= lang("Product Description"); ?></label>
                    <div class="controls"> <?= form_textarea('prod_desc', '', 'class="form-control" id="prod_desc" maxlength="255" style="height:60px;"');?></div>
                </div>   
                <div class="form-group">
                    <div class="controls"> <?= form_submit('submit', lang("add_product"), 'class="btn btn-primary"');?> </div>
                </div>
            <?= form_close();?>  
            <div class="clearfix"></div>
        </div>
        <div class="clearfix"></div>
    </div> 
    <script>
    $(document).on('change', '#customer', function(){
        // alert();         
        var cus_customer=$("#customer").val();
        // alert(cus_customer); 
         $.ajax({
            type:'GET',
            url: Site.base_url+'lead/get_customer_details',                 
            data : { 'cus_customer': cus_customer},
                dataType: 'json',
                success  : function(json){
                    //console.log(json);    
                    $('#company_name').val(json.company_name);
                    $('#email').val(json.customer_email);
                    $('#phone').val(json.customer_mobile);
                }
            });  
        });
    </script>

在控制器中:

function add()
{  
    $this->form_validation->set_rules('lead_date', $this->lang->line("lead_date"), 'required');
    $this->form_validation->set_rules('customer', $this->lang->line("customer"), 'required'); 
    $this->form_validation->set_rules('lead_type', $this->lang->line("lead_type"), 'required');
    $this->form_validation->set_rules('lead_no', $this->lang->line("lead_no"), 'required');
    $this->form_validation->set_rules('quantity', $this->lang->line("quantity"), 'required');
    $this->form_validation->set_rules('prod_desc', $this->lang->line("prod_desc"), 'required'); 
    if ($this->form_validation->run() == true) {
            $form = $this->leads_model->leadsform();
            $customer_data = $form['customer_data'];            
            $data = $form['data']; 
    }         
    if ($this->form_validation->run() == true && $this->leads_model->addLead($data,$customer_data)) { 
        $this->session->set_flashdata('message', $this->lang->line("lead_added"));
        redirect("lead");

    }  else { 
        $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error')); 
        $this->data['customers'] = $this->leads_model->getAllCustomers();
        $this->data['page_title'] = $this->lang->line("new_lead");
        $this->page_construct('lead/add', $this->data); 
    } 
}

在型号中:

    public function leadsform()
    {   
        $customer_data = array();  
        if($this->input->post('customer') == 'new')
        {
            $customer_id = 0;
            $customer_name = $this->input->post('company') ? $this->input->post('company') : $this->input->post('name');
            $customer_data = array(
                'company' => $this->input->post('company'),
                'email' => $this->input->post('email'),
                'phone' => $this->input->post('phone'), 
            ); 
            $this->db->insert('customers', $customer_data);
            $customer_id = $this->db->insert_id(); 
        }
        else 
        {
            $customer_id = $this->input->post('customer');  
            $customer_details = $this->Leads_model->getCustomerByID($customer_id); 
            $customer_name = $customer_details->company ? $customer_details->company : $customer_details->name;
        } 
        $data = array('lead_id' => $this->input->post('lead_id'),
                    'lead_date' => $this->input->post('lead_date'), 
                    'customer' => $customer_id,  
                    'lead_type' => $this->input->post('lead_type'),  
                    'lead_no' => $this->input->post('lead_no'),   
                    'quantity' => $this->input->post('quantity'), 
                    'prod_desc' => $this->input->post('prod_desc'), );                  
        return array('data' => $data, 'customer_data' => $customer_data);
    }

0 个答案:

没有答案