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
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'),
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
);
save the value in the input box of type hidden , so the form will get submitted and category value can be fetched at controller
something like below
<input type="hidden" name="category" value="<?= $page_detail->category_id;?>">