简体   繁体   中英

Unable to load a form in codeigniter from a controller

I have a simple sign up/Login registration project in php/codeigniter

and I always get this particular error while clicking on the Sign in or the Register button

Error: "The requested URL /CI/user/login was not found on this server."

Here is the Controller file user.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class User extends CI_Controller{   
     public function __construct()
     {

      parent::__construct();
      $this->load->model('user_model');

     }

     public function index()

     {
      if(($this->session->userdata('user_name')!=""))
      {
       $this->welcome();
      }

      else{
       $data['title']= 'Home';
       $this->load->view('header_view',$data);
       $this->load->view("registration_view.php", $data);
       $this->load->view('footer_view',$data);
      }}

     public function welcome()
     {
      $data['title']= 'Welcome';
      $this->load->view('header_view',$data);
      $this->load->view('welcome_view.php', $data);
      $this->load->view('footer_view',$data);
     }

     public function login()
     {
      $email=$this->input->post('email');
      $password=md5($this->input->post('pass'));

      $result=$this->user_model->login($email,$password);
      if($result) $this->welcome();
      else        $this->index();
     }

     public function thank()
     {
      $data['title']= 'Thank';
      $this->load->view('header_view',$data);
      $this->load->view('thank_view.php', $data);
      $this->load->view('footer_view',$data);
     }

     public function registration()
     {
      $this->load->library('form_validation');
      // field name, error message, validation rules
      $this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
      $this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
      $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
      $this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');

      if($this->form_validation->run() == FALSE)
      {
       $this->index();
      }
      else
      {
       $this->user_model->add_user();
       $this->thank();
      }
     }

     public function logout()
     {
      $newdata = array(
      'user_id'   =>'',
      'user_name'  =>'',
      'user_email'     => '',
      'logged_in' => FALSE,
      );

      $this->session->unset_userdata($newdata );
      $this->session->sess_destroy();
      $this->index();
     }
    }
    ?>

Here is the view file registration_view.php

    <html>
<div id="content">
<div class="signup_wrap">
<div class="signin_form">


 <?php echo form_open("user/login"); ?>

  <label for="email">Email:</label>
  <input type="text" id="email" name="email" value=""/>

  <label for="password">Password:</label>
  <input type="password" id="pass" name="pass" value=""/>

  <input type="submit" class="" value="Sign in"/>

 <?php echo form_close(); ?>




</div><!--<div class="signin_form">-->
</div><!--<div class="signup_wrap">-->





<div class="reg_form">
<div class="form_title">Sign Up</div>
<div class="form_sub_title">It's free and anyone can join</div>



<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open("user/registration");?>

  <p>
  <label for="user_name">User Name:</label>
  <input type="text" id="user_name" name="user_name" value="<?php echo set_value('user_name'); ?>" />
  </p>

  <p>
  <label for="email_address">Your Email:</label>
  <input type="text" id="email_address" name="email_address" value="<?php echo set_value('email_address'); ?>" />
  </p>

  <p>
  <label for="password">Password:</label>
  <input type="password" id="password" name="password" value="<?php echo set_value('password'); ?>" />
  </p>

  <p>
  <label for="con_password">Confirm Password:</label>
  <input type="password" id="con_password" name="con_password" value="<?php echo set_value('con_password'); ?>" />
  </p>

  <p>
  <input type="submit" class="greenButton" value="Submit" />
  </p>

 <?php echo form_close(); ?>

</div><!--<div class="reg_form">-->
</div><!--<div id="content">-->

</html>

I think the main problem is with form_open ('form');

Here is the htacess file

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

您需要将完整的URL传递给form_open函数,如下所示

   echo form_open(base_url()."/user/login");

change this: <?php echo form_open("user/login"); ?> <?php echo form_open("user/login"); ?>
to this: <?php echo form_open(base_url()."User/login"); ?> <?php echo form_open(base_url()."User/login"); ?>
in config:

$config['base_url'] = 'http://localhost/projectname/';
$config['index_page'] = '';

after that if doesn't work call your method from url if work correct post data to your method!!!

EDIT: in your htaccess file, change to. (Another suggestion, no guarantee)

RewriteBase /project
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

Check your /etc/apache2/apache2.conf or /etc/apache2/httpd.conf whichever exists. Basically, apache does not acknowledge the .htaccess file unless you declare the AllowOverride directive.

So, you have to open up the said config file, and add this to the bottom, if your full path to the sub-directory is /var/www/html/subdirectory/

<Directory /var/www/html/subdirectory/>
    AllowOverride all
</Directory>

And don't forget to restart apache!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM