How to sole the problem of accessing form action function directly in URL? |
[eluser]term25[/eluser]
E.g. you have a form opening action like this: Code: echo form_open_multipart('page/create_do'); So, when somebody goes to e.g. http://localhost/page/create and press the sumbit button, the POST data go to create_do function in page controller. However, if somebody access directly this function from the browser like: http://localhost/page/create_do it will throw a lot of errors. How to avoid this? I write my functions in controllers like this : Code: public function create_do() { I tried private function create_do() and private function _create_do() but it disabled access from my form also Thanks for any advice.
[eluser]Glazz[/eluser]
You can check if a form was submited but why don't you place the logic in the create controller ? Code: function create_do() Edit: You can of course use the Form Validation library instead of the $this->input->post() ...
[eluser]InsiteFX[/eluser]
You can do this, CI allows you prefix a method name with an underscore to make it private. Code: public function _create_do() {
[eluser]term25[/eluser]
[quote author="InsiteFX" date="1332716999"]You can do this, CI allows you prefix a method name with an underscore to make it private. Code: public function _create_do() { Sorry, but this is not working when calling from form action.
[eluser]term25[/eluser]
[quote author="Glazz" date="1332709953"]You can check if a form was submited but why don't you place the logic in the create controller ? Code: function create_do() Thanks, this code works. Edit: You can of course use the Form Validation library instead of the $this->input->post() ...[/quote]
[eluser]InsiteFX[/eluser]
You can also use a hidden form input field and then check for that value in your method.
[eluser]term25[/eluser]
[quote author="InsiteFX" date="1332742073"]You can also use a hidden form input field and then check for that value in your method. [/quote] Thanks. I will try it.
[eluser]srpurdy[/eluser]
You don't even need a hidden field in most cases. You can just check for blank data in $_POST if $_POST is empty then just redirect them to the form. really though you can just use CI's form validation to do this as well. if the validation fails they get back the form, and it will always fail if the function is accessed directly unless you have no required fields.
[eluser]Matalina[/eluser]
I use to do it the way you are with two methods but now I use one method. I dislike on errors having the create_do showing up in the url. So I use one function for both display and processing. Code: // Do all processing for display of form first |
Welcome Guest, Not a member yet? Register Sign In |