abstrakt Posted November 30, 2014 Share Posted November 30, 2014 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()); } } } } Quote Link to comment https://forums.phpfreaks.com/topic/292815-help-with-formsubmit-codeigniter/ Share on other sites More sharing options...
hansford Posted November 30, 2014 Share Posted November 30, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292815-help-with-formsubmit-codeigniter/#findComment-1498131 Share on other sites More sharing options...
abstrakt Posted November 30, 2014 Author Share Posted November 30, 2014 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." />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/292815-help-with-formsubmit-codeigniter/#findComment-1498135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.