How do I submit a post-method form to the same URL in another function in CodeIgniter?

I found a CodeIgniter form check to show an error message with the load-> view method, and the field error message will be lost if you use "redirection".

I am currently using one function to display a form page and another function to send a form message.

class Users extends CI_Controller {
  function __construct () {
      parent :: __ construct ();
  }

  public function sign_up ()
  {
    $ this-> load-> view ('users / sign_up');
  }

public function do_sign_up () {
      $ this-> form_validation-> set_rules ('user_login', 'User Name', 'trim | required | is_unique [users.login]');
      $ this-> form_validation-> set_rules ('user_email', 'Email', 'trim | required | valid_email | is_unique [users.email]');

      if ($ this-> form_validation-> run () == FALSE) {
          $ this-> load-> view ('users / sign_up');
      } else {
       // save post user data to users table
       redirect_to ("users / sign_in");
}


If the form validation fails, the URL in the browser will change to "/ users / do_sign_up", I want to keep the same URL on the sign_up page.

Using the redirect method ("users / sign_up") as a result of an unsuccessful form validation will support the same URL, but the validation error message will be lost.

in Rails, I cannot use routes for configuration as follows:

get "users / sign_up" => "users # signup"
post "users/sign_up"       => "users#do_signup"
+5
2

imho , "GET" ... "POST" , . , "POST" .

imho CodeIgniter:

public function sign_up()
{
    // Setup form validation
    $this->form_validation->set_rules(array(
        //...do stuff...
    ));

    // Run form validation
    if ($this->form_validation->run()) 
    {
        //...do stuff...
        redirect('');
    }

    // Load view
    $this->load->view('sign_up');
}

, , config/routes.php, CI RoR-. , route.php php, .

switch ($_SERVER['REQUEST_METHOD'])
{
    case 'GET':
        $route['users/sign_up'] = "users/signup";
    break;
    case 'POST':
        $route['users/sign_up'] = "users/do_signup";
    break;
}
+4
<button type="submit"class="md-btn btn-sm md-fab m-b-sm indigo" id="filterbtn" formaction="<?php echo base_url(); ?>front/get_filter/<?php echo$device_id;?>"><i class="fa fa-bar-chart"></i></button>
<button type="submit"class="md-btn btn-sm md-fab m-b-sm indigo" id="filterbtn" formaction="<?php echo base_url(); ?>front/get_data/<?php echo$device_id;?>"><i class="fa  fa-th-list"></i></button>
-2

All Articles