CodeIgniter Forums
How to pass data from a view to a controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to pass data from a view to a controller? (/thread-27497.html)

Pages: 1 2


How to pass data from a view to a controller? - El Forum - 02-12-2010

[eluser]theciman[/eluser]
I have a view where I have a hidden form which contains the id of a database table. I want to pass that id to my controller using the $this->input-post method but it seems that hidden forms does not allow this. If I put the id in a form_input field it works fine.

Here is the code for the controller:
function update_project()
{
$project_id = $this->input->get('ProjectID');
echo $project_id;
{


and the code for the view:

echo form_open('test/update_project');

$id_name = 'ProjectID';
$hidden_value = array('name' => $id_name, 'id' => $id_name, 'value' => $row->$id_name); //note $row->$id_name contains the table id I need which is there and valid
echo form_hidden($hidden_value); //pass the ID with a hidden form

echo form_submit('project_submit', 'Submit Changes');
echo form_close();

Any help would be appreciated.


How to pass data from a view to a controller? - El Forum - 02-12-2010

[eluser]danmontgomery[/eluser]
You have a hidden form? What does that mean? A hidden input? They are used specifically for what you are trying to do, so maybe you can provide some more details about what's not working, and what you mean by "does not allow this"?

[edit]

form_open() by default opens a POST form, and you are checking for GET

Code:
$this->input->post("ProjectID")



How to pass data from a view to a controller? - El Forum - 02-12-2010

[eluser]theciman[/eluser]
The website gives users the ability to change certain attributes within a database. After they login, they can list certain values within the database which I provide through a query. The values are presented in input_forms and dropdown menus which a user has the ability to change. Once he makes the changes and clicks the submit button, I do a $this->input->post call in the controller for each of the form_inputs and dropdown forms to see what was changed and then update the database accordingly. My issue is I need access to the row ID which was queried in the view and which needs to be returned to the controller so it knows what table row to update. I want to put the row id in a hidden form so the user does not have access to it but yet I need it so I know what table row to update. If I put the id in a input_form and then use the post method I have access to it but if I use a hidden form I don't. I need away to get the id which in the view over to the controller. If there is another way fine but I am not sure how to do this.

Any help would be great.


How to pass data from a view to a controller? - El Forum - 02-12-2010

[eluser]gcc.programmer[/eluser]
Here's the thing:

Your code for the controller:
Code:
function update_project()
{
  $project_id = $this->input->get(‘ProjectID’);
  echo $project_id;
{

As was stated, you need to change the input function to POST:
Code:
$project_id = $this->input->post('ProjectID');



your code for the view:
Code:
echo form_open(‘test/update_project’);

$id_name = ‘ProjectID’;
$hidden_value = array(‘name’ => $id_name, ‘id’ => $id_name, ‘value’ => $row->$id_name); //note $row->$id_name contains the table id I need which is there and valid
echo form_hidden($hidden_value);  //pass the ID with a hidden form



How to pass data from a view to a controller? - El Forum - 02-12-2010

[eluser]theciman[/eluser]
I tried $project_id = $this->input->post('ProjectID') and it didn't work. I posted $this->input->post('ProjectID') since the manual says it does the same thing. Either way it does not work. The thing is a hidden form (form_hidden) does not allow passing a value with a post from what I can tell. Is that correct?


How to pass data from a view to a controller? - El Forum - 02-12-2010

[eluser]gcc.programmer[/eluser]
No, that is NOT correct. Something else is going on.
When you view the source created by the form helper functions, can you SEE in the html source the id that your are trying to pass?


How to pass data from a view to a controller? - El Forum - 02-15-2010

[eluser]theciman[/eluser]
Here is the html code
<form action="http://ccc.ccc.com/dd/index.php/updateprojects/update_project" method="post">
<input type="hidden" name="name" value="ProjectID" />
<input type="hidden" name="id" value="ProjectID" />
<input type="hidden" name="value" value="5" />

As we can see the value is passed but when I try to print the value in the form I get nothing. When I make the form viewable, that everything works fine. Any ideas?


How to pass data from a view to a controller? - El Forum - 02-15-2010

[eluser]danmontgomery[/eluser]
[quote author="theciman" date="1266277585"]Here is the html code
<form action="http://ccc.ccc.com/dd/index.php/updateprojects/update_project" method="post">
<input type="hidden" name="name" value="ProjectID" />
<input type="hidden" name="id" value="ProjectID" />
<input type="hidden" name="value" value="5" />

As we can see the value is passed but when I try to print the value in the form I get nothing. When I make the form viewable, that everything works fine. Any ideas?[/quote]

You don't seem to have a very solid grasp on how HTML forms work... You have 3 hidden inputs here, none of them are "ProjectID" (what you're trying to access with $this->input->post("ProjectID"))

I think what you want is:
Code:
form_hidden("ProjectID", $projectID);

Which will produce:
Code:
<input type="hidden" name="ProjectID" value="5"/>

And can be accessed with:

Code:
$this->input->post("ProjectID"); // 5



How to pass data from a view to a controller? - El Forum - 02-15-2010

[eluser]theciman[/eluser]
Maybe it's me and yes I am knew to html and php, CI, etc. But while I was waiting for an answer I did the following:

Changed my code in the view to:
$id_name = 'ProjectID';
echo form_hidden($id_name, $row->$id_name); // simple -- name_id is "ProjectID" and value is ($row->$id_name) which is "5"

My controller has the following code:
$project_id = $this->input->post("ProjectID");
echo $project_id;

Then I printed out the html code:
<input type="hidden" name="ProjectID" value="5" />

Guess what I get nothing on the echo $project_id! So what could be wrong with the code listed above?


How to pass data from a view to a controller? - El Forum - 02-15-2010

[eluser]danmontgomery[/eluser]
I think you need to post more complete code, I just ran this:

Controller:
Code:
public function update_project() {
    echo $this->input->post("ProjectID");        
}
    
public function index() {
    echo form_open('test/update_project');
    echo form_hidden("ProjectID", "5");
    echo form_submit("submit", "submit");
    echo form_close();
}

Which, when I click submit, gives me an output of "5".