Welcome Guest, Not a member yet? Register   Sign In
How to sole the problem of accessing form action function directly in URL?
#1

[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() {

// something here

}

I tried private function create_do() and private function _create_do() but it disabled access from my form also Sad

Thanks for any advice.

#2

[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()
{
    if($this->input->post())
    {
        // form submited, do the stuff here.
    } else {
        // form was not submited, redirect the user or show a message...
    }
}



Edit:

You can of course use the Form Validation library instead of the $this->input->post() ...
#3

[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() {

// something here

}
#4

[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() {

// something here

}
[/quote]

Sorry, but this is not working when calling from form action.
#5

[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()
{
    if($this->input->post())
    {
        // form submited, do the stuff here.
    } else {
        // form was not submited, redirect the user or show a message...
    }
}

Thanks, this code works.


Edit:

You can of course use the Form Validation library instead of the $this->input->post() ...[/quote]
#6

[eluser]InsiteFX[/eluser]
You can also use a hidden form input field and then check for that value in your method.
#7

[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.
#8

[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.
#9

[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

// Check if the form was submitted
if($this->input->post('submit_button_pressed') {
// If the form was submitted process the form here
if($this->form_validation->run) {
  // If the form validated process then redirect to with success flash data message
}
}

// If there was an error in the form or the form was not submited get view to display
}




Theme © iAndrew 2016 - Forum software by © MyBB