Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4579996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:43:15+00:00 2026-05-21T20:43:15+00:00

So I have a web app that i’m working on with a form that

  • 0

So I have a web app that i’m working on with a form that requires all of the fields to be populated before submitting. If you try to submit the app without a field being populated, it loads the page again with the errors. Once you fill all the fields in and click submit, it does a redirect to the same page and shows a message which is generated from flashdata. See simplified example below.

Welcome controller:

function show_view() 
{
  $this->load->view('form');
}

function process_form()
{
  // make the 'quality' field required
  $this->form_validation->set_rules('quality', 'Quality', 'required');

  if($this->form_validation->run() == FALSE) //if the fields are NOT filled in...
  {
    echo form_error('quality');
    $this->load->view('form'); //reload the page  
  }
  else  // if the fields are filled in...
  {
    // set success message in flashdata so it can be called when page is refreshed. 
    $this->session->set_flashdata('message', 'Your rating has been saved');
    redirect(welcome/show_view);
  }
}

Now to illustrate my issue, lets say I’m on the ‘home’ view and I navigate to the ‘form’ view. If i populate the ‘quality’ field and click submit, i get redirected back to the ‘form’ view, with a success message. If i click the back button on the browser, it takes me back to the ‘home’ view. EVERYTHING WORKS AS EXPECTED

Now lets say i’m on the ‘home’ view and I navigate to the ‘form’ view. If i click the submit button without populating the ‘quality’ field, the ‘form’ view is reloaded again and it displays the error message. If i then populate the ‘quality’ field and click submit, i get redirected back to the ‘form’ view with a success message. The problem is, if i click the back button on the browser, it now takes me back to the form page with the error, and I have to click the back button again to get back to the ‘home’ view.

What is the best coding practice so that if a user submits the form with errors, it will display the errors, and if they fix the errors and submit the form again it will display the success message and if they click back on the browser, it will take them back to the ‘home’ view??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:43:16+00:00Added an answer on May 21, 2026 at 8:43 pm

    The problem is that you’re using two separate functions to do form handling. The form validation class docs don’t really explain it well, and it took me awhile to realize it but the form_validation->run() returns false if there is an error, but also if it is a GET request, and subsequently accounts for the GET request in the related functions like the form_error(), and validation_errors(), set_value(), etc.

    The best practice in CI (and in general) is to do this:

    class Welcome extends CI_Controller{
    
    function home(){
        $this->load->view('home');
    }
    
    function form() 
    {
        // make the 'quality' field required
        $this->form_validation->set_rules('quality', 'Quality', 'required');
    
        // If the fields are NOT filled in...
        // or if there isn't a POST! (check the Form_validation.php lib to confirm)
        if ( $this->form_validation->run() === FALSE) 
        {
             // This form_error() function actually doesn't do anything if there 
             // wasn't a form submission (on a GET request)
             echo form_error('quality');
             $this->load->view('form'); // load or reload the page
        }
        else  // if the fields are filled in...
        {
             // set success message in flashdata so it can be 
             // called when page is redirected. 
             $this->session->set_flashdata('message', 'Your rating has been saved');
             redirect('welcome/home','location', 303);
             exit;
        }
    
    }
    

    then in the view have the form action="welcome/form"

    Basically all of the form error functions and all the stuff related to form validation have checks to see if the form validator actually ran… here is an example from the form_error function in the form helper file

    function form_error($field = '', $prefix = '', $suffix = '')
    {
        if (FALSE === ($OBJ =& _get_validation_object()))
        {
            return '';
        }
    
        return $OBJ->error($field, $prefix, $suffix);
    }
    

    When their isn’t a POST, it shows as normal, and has the natural page flow you are looking for.

    Unrelated to the question, but confusing/noteworthy about the form validation class… if you use the filters like xss_clean, prep_url, etc. in the parameters field, it actually repopulates the $_POST array for you, so you don’t really need to do anything extra.

    Sometimes it’s worth taking a look at the internals of the CI source, there’s some clever stuff in there which isn’t entirely obvious.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app that is heavily loaded in javascript and css. First
I have a web app that only registered users can use, therefore I should
Problem: We have a web app that calls some web services asynchronously (from the
I have a huge web app that is having issues with memory leak in
I have an existing web app that allows users to rate items based on
I have this .NET web app that draws a table in Page_Load. Then after
This a bit of strange one.... We have an internal web app that runs
I have been support a web app that is used by a user base
We are currently storing plain text passwords for a web app that we have.
I have a web app for commercial property management that needs spreadsheet-like functionality for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.