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('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()); 
}
}
 
 
}
}

 

Link to comment
Share on other sites

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." />";
}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.