Welcome Guest, Not a member yet? Register   Sign In
IBM tutorial model method call failure
#1

[eluser]fourcs[/eluser]
:coolmad: I'm using CI 1.6.3. Error logs tell me that there is a PHP fatal error: Call to undefined method MContacts::addContact() in controllers/welcome.php, line 23.

This is the model code, mcontacts.php:
Code:
<?php
class MContacts extends Model{

  function MContacts(){
    parent::Model();
  }
}

function addContact(){
  $now = date("Y-m-d H:i:s");
  $data = array(
    'name' => $this->input->xss_clean($this->input->post('name')),
    'email' => $this->input->xss_clean($this->input->post('email')),
    'notes' => $this->input->xss_clean($this->input->post('notes')),
    'ipaddress' => $this->input->ip_address(),
    'stamp' => $now
  );

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

?>

This is the controller code, file welcome.php:

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    

function index(){
  $this->load->helper('form');
  $data['title'] = "Welcome to our Site";
  $data['headline'] = "Welcome!";
  $data['include'] = 'home';
  $this->load->vars($data);
  $this->load->view('template');
}

function contactus(){
  $this->load->helper('url');
  $this->load->model('MContacts','',TRUE);
  $this->MContacts->addContact();
  redirect('welcome/thankyou','refresh');
}

function thankyou(){
  $data['title'] = "Thank You!";
  $data['headline'] = "Thanks!";
  $data['include'] = 'thanks';
  $this->load->vars($data);
  $this->load->view('template');
}

function contact(){
  $this->load->helper('url');
  if ($this->input->post('email')){
    $this->load->model('MContacts','',TRUE);
    $this->MContacts->addContact();
    redirect('welcome/thankyou','refresh');
  }else{
    redirect('welcome/index','refresh');
  }
}

}
?>

This is the view with the form for input, home.php:
Code:
<p>This is random text for the CodeIgniter article.
There's nothing to see here folks, just move along!</p>
<h2>Contact Us</h2>

&lt;?php
echo form_open('welcome/contactus');
echo form_label('your name','name');
$ndata = array('name' => 'name', 'id' => 'id', 'size' => '25');
echo form_input($ndata);

echo form_label('your email','email');
$edata = array('name' => 'email', 'id' => 'email', 'size' => '25');
echo form_input($edata);

echo form_label('how can you help you?','notes');
$cdata = array('name' => 'notes', 'id' => 'notes', 'cols' => '40', 'rows' => '5');
echo form_textarea($cdata);

echo form_submit('submit','send us a note');
echo form_close();

?&gt;

Any suggestions will be appreciated.
#2

[eluser]Frank Berger[/eluser]
[quote author="fourcs" date="1224407841"]:coolmad: I'm using CI 1.6.3. Error logs tell me that there is a PHP fatal error: Call to undefined method MContacts::addContact() in controllers/welcome.php, line 23.

This is the model code, mcontacts.php:
Code:
&lt;?php
class MContacts extends Model{

  function MContacts(){
    parent::Model();
  }
} // <------- look here

function addContact(){
  $now = date("Y-m-d H:i:s");
  $data = array(
    'name' => $this->input->xss_clean($this->input->post('name')),
    'email' => $this->input->xss_clean($this->input->post('email')),
    'notes' => $this->input->xss_clean($this->input->post('notes')),
    'ipaddress' => $this->input->ip_address(),
    'stamp' => $now
  );

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

?&gt;
...
Any suggestions will be appreciated.[/quote]

The function addContact is not a member of your class Mcontacts, you close the class definition too soon. change it to this:

Code:
&lt;?php
class MContacts extends Model{

  function MContacts(){
    parent::Model();
  }


function addContact(){
  $now = date("Y-m-d H:i:s");
  $data = array(
    'name' => $this->input->xss_clean($this->input->post('name')),
    'email' => $this->input->xss_clean($this->input->post('email')),
    'notes' => $this->input->xss_clean($this->input->post('notes')),
    'ipaddress' => $this->input->ip_address(),
    'stamp' => $now
  );

  $this->db->insert('contacts', $data);
}
}  // <--- much better
?&gt;

cheers,
Frank
#3

[eluser]fourcs[/eluser]
:red: Absolutely right! How stupid of me. Thanks, mate.




Theme © iAndrew 2016 - Forum software by © MyBB