2013-03-02 55 views
0

我有一個允許我將新客戶添加到數據庫表的表單。codeigniter表單驗證和ajax查找以查看名稱是否存在

customer_name字段是唯一字段。因此我想在嘗試寫入數據庫之前在輸入字段上執行表單驗證。

理想情況下,驗證可以是兩倍。如果用戶輸入客戶名稱時可用名稱,則顯示ajax查找。其次,codeigniter的驗證功能?應該這可能不可用提交查詢可以做出來看看是否存在,然後相應地處理?

我已經爲一個完整的例子進行了廣泛的搜索和搜索,但沒有遇到任何問題。試了幾次都沒有運氣。不幸的是我的Java腳本和jquery技能不存在,並試圖感受我的代碼點火器。

我有審查和嘗試下面的文章: CodeIgniter - Checking to see if a value already exists in the database http://www.joshuawinn.com/check-if-email-username-exists-with-codeigniter-and-jquery-validation/

我的代碼目前是:

jQuery的鏈接:

<head> 
    <meta charset="utf-8"> 
    <title><?php echo $title; ?></title> 
    <link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"> 
    <link href="<?php echo base_url(); ?>styles/menu.css" type="text/css" rel="stylesheet"> 
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script> 
    <script type="text/javascript"> 

    </script> 
</head> 

我的控制器:

function create_customer() 
    { 

     $this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean"); 
     $this->form_validation->set_rules("address_line_1","`Address Line 1`","required|xss_clean|min_length[6]"); 
     $this->form_validation->set_rules("address_line_2","`Address Line 2`","xss_clean|min_length[6]"); 
     $this->form_validation->set_rules("suburb","`Suburb`","required|xss_clean|min_length[6]"); 
     $this->form_validation->set_rules("city","`City`","required|xss_clean|min_length[6]"); 
     $this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[4]|max_length[5]"); 
     $this->form_validation->set_rules("primary_contact_name","`Contact Person Name`","required|xss_clean|min_length[6]"); 
     $this->form_validation->set_rules("primary_contact_email","`Contact Person email`","required|valid_email|xss_clean"); 
     $this->form_validation->set_rules("primary_contact_tell","`Contact Person tell`","required|xss_clean|min_length[10]|max_length[14]"); 

     if ($this->form_validation->run() == FALSE){ 
      $data["message"]=""; 

      $data['title']="Master Data Home Page"; 
      $this->load->view("master_data/view_master_data_header",$data); 
      $this->load->view("master_data/view_master_data_nav"); 
      $this->load->view("master_data/view_content_master_data_create_customer"); 
      $this->load->view("master_data/view_master_data_footer"); 
     } else { 

     $data = array(
      'customer_name' => $this->input->post('customer_name'), 
      'address_line_1' => $this->input->post('address_line_1'), 
      'address_line_2' => $this->input->post('address_line_2'), 
      'suburb' => $this->input->post('suburb'), 
      'city' => $this->input->post('city'), 
      'postalcode' => $this->input->post('postalcode'), 
      'primary_contact_name' => $this->input->post('primary_contact_name'), 
      'primary_contact_email' => $this->input->post('primary_contact_email'), 
      'primary_contact_tell' => $this->input->post('primary_contact_tell'), 
      ); 

     $this->model_master_data->add_record($data); 

     $this->customer_created_successfully(); 
      } 
     } 

和我的觀點:

<?php 
echo validation_errors(); 
?> 
<br> 
<?php 
    echo form_open('masterdata/create_customer'); 
?> 

<table> 
    <tr> 
     <td valign="top"> 

