not able get post form submit values in codeigniter
Asked Answered
W

11

5

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>
    <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.?

Waugh answered 22/7, 2011 at 5:28 Comment(0)
W
7

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.

Waugh answered 23/7, 2011 at 6:5 Comment(1)
in /application ?? or in / ??Methylnaphthalene
R
4

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.. :)

Rhoads answered 22/7, 2011 at 8:56 Comment(1)
Personal Opinion: CI's Form Validation is more trouble than it's worth. I personally use my own framework, the CI way ends up violating DRY a lot, even with good planning. It may solve his problem though. :pFerial
B
3

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/';
Bromberg answered 10/2, 2014 at 6:40 Comment(0)
B
1

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.

Benedix answered 5/2, 2012 at 21:31 Comment(0)
A
1

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...

Armorial answered 15/2, 2012 at 7:39 Comment(0)
I
1

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

Impalpable answered 3/9, 2013 at 5:7 Comment(0)
F
0

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.

Ferial answered 22/7, 2011 at 8:48 Comment(6)
none of the trick worked actually. I can see the $_POST coming as empty so no matter with uname or name it doesn't matter and I fixed it though.Waugh
CI_Input Object ( [ip_address] => [user_agent] => Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0 [_allow_get_array] => 1 [_standardize_newlines] => 1 [_enable_xss] => 1 [_enable_csrf] => [headers:protected] => Array ( ) [security] => CI_Security Object ( [_xss_hash:protected] => ea1f1a23ff5349cfe8470d3ff9cc382e [_csrf_hash:protected] => 7108f2252e604139ed5b64c99959adb8 [_csrf_expire:protected] => 7200 [_csrf_token_name:protected] => ci_csrf_token [_csrf_cookie_name:protected] => ci_csrf_token [_never_allowed_str:protected]Waugh
=> Array ( [document.cookie] => [removed] [document.write] => [removed] [.parentNode] => [removed] [.innerHTML] => [removed] [window.location] => [removed] [-moz-binding] => [removed] [] => --> [ <![CDATA[ ) [_never_allowed_regex:protected] => Array ( [javascript\s*:] => [removed] [expression\s*((|&\#40;)] => [removed] [vbscript\s*:] => [removed] [Redirect\s+302] => [removed] ) ) [uni] => CI_Utf8 Object ( ) )Waugh
not sure it will be helpful and is there any PHP settings or Apache settings will restrict the form being post?Waugh
i am also keep on getting in the error log like [Fri Jul 22 19:12:37 2011] [error] [client 127.0.0.1] File does not exist: /var/www/vidyarti/feedback [Fri Jul 22 19:12:44 2011] [error] [client 127.0.0.1] File does not exist: /var/www/vidyarti/feedback, referer: localhost/vidyarti/feedbackWaugh
Note: You should post updates to the bottom of your question by editing it. You'll get better formatting.Ferial
S
0

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>
Spitzer answered 3/5, 2014 at 9:45 Comment(0)
L
0
$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! ;)

Loire answered 28/6, 2016 at 10:21 Comment(0)
A
0

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

Anadem answered 5/8, 2020 at 7:3 Comment(0)
B
0

Make sure your routes file is set to $routes->post() or $routes->add() - that got me! (CI4)

Burrstone answered 26/4, 2023 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.