Welcome Guest, Not a member yet? Register   Sign In
Making form submission generate email and update database.
#1

[eluser]Stompfrog[/eluser]
Hi all,

I am new to code igniter and am working through my first project with it now.

I am finding it brilliant, so much faster than old fashioned PHP :0)

I have only run into one problem so far which is this...

I want to submit a form which sends all its contents to the database and then generates me an email with just some of the info.

I can get each part to work on its own but i can't pull them together.

In my submit.php controller I have a submit_content() function which has the code to generate the email and to update the db. If i comment either half of this code out the other half works fine but if i do them both together the email never turns up. Should I put these two different parts in seperate functions and redirect from one to the other or should I leave it all in one function? If so, why won't it work?

Code:
function submit_content(){
    
    // Email stuff
    $this->load->library('email');
    $this->load->helper('email');
    $message = "email message content";
    $this->email->to('[email protected]');
    $this->email->subject($_POST['email subject']);
    $this->email->message($message);
    $this->email->send();

    // db stuff
    $this->db->insert('db_name', $_POST);    
        
}

Thanks in advance,

Stompfrog
#2

[eluser]charlie spider[/eluser]
this can't be all of your code ?!?!

where's the rest of your db stuff ?
#3

[eluser]gon[/eluser]
Even if you managed to make this code work, you should first validate data at the controller using Validate class. Check the docs to see how.

Then, when having secure data inside an array, you could do the insert and send the email.
There shouldn't be any problem in calling both from the same controller. Doing a redirection looks like a not so good idea.
#4

[eluser]Stompfrog[/eluser]
Hi guys,

Thanks for the speedy response.

I am a bit confused though? Why do you think I have missing code?

I have...

Code:
class Submit extends Controller{
    
    function Submit(){
        parent::Controller();
    }
at the top and an index function which loads a simple submission form but nothing else.
As I said before both the email part and database part work if i comment out the other one they just seem to break when I run them together.

I appreciate that i will need some validation on my forms and have had a brief look at the validation class but I wanted to get all the basic functionality of the application working first before worrying about that.
#5

[eluser]Pascal Kriete[/eluser]
The code is perfect for testing purposes - it's as reduced as it gets. What happens if you insert first and then send the email? There should be no problem running them together.
#6

[eluser]Stompfrog[/eluser]
Putting the insert first makes no difference.

The data appears in the database and the page redirects to the thank you page but there is no email sent.

PS - I have added...
Code:
redirect('submit/thank_you/');
..to the bottom of the function.
#7

[eluser]charlie spider[/eluser]
can you post your entire controller as well as the form that the info is coming from ?
#8

[eluser]Stompfrog[/eluser]
This is the code I have got so far... I am even more confused now, emails have stopped coming through even when i comment out the insert now. Seems I am making things worse not better hehe.

Code:
<?php

class Submit extends Controller{
    
    function Submit(){
        parent::Controller();
    }
    
    function Index(){
        $this->load->helper('date');    
        $this->load->helper("form");
        $data['title'] = "My Submit Title";
        $this->load->view('static/header', $data);
        $this->load->view('submit_view');
        $this->load->view('static/footer');
    }
    
    function submit_art(){
        
        $this->load->library('email');
        $this->load->helper('email');
    
    
        $message = "Title: ".$_POST['art_title']."\n\n".
                    "Author: ".$_POST['author_id'];
            
        $this->email->from('[email protected]', "Generic name");
        $this->email->to('[email protected]');
        $this->email->subject($_POST['art_title']);
        $this->email->message($message);
        $this->email->send();
        //echo $this->email->print_debugger();    
        
        $this->db->insert('art', $_POST);    

        redirect('submit/thank_you/');
        
    }
    
    function thank_you(){
        $data['title'] = "My Contact Title";
        $this->load->view('static/header', $data);
        $this->load->view('thankyou_view');
        $this->load->view('static/footer');    
    }
    
}

?>

Code:
<?=form_open('submit/submit_art');?>
    <label for="art_title">art_title</label><br />
    &lt;input type="text" name="art_title" value="" size="60" /&gt;&lt;br /><br />
    
    <label for="author_id">author_id</label><br />
    &lt;input type="text" name="author_id" value="" size="60" /&gt;&lt;br /><br />
    
    &lt;input type="hidden" name="art_date" value="&lt;?php $datestring = "%Y-%m-%d  %h:%i:%s";$time = time(); echo mdate($datestring, $time); ?&gt;" size="60" /&gt;  
    <label for="art_desc">art_desc</label><br />
    &lt;textarea name="art_desc" cols="60" rows="10" &gt;&lt;/textarea><br /><br />
    
    <label for="art_screenshot">art_screenshot</label><br />
    &lt;input type="text" name="art_screenshot" value="" size="60" /&gt;&lt;br /><br />
    
    <label for="art_tags">art_tags</label><br />
    &lt;input type="text" name="art_tags" value="" size="60" /&gt;&lt;br /><br />
    
    &lt;input type="submit" class="submit" value="Insert" /&gt;
&lt;/form&gt;
#9

[eluser]charlie spider[/eluser]
it doesn't like your redirect
i don't know why

but instead of this:

redirect('submit/thank_you/');

you can use this:

$this->thank_you();


side note:
i don't think you need to load the email helper if you are using the email library

i'm assuming it doesn't like the redirect because the email you are sending essentially has header information, although that would be pretty dung-like if that's what was interfering
#10

[eluser]Stompfrog[/eluser]
Thanks for your help. "$this->thank_you();" is certainly good to know.

I can't get any emails working now, I went back to a comment form i made last week and that has stopped working too Sad

I must be doing something stupid, I'm sure i will figure it out eventually.




Theme © iAndrew 2016 - Forum software by © MyBB