<?php 

    echo form_label("Customer Name", "customer_name"); 
    $data= array(
    "name"=>"customer_name", 
    "id"=>"customer_name", 
    "value"=>set_value("customer_name")); 
    echo form_input($data); 

    echo form_label("Contact Name", "primary_contact_name"); 
    $data= array(
    "name"=>"primary_contact_name", 
    "id"=>"primary_contact_name", 
    "value"=>set_value("primary_contact_name")); 
    echo form_input($data); 

     echo form_label("Contact Email", "primary_contact_email"); 
    $data= array(
    "name"=>"primary_contact_email", 
    "id"=>"primary_contact_email", 
    "value"=>set_value("primary_contact_email")); 
    echo form_input($data); 


    echo form_label("Contact Tel", "primary_contact_tell"); 
    $data= array(
    "name"=>"primary_contact_tell", 
    "id"=>"primary_contact_tell", 
    "value"=>set_value("primary_contact_tell")); 
    echo form_input($data); 

    ?> 
    </td> 
    <td width="20"> 
    <td valign="top"> 
    <?php 

    echo form_label("Address Line 1", "address_line_1"); 
    $data= array(
    "name"=>"address_line_1", 
    "id"=>"address_line_1", 
    "value"=>set_value("address_line_1")); 
    echo form_input($data); 

    echo form_label("Address Line 2", "address_line_2"); 
    $data= array(
    "name"=>"address_line_2", 
    "id"=>"address_line_2", 
    "value"=>set_value("address_line_2")); 
    echo form_input($data); 

    echo form_label("Suburb", "suburb"); 
    $data= array(
    "name"=>"suburb", 
    "id"=>"suburb", 
    "value"=>set_value("suburb")); 
    echo form_input($data); 

    echo form_label("City", "city"); 
    $data= array(
    "name"=>"city", 
    "id"=>"city", 
    "value"=>set_value("city")); 
    echo form_input($data); 

    echo form_label("Postal Code", "postalcode"); 
    $data= array(
    "name"=>"postalcode", 
    "id"=>"postalcode", 
    "value"=>set_value("postalcode")); 
    echo form_input($data); 

    ?> 
    </td> 
     </tr> 
      </table> 
<br> 
    <p> 
     <input type="submit" value="Add New Record"> 
    </p> 
<br> 
<?php 
    echo form_close(); 
?> 

任何意見是非常感謝一如既往。如果你知道一個完整的工作示例或教程,我可以遵循,請告知。再次

感謝,

回答

1

確定這會給你暗示

 <script type="text/javascript" > 
     function registerForm(id) 
    { 
     $('#user_error').html('');  


    var name  = $.trim($('#name').val()); 


    if(name=='') 
    { 
     $('#user_email_error').html('All fields are required!'); 
     return false; 
    } 


    $.ajax({ 
    type : 'POST', 
    data : $('#form-register').serialize(), 
    url  : '<?=base_url()?>login/register_user', 
    cache : false,  
    success: function(data){ 
     if(data=='name') 
     { 
      $('#user_error').html('This name is already in use try new one!'); 
      return false; 
     } 



     else 
     {     
      document.location.href='<?=base_url()?>login'; 
     } 
     } 

     }); 

    return false; 

    } 
     </script> 

這一個在你的位指示

 function register_user() 
{ 
    $str = ''; 

    $user_email = $this->input->post('email'); 
    $name= $this->input->post('name'); 
    $data_a['name']   = $name; 

     $result1  =  $this->login_model->check_username($name); 

     if($result1->num_rows() > 0) 
     { 
      $str = 'name'; 
     } 


    else 
    { 
     $this->login_model->add_user($data_a); 

     $str = 'goo'; 


    } 
    echo($str); 


    exit; 
} 

這個觀點

 <div id="user_error" style="color:#FF0000; font-size:13px; font-weight:bold"></div> 
    <form method="post" action="" id="form-register" title="Register" onSubmit="return registerForm(this)"> 


      <input type="text" name="name" id="name" value="" class="input-unstyled" placeholder="Your name" autocomplete="off"> 



        <button type="submit" id="send-register">Register</button> 
      </form> 
+0

感謝。是函數中內置的'check_email_user'和'check_username',還是你有模型語法?我假設第一塊代碼是在我的視圖的標題中出現?再次感謝。 – Smudger 2013-03-02 11:55:48

+0

我有編輯代碼jst忽略check_email_user bt check_username是我在控制器中檢查用戶的函數。你會讓它取決於你的桌子 – 2013-03-02 13:08:26

相關問題