I'm a bit new to PHP and I'm getting my feet wet with mvc with code igniter. I'm having a bit of trouble with my view accessing a function from my controller. If anyone has seen CodeIgniter from Scratch Day 4, it's that tutorial - i've created an email newsletter signup.
In my my view (newsletter.php), my submit button is not working and isn't able to access a function from my controller (email.php). The inaccessible function is called function send(). Instead, I get a 404 error. I'll post the code from both the view and controller, but I highly suspect the error is contained within the view because my controller loads the view, but my view can't call a function from the controller. Any help you can provide would be greatly appreciated. Thanks.
Here's the code from my view (newsletter.php):
<html lang='en'>
<head>
<title>untitled</title>
<link rel ='stylesheet' href='<?php echo base_url(); ?>css/style.css' type='text/css' media='screen' />
</head>
<body>
<div id="newsletter_form">
<?php
echo form_open('email/send');
?>
<?php
$name_data = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name')
);
?>
<p><label for="name">Name: </label><?php echo form_input($name_data); ?></p>
<p>
<label for="name">Email Address: </label>
<input type = 'text' name = 'email' id = 'email' value = '<?php echo set_value('email'); ?>'>
</p>
<p>
<?php
echo form_submit('submit', 'Submit');
?>
</p>
<?php
echo form_close();
?>
<?php
echo validation_errors('<p class = "error">');
?>
</div><!--end newsletter-form-->
</body>
</html>
Here's the code from my controller (email.php):
?php
/* SENDS EMAIL WITH GMAIL */
class Email extends CI_Controller
{
function index()
{
$this->load->view('newsletter');
}
function send()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
if($this->form_validation->run() == FALSE)
{
$this->load->view('newsletter');
}
else
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from('myemail@gmail.com', 'My Email Name');
$this->email->to($email);
$this->email->subject('Hey, from Codeigniter');
$this->email->message('It is working');
/*attachments */
$path = $this->config->item('server_root');
echo $path;
$file = $path . '/ci/attachments/yourinfo.txt';
$this->email->attach($file);
if($this->email->send())
{
echo 'Your email was sent';
}
else
{
show_error($this->email->print_debugger());
}
}
}
}