douqiao1413 2017-08-18 10:01
浏览 34
已采纳

将变量从View页面传输到codeigniter中的Controller页面是否理想?

I'm using codeigniter's MVC to build a form where logged in users can post questions.

I am using a form that allows logged in users to insert data in a mysql table called Questions (table structure is shown below).

Questions table

______________________________________________
| id   |    User id  | description | category |
|---------------------------------------------|
|1     |      22     |  Car parts  |   12     |
|---------------------------------------------|
|2     |      54     |  Car parts  |   8      |
|---------------------------------------------|
|3     |     112     |  Car parts  |   11     |
|_____________________________________________|

Once a user submits the form, the user's id and category_id (from the table below "users table") is inserted into the questions table above

Users table

___________________________________
| id   |    User id  |category_id |
|-------------------------------
|1     |      22     |    12      |
|------------------------------   |
|2     |      54     |    54      |
|----------------------------     |
|3     |     112     |    8       |
|_________________________________|

My controller page code

 $this->load->model('questions_page');
    $data['page_detail']  = $this->questions_page->get_business_page_by_pid($page_id);

    if ($this->form_validation->run() == false || $form_validation == false)
        {
            $data['form_value'] = array(
                'country'        => $this->input->post('description'),
                'street_address' => $this->input->post('street_address'),
                'post_code'      => $this->input->post('post_code'),
            );
            $this->load->view('template', $data);
        }

This is my View page

<input type="text" name="post_code" value="<?= $form_value['description'] ?>">
                    <?php echo form_error("description"); ?>
<input type="text" name="post_code" value="<?= $form_value['street_address'] ?>">
                    <?php echo form_error("street_address"); ?>

   <?php // I gathered some info from the users table below which needs to be inserted into the questions table
    ////////////////////////////
    ///////////////////////////
    $page_detail->category_id;
    ////////////////////////////
    ///////////////////////////          
   ?>

My problem I have data in the view page in the last line ( $page_detail->category_id; ) that I'd like to transfer to the controller page so I can insert it in the mysql table using the $data['form_value'] = array(); as shown in the controller page.

How do I do this?

Is transferring data from the view to controller safe?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douliao8318 2017-08-18 10:12
    关注

    1st : Your storing the user id in question table so There is created relationship between two table so you can join two table with user id so that you can get the category id from user table . so no need to store the category id in question table .it's unnecessary . if you store . it's called data redundancy.

    2nd : Guess user id will be get from session .

    3rd : still you want pass the category_id from view to controller means just use some hidden input

     <input type='hidden' name="category_id" value="<?php echo  $page_detail->category_id; ?>" />
    

    In controller :

    $data['form_value'] = array(
                    'country'        => $this->input->post('description'),
                    'street_address' => $this->input->post('street_address'),
                    'post_code'      => $this->input->post('post_code'),
                    'category_id'    => $this->input->post('category_id'),
                    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条