Jump to content

help with form(submit) [CodeIgniter]


abstrakt

Recommended Posts

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('[email protected]', '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()); 
}
}
 
 
}
}

 

First thing I would check is the action attribute of the form, what url is the form submitting to and is it a valid address of a script.

The function that loads the form will by default use the base url you set up to use Codeigniter.

Well here's the form_submit() code. I'm having difficulty understanding it, but here's how I interpret it: it creates an array called $defaults, into which the type, name (nonexistent in this case, I believe?), and value are passed. I don't understand this part: return "<input "._parse_form_attributes($data, $defaults).$extra." />";

 

if ( ! function_exists('form_submit'))
{
function form_submit($data = '', $value = '', $extra = '')
{
$defaults = array(
                            'type' => 'submit',
                            'name' => (( ! is_array($data)) ? $data : ''),
                            'value' => $value
                   );
 
return "<input "._parse_form_attributes($data, $defaults).$extra." />";
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.