2013-03-14 128 views
0
<?PHP 
$attributes = array('class' => "profile_form", 'id' => "profile_form",'name'=>"profile_form"); 
echo form_open_multipart("$form_action",$attributes); 
?> 
<LABEL for='email'>E-Mail</LABEL> 
<INPUT type='text' id='email' name="email" class="w200" value='<?PHP echo $email;?>' remote="<?PHP echo base_url()."php/unique_email.php?id=$id"; ?>"> 
<DIV class='ca clr-lr'> 
<input type="submit" id="submitbutton" name="submitbutton" value="<?PHP echo $title;?>" class="pr mbutton"/> 
</DIV> 
</FORM> 

JQuery驗證遠程驗證只有場改變

rules: { 
     email: { 
      required:true, 
      email:true 
     } 
    } 

這是我使用的驗證電子郵件字段代碼工作。如果我更改電子郵件字段或鍵入新值,它工作正常,但如果我只是提交表單,它會給我一個錯誤。經過簡短的檢查後,我發現如果我不編輯電子郵件字段,提交按鈕不包含在發佈數據中。

我做錯了什麼,請幫助。

這裏是我的控制器代碼,它可能有助於擴大調試範圍。我正在檢查submitbutton是否是發佈數據的一部分。

if($this->input->post('submitbutton') !== false){ 
     if ($this->form_validation->run('admin_profile_form') == FALSE){ 
      $form=(array)$this->input->post(); 
      $data=array_merge($form,$data); 
     }else{ 
      //database fields 
      $database = array(
       'first_name' => $this->input->post('first_name'), 
       'last_name' => $this->input->post('last_name'), 
       'email' => $this->input->post('email'), 
       'phone' => $this->input->post('mobile') 
      ); 
      $user_id=$user->id; 
      $this->db->where('id',$user_id); 
      $this->db->update('users',$database); 
      $this->session->set_flashdata('message_type', 'info'); 
      $this->session->set_flashdata('message', 'Profile updated'); 
      redirect($this->homepage); 
      return; 
     } 
    } 

最後,Fabio Antunes建議的解決方案工作。

我改變了規則如下,並從輸入字段遠程刪除內嵌

 email: { 
      required:true, 
      email:true, 
      remote: { 
       url: "php/unique_email.php", 
       async: false, 
       type: "post", 
       data: { id: $('#edit_id').val() } 
      } 
     } 

回答

-1

試試設置

$(/*your form*/).validate({ 
    onsubmit: true 
}) 
+0

請不要介意我問,但這會做什麼。我要求確保它不會代碼的其他部分。 – 2013-03-14 10:43:10

+0

這將永遠驗證您的表單提交,我希望這是你想要的 – Cris 2013-03-14 10:43:36

+0

它沒有工作:( – 2013-03-14 11:00:29

1

編輯

至於你說你的代碼已經工作你可以把這個你的js文件 你必須使用這個功能$("#profile_form").validate({})所以它v在提交併在keyup事件alidates

<script> 
$().ready(function() { 

     // validate signup form on keyup and submit 
     $("#profile_form").validate({ 
    rules: { 
     email: { 
      email: true 
     } 
    }, 
    messages: { 
     email: { 
      email: "Please provide a valid email" 
     } 
    } 
}); 
}); 

我注意到,在你看來,你有這樣的

<INPUT type='text' id='email' name="email" class="w200" value='<?PHP echo $email;?>' remote="<?PHP echo base_url()."php/unique_email.php?id=$id"; ?>"> 

而是把這個

<INPUT type='text' id='email' name="email" class="w200" value='<?PHP echo $email;?>' remote="<?PHP echo base_url()."php/unique_email.php?id=".$id; ?>"> 

如果您注意你的$ id在字符串內

+0

事情是我的JavaScript代碼是在一個.js文件,這就是爲什麼我使用遠程屬性。爲php和html標籤使用大寫字母正是我感覺舒服的地方,沒有什麼特別的。你的代碼看起來就像我的另一個版本,但我不確定類型:post。你能否在我的代碼中指出錯誤,我將非常感激。提前致謝。我的代碼工作正常,如果我在字段中輸入的東西,它會導致問題,當我提交表單時,即使有一個有效的值,而不輸入任何字段(即遠程驗證發射) – 2013-03-14 11:05:04

+0

我編輯了我的答案 – 2013-03-14 11:11:36

+0

兩個版本,即嵌入字符串中的變量否則會在瀏覽器中產生相同的輸出。但無論如何,我嘗試了你的建議,但現在仍然運氣。我已經添加了我的控制器代碼,負責處理我的問題。也許我在那裏做錯了什麼。 – 2013-03-14 11:23:58