tagit:输入不是作为数组出现的

时间:2015-01-21 12:03:52

标签: php jquery codeigniter jquery-plugins

我正在使用Codeigniter框架,并希望使用jQuery Tag-it!创建一个" EducationLevel"我的用户的输入表单。我有标签的UI - 它工作,但我无法将用户输入到PHP数组。

javascript代码:



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




HTML代码:



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

通过此代码,我可以获得表单中输入的相关选项。

但是,在选择任何选项并提交表单时,我收到错误消息。在调试时我注意到了&#34;标签&#34;正在查询中填充。这个词不应该出现并导致错误。

&#13;
&#13;
INSERT INTO `profile` (`name`, education_level, tags, institution) VALUES (xyz, abc, pqr) 
&#13;
&#13;
&#13;

我在控制器中的功能。

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

服务守则

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

	}
&#13;
&#13;
&#13;

模型代码:

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

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

CheckAndImplode的代码

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

我认为因为is_array($ arr [$ field])返回false我的implode函数没有被执行因此错误。希望有助于解决这个问题。

1 个答案:

答案 0 :(得分:1)

用于将输入数据作为数组追加[]并输入名称

更新输入字段,如下所示

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