tagit:输入不是以数组的形式出现
问题描述:
我正在使用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我破灭功能。希望能帮助解决这个问题。
答
对于发布输入数据作为数组追加[]
与输入名称
更新输入字段如下
<input type="hidden" name="education_level[]" value="" />
我这样做,但错误依然存在。 – nitesh 2015-01-21 12:28:52