Welcome Guest, Not a member yet? Register   Sign In
$_POST data not mapping to $this->input->post
#1

[eluser]randalrust[/eluser]
I am completely new to actually coding with CI and MVC, but have a lot of experience with OOP in PHP. I am working through a tutorial [1][2], but I'm having an issue that I don't quite understand.

My form_view has the following fields:

first name
last name
seminar (four checkboxes)
comments (textarea)

When I properly fill out and submit the form and the data is passed to the controller, I keep getting error messages that say:

A PHP Error was encountered
Severity: Notice
Message: Undefined index: email

The exception is the 'seminar' field. If I check a box, then it works properly.

I know the data is getting passed, because I can echo the $_POST array and see it.

Oddly enough, I added this to my controller file:

$this->input->post['fname']=$_POST['fname'];
$fname=$this->input->post['fname'];

Then I was able to avoid the error on the 'first name' field. So what it looks like is that the $_POST array is not getting properly passed to $this->input->post. Any thoughts?


[1] http://godbit.com/article/introduction-to-code-igniter
[2] http://godbit.com/article/introduction-t...ter-part-2
#2

[eluser]randalrust[/eluser]
Here is the code that I am working with.

CONTROLLER
----------

<?php
class Form extends Controller {

function index(){
//echo 'Hello World';
//$this->load->helper(array('form','url'));
#Input and textarea field attributes
$data['fname'] = array('name' => 'fname', 'id' => 'fname');
$data['email'] = array('name' => 'email', 'id' => 'email');
$data['comments'] = array('name' => 'comments', 'id' => 'comments', 'rows' => 3, 'cols' => 40);

#Checkbox attributes
$data['purpose'] = array('name' => 'seminar[]', 'id' => 'purpose', 'value' => 'Purpose of Prayer', 'checked' => FALSE);
$data['prepare'] = array('name' => 'seminar[]', 'id' => 'prepare', 'value' => 'Prepare for Prayer', 'checked' => FALSE);
$data['principles'] = array('name' => 'seminar[]', 'id' => 'principles', 'value' => 'Principles of Prayer', 'checked' => FALSE);
$data['power'] = array('name' => 'seminar[]', 'id' => 'power', 'value' => 'Power in Prayer', 'checked' => FALSE);

#Load our view
$this->load->view('form_view', $data);
#validations
$this->load->library('validation');
$rules['fname']='required';
$rules['email']='required|valid_email';
$this->validation->set_rules($rules);
#load view
if($this->validation->run()==FALSE){
$this->load->view('form_view', $data);
}
else {
#get post data
$this->input->post['fname']=$_POST['fname'];
$fname=$this->input->post['fname'];
echo $fname;
$email=$this->input->post['email'];
$comments=$this->input->post['comments'];
$seminars='';
foreach($this->input->post('seminar') as $value){
$seminars.="$value\n";
}
$message=$fname." would like to register for the following seminars:\n";
$message.=$seminars;
/*
#set email fields
$this->email->from($email, $fname);
$this->email->to('[email protected]');
$this->email->subject('seminar registration');
$this->email->message($message);
$this->email->send();
*/
#load the view file
$this->load->view('formsuccess');
}
}

}
?>

VIEW
-----------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="text/xhtml; charset=iso-8859-1" /&gt;
&lt;/head&gt;
&lt;body&gt;

<h1>registration form</h1>
<p>please complete the following form</p>
&lt;?php
echo $this->validation->error_string;
echo form_open('/form/index');
?&gt;
<h3>personal information</h3>
<fieldset>
<div class="row">
<span class="label"><label for="fname">first name &raquo;</label></span>
&lt;?php echo form_input($fname); ?&gt;
</div>
<div class="row">
<span class="label"><label for="email">email &raquo;</label></span>
&lt;?php echo form_input($email); ?&gt;
</div>
</fieldset>
<h3>seminar</h3>
<p>please select the seminars that you would like to attend:</p>
<fieldset>
<label for="purpose">purpose</label>&lt;?php echo form_checkbox($purpose); ?&gt;
<label for="prepare">prepare</label>&lt;?php echo form_checkbox($prepare); ?&gt;
<label for="principles">principles</label>&lt;?php echo form_checkbox($principles); ?&gt;
<label for="power">power</label>&lt;?php echo form_checkbox($power); ?&gt;
</fieldset>
<h3><label for="comments">comments</label></h3>
<fieldset>
<div class="row">
&lt;?php echo form_textarea($comments); ?&gt;
</div>
</fieldset>
<fieldset>
&lt;?php echo form_submit('submit', 'Submit'); ?&gt;
</fieldset>
&lt;?php echo form_close(); ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#3

[eluser]tonanbarbarian[/eluser]
please post a complete example of the problematic code and indicate where the error is.
be sure that you are using the post correctly.
it should be post as a method not an array
i.e.
Code:
$fname = $this->input->post('fname');
not
Code:
$fname = $this->input->post['fname'];
#4

[eluser]xwero[/eluser]
[quote author="randalrust" date="1199901553"]

$this->input->post['fname']=$_POST['fname'];
$fname=$this->input->post['fname'];
[/quote]
I don't know it it's a typo but method arguments are surrounded by rounded brackets
Code:
$this->input->post('fname')
#5

[eluser]randalrust[/eluser]
[quote author="xwero" date="1199901996"]I don't know it it's a typo but method arguments are surrounded by rounded brackets[/quote]

That was it. Can't believe I missed it, but I'm pretty used to brackets because I use the adodb abstraction library.

Thanks!
#6

[eluser]johnwbaxter[/eluser]
I do that more often than you can imagine!
#7

[eluser]Derek Allard[/eluser]
Since this is resolved, allow me to share my favourite.
Code:
$this->db>get('something');

I probably do that 4 times daily...
#8

[eluser]johnwbaxter[/eluser]
See Derek, this is why we all need a CI IDE to help us with the stupid things that we do.

Can you quickly make one and post it on this thread please. I'll give you about 10 minutes...
#9

[eluser]Derek Allard[/eluser]
done!
#10

[eluser]johnwbaxter[/eluser]
That's cheating!

Still you managed it in under 10 mins so i suppose i shouldn't complain!

I use coda, has there been anything similar done for that do you know?




Theme © iAndrew 2016 - Forum software by © MyBB