0

I have a page with a form, this form redirects the user to another page, however the user can only access this page if the form has been subimitted, for that I check if there's any token.

I have the following piece of code in my viw file:

 if( JRequest::checkToken() ==false){
       echo "The form was not submitted";   
    }else{
    
    // Display the view
    parent::display($tpl);
    
    }
    // Set the document
    $this->setDocument();

This partially works because it indeed verifies whether the user submitted the form or not; however, when the user submits the form for the first time, it does not recoginse the token (because it displays the error message "The form was not submitted") but if I come back to the form page and submit the form again, it works fine.

My question is:

What is the proper way to check the form submission using JRequest::checkToken() or any similar method and where would be the best way to place the function? Would it be in the view file?

2

I think you could more easily achieve what you're trying to do by displaying the other page as a response to the HTTP POST associated with the form submission, rather than by using a redirect.

After a form is submitted the core Joomla components always redirect to another page via a GET, but you don't have to follow that approach. You can provide the other page as the POST response if you just want to display it after the form has been submitted.

So you can do the following:

Output the token in the layout file of the form.

When the form is submitted you'll get a POST request and you should check the token in your controller using JSession::checkToken() (either directly or via inheritance from a parent AdminModel method which you call).

In your controller define the view and associated model and then call display() on that view.

| improve this answer | |
2

JRequest::checkToken() is deprecated. So use

if( JSession::checkToken() ==false){
       echo "The form was not submitted";   
    }else{

    // Display the view
    parent::display($tpl);

    }
    // Set the document
    $this->setDocument();

OR better you can even use JSession::getFormToken().

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.