开发者

not able get post form submit values in codeigniter

I tried a simple form submit But I am not able to get the form values on controller using $this->input->post as well as $_POST[] methods. My view part is

<html>
<head>
    <title> Feedback page</title>       
</head>
<body>

    <?php echo form_open('feedback/save'); ?>       
    <p>
        <label>name: </label>
        <?php echo form_input('name'); ?>

    </p>
    <p>
    <label>Email: </label>
        <?php echo form_input('email'); ?>
    </p>
    <p>
   开发者_Go百科 <label>Feedback: </label>
        <?php echo form_textarea('feedback'); ?>
    </p>
    <p>
        <?php echo form_submit('submit','Submit'); ?>
    </p>

    <?php echo form_close(); ?>

</body> 

</html>

and controller part is

<?php
class Feedback extends CI_Controller {

function __construct() {
    parent::__construct();      
    $this->load->model("MFeedback");

}
function index() {

    $this->load->view('home/feedback_view.php');
    //print "loaded";


}

function save() {
    print "called";     
    print_r($this->input); 
    $name = $this->input->post('uname');
    $email = $this->input->post('email');
    $feedback = $this->input->post('feedback');
    print $name . $email . $feedback;
    $this->index();
}

}
?>

I am not sure what went wrong here or is there any config settings I need to look in to it.?


I have found out the problem. It is actually with the rewrite rule. Make sure you have rewrite rule like

RewriteEngine On
RewriteRule ^(application) - [F,L] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

on root folder of codeigniter.


Take a look at this: http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules

I had a similar issue when I started off using CI. You need to set at least one validation rule for the form and then check to see if the form submitted met that rule. You can then access the submitted form data like you are doing above..

It's been a while since I've used CI but something like this should solve your problem: (Taken from the link above)

    $this->load->library('form_validation');

    $this->form_validation->set_rules('uname', 'Username', 'required');
    $this->form_validation->set_rules('email', 'Password', 'required');
    $this->form_validation->set_rules('feedback', 'Feedback', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');

    if ($this->form_validation->run() == FALSE)
    {
                    // Here is where you do stuff when the submitted form is invalid.
        $this->load->view('myform');
    }
    else
    {
                    // Here is where you do stuff when the submitted form is valid.
            print "called";     
            print_r($this->input); 
            $name = $this->input->post('uname');
            $email = $this->input->post('email');
            $feedback = $this->input->post('feedback');
            print $name . $email . $feedback;
            $this->index();

    }

Hope that helps you in someway.. :)


your url address should be same as config->config.php->$config['base_url'] if your url address like

http://www.test.com

then your configh should be

$config['base_url'] =  'http://www.test.com/';


I was facing the same problem as you since the past half hour couldn't get anything to work. I tried your solution, it didn't help. But you were right it has to do with routing.

I was also passing other variables to my action like :

domain/controller/action/value1/value2

when I had my form submit data to :

domain/index.php/controller/action/value1/value2

it solved the problem. I am guessing if you pass values at the end the post variables don't work as expected. I know its supposed to work and I guess it does as well. Think its a problem with setting .htaccess correctly.


Thanks for the ideas that I solved my probs. I've got the same issue. My code worked well in WAMP, but when I moved to LAMP, I got all sorts of wired problems that I've never met before, and not getting any form post value was one of them.

According to the suggestion above:

I used form_open(index.php/controller/method) instead of form_open(controller/method) and it worked straight away.

However I got my index.php removed, and it's not shown in the address bar neither. As I said it's wired...


Use form action='domain/index.php?/controller/function name/parameter1/parameter2'

For example your domain is abc.com, controller is get, function name value,and parameter to be passed in functions are a and b then just write the form action like following way.

<form action='http:/abc.com/index.php?/get/value/a/b' method='post' >

I solved my problem this way. Hope it will work for you. Thanks


Firstly, In your view you've specified the name of your one input to be name, in your controller you're looking in post for uname.

Secondly, I don't remember if CodeIgniter does the same to $_POST but it definately destroys the $_GET array. If you want an associative array of all post inputs you can call this:

$this->input->post();

Thirdly, In a very very rare case your inputs might be getting blocked by XSS Filtering, you can stop this from happening by calling it like this (only for inspection purposes to see what's wrong, dont use this in production):

$this->input->post(NULL, FALSE);

If something is generally wrong, these calls will return FALSE, you should test for this using the === operator, as it will only match FALSE where == will match NULL as well.

Fourthly, You should test quickly using a simple html form, it looks like you're building your form right with the form helper but it never hurts to use a straightforward HTML Form for quick testing.

Other than that, you'll need to provide more information about your environment / configuration / generated html / etc... for us to figure out. You really didn't give us a lot to work with.


Well I have faced the same issue and following additions to .htaccess helped solved my problem.

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>


$data = array('id' => 'email',
 'name' => 'email',
 'class' => 'form-control');

echo form_input($data);

Just a quick mention that if you use an array to set up your inputs etc.. dont forget to include the name => 'your_desired_post_variable_name' in your array as this was my mistake, I was giving it just an id and wondering why my POST array was blank! Dont do the same! ;)


I've had a similar issue on my local ubuntu. htaccess was properly configured but nothing inside post. My issue was that apache didn't have mod rewrite enabled and I've fixed it by running these commands:

sudo a2enmod rewrite
sudo service apache2 restart

After that, all my post data went trough. Hope that helps the next person who has the same issue

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