【问题标题】:How to get value from checkbox and post array in codeigniter?如何从复选框中获取值并在codeigniter中发布数组?
【发布时间】:2019-07-12 13:37:46
【问题描述】:

如何在 codeigniter 中获取值形式的倍数复选框并发布数组?当我得到值后数组并回显该值时,我遇到了问题。我只看到最后一个选中复选框的值。提交时如何显示帖子值? 您会找到三个文件:视图、控制器和模型。请检查我错在哪里...

更新.PHP:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div class="row">
<div class="col-sm-12">
<?php echo form_open('page_controller/update_page_post'); ?>

<div class="form-group">
            <div class="row">
                <div class="col-sm-3 col-xs-12">
                    <label><?php echo trans('subcategory'); ?></label>
                </div>
           </div><div class="col-xm-12">
                         <div class="table-responsive">
                             <table class="table table-bordered table-striped" role="grid">
                            <tbody>
                                <tr>
                                <?php   $valuesub = ($page->subcat_recip_id); ?>
                            <?php   $array_of_values = explode(",", $valuesub); 
                                //if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0") :
                    foreach ($array_of_values as $item) { 

                        if(in_array($subcat_recip_id,$item)): {  ?>

                       <td>
                           <input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" CHECKED> &nbsp; <?php echo html_escape($item["title"]);
                                    } ?>
                                <?php   else: { ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>"> &nbsp; <?php echo html_escape($item["title"]);
                        } 

                                endif;  }?>
                                </td> 
        <?php echo html_escape($valuesub); ?></tr>
                                 </tbody>
                             </table>
                 </div>   
                </div>
        </div>

PAGE_MODEL.PHP:

<?php class Page_model extends CI_Model
{
public function input_values()
{
    $data = array(
        'lang_id' => $this->input->post('lang_id', true),
        'title' => $this->input->post('title', true),
        'slug' => $this->input->post('slug', true),
        'page_description' => $this->input->post('page_description', true),
        'page_keywords' => $this->input->post('page_keywords', true),
        'page_content' => $this->input->post('page_content', false),
        'parent_id' => $this->input->post('parent_id', true),
        'page_active' => $this->input->post('page_active', true),
        'title_active' => $this->input->post('title_active', true),
        'breadcrumb_active' => $this->input->post('breadcrumb_active', true),
        'need_auth' => $this->input->post('need_auth', true),
        'howmany_people' => $this->input->post('howmany_people', true),
        'difficulty' => $this->input->post('difficulty', true),
        'howmany_time' => $this->input->post('howmany_time', true),
        'location' => $this->input->post('location', true),
        'subcat_recip_id' => $this->input->post('subcat_recip_id')
    for ($i=0; $i<count($menu_links); $i++) 
   {
    echo $subcat_recip_id[$i];
   } 
    );  
    return $data;
}
//add page
public function add()
{
    $data = $this->page_model->input_values();
    if (empty($data["slug"])) 
    {
    //slug for title
    $data["slug"] = str_slug($data["title"]);
    if (empty($data["slug"])) 
      {
            $data["slug"] = "page-" . uniqid();
      }
    }
    return $this->db->insert('pages', $data);
}

//update page
public function update($id)
{
    //set values
    $data = $this->page_model->input_values();
    if (empty($data["slug"])) {
        //slug for title
        $data["slug"] = str_slug($data["title"]);
        if (empty($data["slug"])) {
            $data["slug"] = "page-" . uniqid();
        }
    }
  $page = $this->get_page_by_id($id);
  if (!empty($page))  {
    $this->db->where('id', $id);
    return $this->db->update('pages', $data);
}
     return false;
}

PAGE_CONTROLLER.PHP

 * Add Page Post*/
public function add_page_post()
{
    //validate inputs
    $this->form_validation->set_rules('title', trans("title"), 'required|xss_clean|max_length[500]');

    if ($this->form_validation->run() === false) {
        $this->session->set_flashdata('errors', validation_errors());
        $this->session->set_flashdata('form_data', $this->page_model->input_values());
        redirect($this->agent->referrer());
    } else {

        if (!$this->page_model->check_page_name()) {
            $this->session->set_flashdata('form_data', $this->page_model->input_values());
            $this->session->set_flashdata('error', trans("msg_page_slug_error"));
            redirect($this->agent->referrer());
            exit();
        }

        if ($this->page_model->add()) {
            $this->session->set_flashdata('success', trans("page") . " " . trans("msg_suc_added"));
            redirect($this->agent->referrer());
        } else {
            $this->session->set_flashdata('form_data', $this->page_model->input_values());
            $this->session->set_flashdata('error', trans("msg_error"));
            redirect($this->agent->referrer());
        }
    }
}

【问题讨论】:

  • 恐怕这里没有足够的信息让我们帮助您。例如,我们不知道 $menu_links 是什么样的,我们也不知道您是如何处理 $_POSTed 数据的。对不起。

标签: php arrays codeigniter checkbox codeigniter-3


【解决方案1】:

我试过这段代码 在您的 Page_model 中放置此代码。

public function input_values(){
$checkbox = implode(',', $this->checkBox());
$data = array(
    'lang_id' => $this->input->post('lang_id', true),
    'title' => $this->input->post('title', true),
    'slug' => $this->input->post('slug', true),
    'page_description' => $this->input->post('page_description', true),
    'page_keywords' => $this->input->post('page_keywords', true),
    'page_content' => $this->input->post('page_content', false),
    'parent_id' => $this->input->post('parent_id', true),
    'page_active' => $this->input->post('page_active', true),
    'title_active' => $this->input->post('title_active', true),
    'breadcrumb_active' => $this->input->post('breadcrumb_active', true),
    'need_auth' => $this->input->post('need_auth', true),
    'howmany_people' => $this->input->post('howmany_people', true),
    'difficulty' => $this->input->post('difficulty', true),
    'howmany_time' => $this->input->post('howmany_time', true),
    'location' => $this->input->post('location', true),
    'subcat_recip_id' => $checkbox
);
}

public function checkBox(){
$count = count($_POST['subcat_recip_id']);
for($n=0; $n<$count; $n++){
$checkbox[$n] = $_POST['subcat_recip_id'][$n];
}
return $checkbox;
}

希望对你有帮助

【讨论】:

  • 现在它不会向数据库添加任何内容,只会添加 NULL 字段。它需要返回 $data 吗?
  • 你能把结果贴出来吗 var_dump($data);die;在 input_values(){ var_dump($data);die; }
  • Hip Hura:我在哪里添加 var_dump?视图页面中的表单内部还是控制器功能内部? Tks
  • 在您的控制器内部,在关闭您的功能之前,顺便说一句,您是否测试了另一种方式的答案框?
  • 我更新了所有你需要检查的文件我错了。 TKS
【解决方案2】:

这就是$menu_links:

public function get_menu_links_by_lang()
{
    $lang_id = $this->input->post('lang_id', true);
    if (!empty($lang_id)):
        $menu_links = $this->navigation_model->get_menu_links_by_lang($lang_id);
        foreach ($menu_links as $item):
            if ($item["type"] != "category" && $item["location"] == "header" && $item['parent_id'] == "0"):
                echo ' <option value="' . $item["id"] . '">' . $item["title"] . '</option>';
            endif;
        endforeach;
    endif;
}      

我编辑了一个代码来插入新的表单页面,我需要这个页面来将一些值输入 DB。一切正常,他们插入数据库,只有复选框不起作用,或者更好的是它只需要最后一个选中复选框的值,而不是所有选中的复选框!我已经尝试了很多方法,但我不能一个人做。对不起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多