CodeIgniter's form action is not working prope

2019-07-29 00:22发布

问题:

I have failed many times to figure out why the action attribute of my form is malfunctioning when I click the submit button.

All I wanted to do is to pass the form data to the controller. But what's happening is that the browser is just redirecting me to another page (on localhost, even the URI is correctly supplied.)

<form name = "employee" method = "post" action = "<?php echo base_url() .'employee/add_employee'; ?>">
First Name:  <input type = "text" name = "F_Name">
Middle Name: <input type = "text" name = "M_Name">
Last Name:   <input type = "text" name = "L_Name">
<input type = "submit" value = "save">
</form>

Here's the add_employee function in my employee.php (with the class name of 'Employee'):

public function add_employee(){

    $employee = array(
    'F_Name' => $this->input->post('F_Name'),
    'M_Name' => $this->input->post('M_Name'),
    'L_Name' => $this->input->post('L_Name')
    );

    $this->Employee_model->insert_employee($employee);

    echo "Employee added!<br />";
}

I don't think the Employee_model is the problem, so I won't add it here. I'm guessing that the problem has to do with the URL in my form action.

Why is the browser redirecting me to another page instead of executing the add_employee() function?

回答1:

So after a long discussion, here is the solution :

In your htaccess, you had to change

RewriteBase /CI/ into RewriteBase /CI_Practice2

Also, maybe you have to do this with MAMP (Maybe not)

localhost:8080/CI_Practice2

It's if you didn't change the defaults ports of MAMP.

Have a nice day



回答2:

Use site_url()

Try this

<form name = "employee" method = "post" action = "<?php echo site_url('employee/add_employee'); ?>">