not able get post form submit values in codeigniter - 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>
<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

Related

PHPMailer - Show success message after redirect

I building a very simple app with Laravel4 and so far i have managed to set up PHPMailer to work with a contact form, the users fill in their details and send me an email, normal stuff, everything works fine.
After the user sent the email successfully, he is redirected to the home page via
if($m->send()) {
header('Location: /path/to/home/');
die();
}
Now what i need is a success message that appears at the top of the homepage if the user has been redirected after a successfully sent email.
I have a div with .success class sitting on top of my home page, absolutely positioned out of view, with a negative Y value.
I tried pulling it down after on $m->send() like so:
if($m->send()) {
header('Location: /path/to/home/');
echo "<script type='text/javascript'>
$('.success').animate({
top: 0
}, 2000);
</script>";
die();
}
but it didnt work. In fact, nothing i echo after the header() has any effect.
What can I do?
Thank you guys!
This is simple HTTP - when you set the Location header, it's telling the browser to leave the page you're on and go somewhere else - anything that happens afterwards (like that little JS snippet) will never reach the browser. You need to put that snippet on the page you're redirecting to, not on this one.
I solved the problem after realizing that you can't use jquery on the document before you actually link the jquery lib in.
So, in my phpmailer config file, I set $_SESSION['success'] = true before i redirect with the header('Location: /path/to/home/'); , and then, on the Homepage, the page I wanted the success message to be displayed on, I added this bit of code (AFTER linking the jQuery library):
<?php
if(isset($_SESSION['success']) && $_SESSION['success'] == true ) {
?>
<script type='text/javascript'> $('.success').animate({top : 0}, 'normal').delay(3000).animate({top : -57}, 'normal');</script>
<?php
} else {
$_SESSION['success'] = false;
}
?>
I don't know if this is a good practice but it does work.
I also had to session_start(); on my Homepage (obviously).
Hope this helps anyone in the same situation!

CodeIgniter adjusting url when passing parameters to controler

Ok i have records from database listed in view file, so u can see i wanna pass values to controler via href by update/grab function controler
echo $this->pagination->create_links();
br().br();
foreach ($query->result() as $q): ?>
<?php echo $q->info . br()?>
<?php endforeach; ?>
it works for first page in my pagination, when i am on some other page when i clicked on on record, instead passing parametars to controler when i clicked in keep adding url for example http://localhost/z/records/users/update/grab/3/update/grab/1/update/grab/1/update/grab/1/trtr
So error is when i have in url, when i am on second page in pagination
http://localhost/z/records/users/2
works only when i am on first page
http://localhost/z/records
is there a way to solve this proble. Will it works if i some how adjust routes??? Need help, please help me its very important
Try changing your link to an absolute URL:
<a href="/z/update/grab/<?php echo $q->id;?>/<?php echo $q->info; ?>">
Or adding a correct relative URL base to the header of your pages:
<base href="/z/" />
Codeigniter routes allow you to do this:
$route['post/(:any)/comment/(:any)'] = "posts/comments/$1/$2";
Then in the controller, the function inside my posts controller would work like this:
public function comments($one, $two) {
echo $one."-".$two;
}
so if you hit the url "/post/111/comment/222" the output would be
111-222

codeigniter : using flash data but no new page load?

Usually, I just set a $feedback var or array and then check for that to display in my views.
However, it occurred to me I should perhaps use flashdata instead.
The problem is sometimes - for say an edit record form, I may simply want to reload the form and display feedback - not redirect. when i use flashdata, it shows but then it shows on the next request as well.
What would be the best practice to use here?
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
u use hidden field for that
I would use the validation errors from the Form validation class and load those directly to the view in its 2nd argument.
$this->form_validation->set_error_delimiters('<p>', '</p>');
$content_data = array();
if (!$this->form_validation->run()) {
$content_data['errors'] = validation_errors();
}
$this->load->view('output_page', $content_data);
Then check in your view whether $errors isset.
Controller:
$data['message'] = 'some message you want to see on the form';
$this->load->view('yourView', $data);
View:
if (isset ($message)) : echo $message; endif;
...

PHP Sessions Var Not Storing... Possible php.ini issues?

