2015-01-21 36 views
1

我正在使用Codeigniter框架並希望使用jQuery Tag-it!爲我的用戶創建一個「EducationLevel」輸入表單。我有標籤的UI - 它的工作,但我無法將用戶輸入到一個PHP數組。tagit:輸入不是以數組的形式出現

的JavaScript代碼:

$('#educationLevel').tagit({ 
 
\t \t showAutocompleteOnFocus :true, 
 
\t \t allowSpaces :true, 
 
\t \t availableTags : PROJECTS['configs']['education_level'], 
 
\t \t afterTagAdded : function(event, ui) { 
 
\t \t \t tagitCallback('education_level', '#educationLevel'); 
 
\t \t }, 
 
\t \t afterTagRemoved : function(event, ui) { 
 
\t \t \t tagitCallback('education_level', '#educationLevel'); 
 
\t \t } 
 
\t });

HTML代碼:

<li><?php echo form_error('education_level'); ?> 
 
\t <label>Intended Education Level Tag: <i> # Only a Search Tag #</i></label> 
 
\t <input type="hidden" name="education_level" value=""></input> 
 
\t <ul id='educationLevel'> </ul> 
 
</li>

通過這個代碼我能夠獲得相關表單中輸入的選項。

但是,在選擇任何選項並提交我的表單時,出現錯誤。在調試時,我注意到查詢中填充了單詞「標籤」。這個詞不應該在那裏,並導致錯誤。

INSERT INTO `profile` (`name`, education_level, tags, institution) VALUES (xyz, abc, pqr)

我的控制器功能。

\t public function post() { 
 
\t \t if($this->profile_service->addProfile()) { 
 
\t \t \t $datas['add']['result_msg'] = 'DONE ! :)'; 
 
\t \t } else { 
 
\t \t \t $datas['add']['result_msg'] = 'FAILED ! :('; 
 
\t \t } 
 
\t \t $datas = array_merge($datas,$this->_getDatas()); 
 
\t \t $datas['add']['sprofileContent'] = $this->input->post(); 
 
\t \t $this->_type = 'add'; 
 
\t \t $this->_setDatas($datas); 
 
\t \t $this->_view(); 
 
\t }

規範服務

public function addProfile() { 
 
\t \t $this->form_validation->set_rules($this->config->item('profile/add')); 
 
\t \t if ($this->form_validation->run() == false) { 
 
\t \t \t return false; 
 
\t \t } else { \t \t \t 
 
\t \t \t try { 
 
\t \t \t \t $params = $this->input->post(); 
 
\t \t \t \t return $this->profile_model->insertProfile($params); 
 
\t \t \t } catch (Exception $e) { 
 
\t \t \t \t return false; 
 
\t \t \t } 
 
\t \t } 
 

 
\t }

代碼型號:

\t public function insertProfile($_params) { 
 
\t \t $_params['enabled_flg']  = 1; 
 
\t \t $_params['insert_datetime'] = date('Y-m-d H:i:s'); 
 
\t \t $_params['operator']  = ''; 
 

 
\t \t $_params['education_level'] = $this->checkAndImplode($_params, 'education_level'); 
 
\t \t $this->db_slave->trans_begin(); 
 
\t \t try { 
 
\t \t \t $this->db_slave->insert(self::TABLE_NAME,$_params); 
 
\t \t \t if ($this->db_slave->trans_status() === FALSE) { 
 
\t \t \t \t $this->db_slave->trans_rollback(); 
 
\t \t \t \t return false; 
 
\t \t \t } \t 
 
\t \t } catch (Exception $e) { 
 
\t \t \t $this->db_slave->trans_rollback(); 
 
\t \t \t return false; 
 
\t \t } 
 
\t \t $this->db_slave->trans_commit(); 
 
\t \t return true; 
 
\t }

代碼CheckAndImplode

public function checkAndImplode($arr, $field) { 
 
\t \t if(!isset($arr[$field])) { 
 
\t \t \t $ret = ""; 
 
\t \t } else { 
 
\t \t \t if(is_array($arr[$field])) { 
 
\t \t \t \t $ret = implode(" ", $arr[$field]); 
 
\t \t \t } else { 
 
\t \t \t \t $ret = $arr[$field]; // Am getting the control here 
 
\t \t \t } 
 
\t \t } 
 
\t \t return $ret; 
 
\t }

我想既然is_array($改編[$場])的是沒有得到執行,因此錯誤返回false我破滅功能。希望能幫助解決這個問題。

回答

1

對於發佈輸入數據作爲數組追加[]與輸入名稱

更新輸入字段如下

<input type="hidden" name="education_level[]" value="" /> 
+0

我這樣做,但錯誤依然存在。 – nitesh 2015-01-21 12:28:52