CodeIgniter Forums
URI change when calling controller functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: URI change when calling controller functions (/showthread.php?tid=41304)

Pages: 1 2


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Bondergaard[/eluser]
Hi
I have a form with several submit buttons for different actions. The way I handle that is that I have a function in my controller that checks the submit button value and call the appropriate function in the controller. It works fine, but when I submit the form the url is index.php/controller/handler and I want it to be index.php/controller/functionA if the first submit button is clicked and so on. I know that I can do it with a redirect, but then all the post variables from the form is not passed to it. Is there a way to do that?

The code below is just an example.

Form:
Code:
<form action="controller/handler" method="POST">
<input type="hidden" name="beverage" value="water" />
<input type="submit" name="formAction" value="a" />
<input type="submit" name="formAction" value="b" />
</form>

Controller:
Code:
function handler()
{
$action = $this->input->post('formAction');

if($action == 'a')
  $this->functionA(); //Works but url is still controller/handler
  redirect('controller/functionA'); //URL is correct but the post variable is not passed
if($action == 'b')
  $this->functionB();
}

functionA()
{
  echo $this->input->post('beverage');
}

functionB()
{
  echo $this->input->post('beverage');
}



URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Sudz[/eluser]
You have to load your view file in the funtion below

Code:
functionA()
{
}

If you can post your code then it is easy to rectify.


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Bondergaard[/eluser]
That is not really my issue, the issue is the url which when clicking button is index.php/controller/handler and not index.php/controller/functionA.

I've updated the code in the first post to clarify what happens.


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Sudz[/eluser]
Issue your are facing is not an issue dude.
it is going to happen
because when you calling functionA
after executing statements in functionA, control returns from functionA to function handler.

And from where you are going to load your form view?

URI only changes when you load the view.


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Bondergaard[/eluser]
I know that it is going to happen, I'm asking if there is an other way to do it.

In other words can I call the redirect() function and pass some post variables to it in some way, or do something similar.


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Sudz[/eluser]
Just load your view in that function.


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Bondergaard[/eluser]
I don't know if you really understand my question but it does not sound like what I want to do is possible.

The only way to do it, is to use javascript to change the form action to index.php/controller/functionA or index.php/controller/functionB before submitting it. I just wanted to see if there was a way to avoid that.

Anyway, thanks for your help


URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Sudz[/eluser]
Try this man
Code:
functionA()
{
  echo $this->input->post('beverage');
$this->load->view('you_view_file');
}



URI change when calling controller functions - El Forum - 05-04-2011

[eluser]Bondergaard[/eluser]
That doesn't make a difference. It doesn't change the url which is what I want to do..


URI change when calling controller functions - El Forum - 07-31-2011

[eluser]blindrc[/eluser]
Not sure if this will help, but "remapping" might be the solution to your problem.
Check out the following article:

http://thenerdary.net/articles/entry/codeigniter_remapd