Welcome Guest, Not a member yet? Register   Sign In
Loading a view displays 'undefined' and that's it.
#1

[eluser]n1ght3y3s[/eluser]
First thing; I am completely new to this so the problem is probably simple. I am just starting to learn to use codeigniter and thought I would start with a simple app that displays the number of sleeps till a certain event (kids love this).

Anyway I have a page that allows you to enter a description and a date which adds a new entry to a database and then it should go on to display a success page. This is very similar to the example on the tutorials page. Instead of getting my success page I am getting a blank page with the word 'undefined' in the top left corner. I enabled logging and the view looks to be loading without error. Any suggestions?

Here are some snippets of the code. If you need more let me know.

Model
Code:
public function set_events()
{
$data = array(
  'id' => 0,
  'desc' => $this->input->post('desc'),
  'date' => $this->input->post('date'),
);

return $this->db->insert('events', $data);
}

Controller

Code:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'New Event';

$this->form_validation->set_rules('desc', 'Description', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
  
if ($this->form_validation->run() === FALSE)
{
  $this->load->view('templates/header', $data);
  $this->load->view('events/create');
  $this->load->view('templates/footer');
}
else
{
  $this->events_model->set_events();
  $this->load->view('events/success');
}
}

View: create.php

Code:
<?php echo validation_errors(); ?>
<?php echo form_open('events/create') ?>

<label for="desc">Description</label>
&lt;input type="input" name="desc" /&gt;&lt;br>

<label for="date">Date</label>
  &lt;input type="input" name="date"/&gt;

&lt;input type="submit" name="submit" value="Create Event" /&gt;
&lt;/form&gt;

View: success.php

Code:
<h2>Success</h2><br>

Added new event
#2

[eluser]rogierb[/eluser]
Where do you load the events_model? In your posted code I dont see it being loaded.
#3

[eluser]LuckyFella73[/eluser]
May be not the solution for your problem but your
table should have a column "id" with auto increment
and when inserting a new row don't define the "id" in
your data array.
Your model should return the insert_id an case the insert
was successful or FALSE if not.

Code:
public function set_events()
{
  $data = array(
   'desc' => $this->input->post('desc'),
   'date' => $this->input->post('date'),
  );
  if ($this->db->insert('events', $data))
  {
   return $this->db->insert_id();
  }
  else
  {
   return FALSE;
  }
}
#4

[eluser]n1ght3y3s[/eluser]
Got it, I will make that change later today and see what affect it has.

Thanks
#5

[eluser]n1ght3y3s[/eluser]
Sorry for the late reply, got a bit tied up in other work Smile

I made the change you suggested and had no effect. However I then went on to make the following change that did:

Code:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'New Event';
$data['footerItems'] = array(self::$homeMenuItem);

$this->form_validation->set_rules('desc', 'Description', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');

if ($this->form_validation->run() === FALSE)
{
  $this->load->view('templates/header', $data);
  $this->load->view('events/create');
  $this->load->view('templates/footer');
}
else
{

  $this->events_model->set_events();

  $this->load->view('templates/header', $data);
  $this->load->view('events/success');
  $this->load->view('templates/footer');
}
}

All I have done is add in the header and footer code. Not sure why this would make the difference... ?
#6

[eluser]LuckyFella73[/eluser]
In your first version you didn't send "$data" to your view file.
You didn't post your header and footer but you obviously echoed
some values there (at least "title") defined in the data array of your controller Wink
#7

[eluser]n1ght3y3s[/eluser]
My impression was that when it was displaying the 'success' page it wasn't trying to render the header and therefore I didn't need to worry about passing $data in.

#8

[eluser]boltsabre[/eluser]
A bit off topic, but on successful form submission you are loading a view ($this->load->view('events/success')Wink.

You should use "redirect" instead.

Why? When you just load another view the php superglobal $_POST still exists with all the post data that the user just submits. If the user refreshes the page it will get posted again, and thus inserted into your db again... not good!

Using "redirect" will... ummm... redirect, thus clearing the $_POST array.
#9

[eluser]CroNiX[/eluser]
You can also just do $_POST = array() and not redirect.




Theme © iAndrew 2016 - Forum software by © MyBB