I'm trying to store information on a form by using sessions in case the validation returns false, then the user doesn't have to refill the form. This how I'm doing this:
PAGE A:
<?
session_start();
$Fname = $_SESSION['FirstName'];
?>
<html>
<input type="text" id="First" value="<? if($Fname){echo $Fname;}?>">
</html>
PAGE B:
<?
$Fname = $_POST['First'];
session_start();
$_SESSION['FirstName'] = $Fname;
//validation is not good then
header('Location: PageA.php');
?>
The issue is that when sent back to page A, nothing is showing up in the inputs, but randomly it will show up or, I'll refresh and it might show up. For most part its just not working and what I don't understand it kind of just started happening when I made a modification of removing one of the session vars and replacing it with a cookie because of other reasons, but decided not to go that route. Still I don't see what I could have done to start causing this issue. Any ideas? I'm thinking its something with the php.ini file because i have a separate form with the same setup and I never touched it, and now its not working when it was the last time i checked.
UPDATE
I just tried the form, I submitted it incorrectly on purpose to trigger the validation and it sent me back with blank inputs as I already mentioned. I clicked on another page and then came back to the form and the inputs appeared. It seems as if its storing but just not being read immediately? Don't know if this helps.
On Page B session_start() should be the first command on the page. I always keep this at the top just to be sure
You do not need SESSION at all ...
PAGE A:
<?
$Fname = isset($_POST['First']) ? $_POST['FirstName'] : '';
?>
<html>
<form method='post' action='PageB.php'>
<input type="text" id="First" value="<? if($Fname){echo $Fname;}?>"/>
</form>
</html>
PAGE B:
<?
//validation is not good then
include 'PageA.php';
exit;
?>
Not sure if it is a typo, but in your input tag you don't have the "name" attribute specified, which would mean an empty $_POST array when you go to page B.
It's difficult (for me at least) to help you without further details, but here are my thoughts:
From the PHP manual: "Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant. "
One solution found in other forums: call session_write_close() before the header redirection: external link
Apparently, header() can sometimes make the redirection BEFORE the session cookie is written. That could explain why sometimes it works and other times it doesn't. But, as I said, not sure that this applies to your code.
I got it working by filling in the things you left out in your example; does it work for you?
PageA.php
note addition of <form action="PageB.php" method="POST"> and change of id="First" to name="First"
<?
session_start();
$Fname = $_SESSION['FirstName'];
?>
<html>
<form action="PageB.php" method="POST">
<input type="text" name="First" value="<? if($Fname){echo $Fname;}?>">
</form>
</html>
PageB.php
(unchanged)
<?
$Fname = $_POST['First'];
session_start();
$_SESSION['FirstName'] = $Fname;
//validation is not good then
header('Location: PageA.php');
?>

Rewriting URL's in codeigniter with url_title()?

I am rewriting my website with codeigniter and have something I want to do but not sure it is possible.
I have a gallery on my site powered by the Flickr API. Here is an example of the code I use to display the Landscape pictures:
<?php foreach ($landscapes->photoset->photo as $l->photoset->photo) : ?>
<a href="<?=$l->photoset->photo->farm?>/<?=$l->photoset->photo->server?>/<?=$l->photoset->photo->id?>/<?=$l->photoset->photo->secret?>/<?=$l->photoset->photo->title?>">
<img class="f_thumb" src="<?=$l->photoset->photo->farm?>.static.flickr.com/<?=$l->photoset->photo->server?>/<?=$l->photoset->photo->id ?>_<?=$l->photoset->photo->secret ?>_s.jpg" title="<?=$l->photoset->photo->title?>" alt="<?=$l->photoset->photo->title?>" />
</a>
<?php endforeach; ?>
As you can see when a user clicks on a picture I pass over the Farm, Server, ID, Secret and Title elements using URI segments and build the page in the controller using
$data['farm'] = $this->uri->segment(3);
$data['server'] = $this->uri->segment(4);
$data['id'] = $this->uri->segment(5);
$data['secret'] = $this->uri->segment(6);
$data['title'] = $this->uri->segment(7);
Everything works and is fine, but the URL’s are a tad long. Example:
http://localhost:8888/wip/index.php/gallery/focus/3/2682/4368875046/e8f97f61d9/Old_Mill_House_in_Donegal
Is there a way to rewrite the URL so its more like:
http://localhost:8888/wip/index.php/gallery/focus/Old_Mill_House_in_Donegal
I was looking at using:
$url_title = $this->uri->segment(7);
$url_title = url_title($url_title, 'underscore', TRUE);
But I can’t seem to be able to get it to work. Any ideas?
Are the links linking internally? Or are they linking to flickr? If it's internally, I'd suppose you could pass all the requisite information via POST. If it's to flickr, I don't think there is a way you can control how they want requests to come in for a specific item.
What's the endgame/purpose of all that extra info (secret, server, etc)?

Resources