Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting form values to controller

Tags:

codeigniter

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 ?

like image 751
Coder25 Avatar asked May 30 '11 18:05

Coder25


2 Answers

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'); ?>

like image 73
Joris Ooms Avatar answered Oct 18 '22 14:10

Joris Ooms


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

like image 24
AlunR Avatar answered Oct 18 '22 12:10

AlunR