9

This should be something really simple but I just can't get it.

I'm learning codeigniter and I have a form with following code

<body>
  <form name ="userinput" action="form_reader.php" method="post">
      Name <input type="text" name="username"> <br/>
      <input type="submit" value="Submit">
  </form>

I have a controller called form_reader.php in my controllers folder. I get a 404 Not Found error. What am I doing wrong ?

Coder25
  • 311
  • 1
  • 4
  • 13

2 Answers2

36

Send your values to a function in your controller

  <form name ="userinput" action="form_reader/save_userinput" method="post">

in your controller, make a function called "save_userinput":

<?php
class Form_reader extends CI_Controller {

    public function save_userinput()
    {
      //code goes here
      // for example: getting the post values of the form:
      $form_data = $this->input->post();
      // or just the username:
      $username = $this->input->post("username");

      // then do whatever you want with it :)

    }
}
?>

Hope that helps. Make sure to check out the CI documentation, it's really good. Any more questions, just ask :)

EDIT: Figured it out. Use this opening form tag instead:

<form name ="userinput" action="index.php/form_reader/save_userinput" method="post">

I'm used to not having the index.php there, I remove it by using a .htaccess file (like this one), so I overlooked that. It works here with that small edit in the action attribute.

Alternatively, you could use the form helper:

Load it in your controller by using this->load->helper('form') and then use this instead of the HTML <form> tag: <? echo form_open('form_reader/save'); ?>

Community
  • 1
  • 1
Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
  • Unfortunately, I'm still getting the same error. Is there some setting I should have changed ? – Coder25 May 30 '11 at 18:52
  • Can you update your question with your controller and perhaps the full view? – Joris Ooms May 30 '11 at 18:57
  • I copied your code exactly. The view doesn't have anything else other than the html, head, body tags. I'm basically trying to get input from a form and display "Hi $username". – Coder25 May 30 '11 at 19:06
  • Thanks, it works now. Do you know why `index.php` is required to be added ? – Coder25 May 30 '11 at 19:31
  • 1
    CodeIgniter basically uses `index.php` to handle all requests, as far as I know. Check out the link I provided and the .htaccess in the first answer there, it removes it for you so you can have nice URLs. :) – Joris Ooms May 30 '11 at 19:34
  • thanks for this example. it didnt work for me either but i have edited it slightly...I was getting a 404 too. so i have just tried entering the full url to action and it has worked.. eg action="http ://www.example.com/index.php/form_reader/save_userinput" – Sarah Jul 13 '16 at 13:54
2

Have a look at the NETTUTS codeigniter tutorials. You'll have to modify slightly as they are using 1.7.2 but the concepts are the same

AlunR
  • 455
  • 3
  • 10