Welcome Guest, Not a member yet? Register   Sign In
Store form data in database and email it to email address x
#1

[eluser]rhand_ci[/eluser]
Just started using CI about two weeks ago. Did a few tutorials and am now trying to make my first form that already stores data into a table even though validation is not done yet. What I am wondering about is how I can send this data to an email address and store it into a database at the same time. Here is the controller code I have made so far:
Code:
<?php

class Rentalform extends Controller {
    // Extend controller with new one
    
    function Rentalform()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url')); // load two helpers
        $this->load->library('form_validation'); // load form validation from the library
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        
    }
    
    function index()
    {
        $data['title'] = "Rental Form";
        $data['heading'] = "Rental Form Heading";
        
        $this->load->view('rentalform_view', $data);
    }
    
    function insert_rentalform()
    {
        $this->db->insert('rentalform', $_POST);
        redirect('rentalform');
        
    }
}

And here is the view:
Code:
<html>
<head>
<link href="<?=base_url()?>style.css" rel="stylesheet" type="text/css">
<title><?=$title?></title>
</head>
<body>
<h1>&lt;?=$heading?&gt;</h1>
    &lt;?=form_open('rentalform/insert_rentalform'); ?&gt;
    <div style="float:left;">
    <h3>Company name</h3>
    <p>&lt;input type="text" name="company_name" /&gt;&lt;/p>
    <h3>Company address</h3>
    <h4>street name</h4>
    <p>&lt;input type="text" name="company_street_name" /&gt;&lt;/p>
    <h4>post code</h4>
    <p>&lt;input type="text" name="company_post_code" /&gt;&lt;/p>
    <h4>City</h4>
    <p>&lt;input type="text" name="company_city" /&gt;&lt;/p>
    <h4>Country</h4>
    <p>&lt;input type="text" name="company_country" /&gt;&lt;/p>
    <h3>Phone Number</h3>
    <p>&lt;input type="text" name="company_phone" /&gt;&lt;/p>
    <h3>Email address</h3>
    <p>&lt;input type="text" name="company_email" /&gt;&lt;/p>
    </div>
    <div style="float:right;">
    <h3>Price range</h3>
    <h4>Small</h4>
    <p>&lt;input type="radio" name="company_price_range_small" value="50%" /&gt;&lt;/p>
    <h4>medium</h4>
    <p>&lt;input type="radio" name="company_price_range_medium" value="25%" /&gt;&lt;/p>
    <h4>large</h4>
    <p>&lt;input type="radio" name="company_price_range_large" value="10%" /&gt;&lt;/p>
    <h4>Largest</h4>
    <p>&lt;input type="radio" name="company_price_range_largest" value="5%" /&gt;&lt;/p>
    <h3>Description</h3>
    <p>&lt;textarea name="company_description" rows="10"&gt;&lt;/textarea></p>
        <p>&lt;input type="submit" value="Submit Comment" /&gt;&lt;/p>
        </div>
        &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Is what I want possible? Any hints how to would be really appreciated!
#2

[eluser]Aidy[/eluser]
Hey

I would take a look at the email class in the user guide..
#3

[eluser]gcc.programmer[/eluser]
You can't store the info into the database, and send the email literally at the 'same time', i.e. in parallel, but you certainly can do both. Codeigniter offers an
EMAIL CLASS
and also a rich set of
DATABASE CLASSES
that will allow you to do this very thing. Best of all, using codeigniter makes this quite easy, and fun!
;-)
#4

[eluser]rhand_ci[/eluser]
@ Aidy thanks for the tip...
@ gcc.programmer Thanks for the tip + links

Will read Email helper again I wil check out and Email library and see if I can combine storing data into a database, which is working and sending it to an email address afterward.
#5

[eluser]Neeraj Kumar[/eluser]
Following Pseudo Code may help you!!

1. Fetch user data.
2. Process data using form_validation. If valid goto step 3 or goto step 1.
3. Prepare and SQL Query like: 'INSERT INTO table_name VALUES...'
4. Fire Query using $this->db->query();
5. Then Initialize Email class.
6. Send an Email to the the person
7. Check if email has been successfully sent or not. If successfully sent then exit otherwise goto next step
8. Delete the last entry from the database and throw an error

I hope it helps!!
Cheers
#6

[eluser]rhand_ci[/eluser]
@ Codemaster Snake

Thanks for this great reply. Here some more questions in response to your post:
[quote author="Codemaster Snake" date="1266001306"]
1. Fetch user data.
[/quote]
I do that using
Code:
$this->db->insert('rentalform', $_POST);
but I am sure you mean something else
[quote author="Codemaster Snake" date="1266001306"]
2. Process data using form_validation. If valid goto step 3 or goto step 1.
[/quote]
Validation should not be to hard, but has no priority yet as I try to store data and send it by email first..
[quote author="Codemaster Snake" date="1266001306"]
3. Prepare and SQL Query like: 'INSERT INTO table_name VALUES...'
[/quote]
Why not just use:
Code:
$this->db->insert('rentalform', $_POST);
? Forgive my ignorance but I am still learning...

[quote author="Codemaster Snake" date="1266001306"]
4. Fire Query using $this->db->query();
[/quote]
Query to get the inserted data? How can I query data just sent so I email what was sent and not all table data?
[quote author="Codemaster Snake" date="1266001306"]
5. Then Initialize Email class.
[/quote]
Found this:
Code:
$this->load->library('email');

$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->cc('[email protected]');
$this->email->bcc('[email protected]');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

echo $this->email->print_debugger();
I guess that is what you mean?
[quote author="Codemaster Snake" date="1266001306"]
6. Send an Email to the the person
[/quote]
using:
Code:
$this->email->send();
?
[quote author="Codemaster Snake" date="1266001306"]
7. Check if email has been successfully sent or not. If successfully sent then exit otherwise go to next step
[/quote]
[quote author="Codemaster Snake" date="1266001306"]
8. Delete the last entry from the database and throw an error
[/quote]

Why delete the last entry? I would like to store data in database AND email to email address x...
#7

[eluser]rhand_ci[/eluser]
Just tested sendmail with script I found on CI. I added script to my first controller:
Code:
&lt;?php

class Rentalform extends Controller {
    // Extend controller with new one
    
    function Rentalform()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url')); // load two helpers
        $this->load->library('form_validation'); // load form validation from the library
        $this->load->library('email');
        
    }
    
    function index()
    {
        $data['title'] = "Rental Form";
        $data['heading'] = "Rental Form Heading";
        $this->load->view('rentalform_view', $data);
        $this->load->library('email');
        $config['protocol'] = 'sendmail';  
        $this->email->initialize($config);
        $this->email->from('[email protected]', 'thephpx');
        $this->email->to('[email protected]');
        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');
        $this->email->send();
        echo $this->email->print_debugger();
    }
    
    function insert_rentalform()
    {
        $this->db->insert('rentalform', $_POST);
        redirect('rentalform');
        
    }
}

And got:

Code:
Your message has been successfully sent using the following protocol: sendmail

User-Agent: CodeIgniter
Date: Fri, 12 Feb 2010 17:39:59 +0300
From: "thephpx"
Return-Path:
To: [email protected]
Subject: =?utf-8?Q?Email_Test?=
Reply-To: "[email protected]"
X-Sender: [email protected]
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0


Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Testing the email class.

So now I just need to read the $_POST array data and add it to the email body. Still working on that...
#8

[eluser]jeffpeck[/eluser]
You can pick up the post data using $this->input->post('INPUT_NAME')

INPUT_NAME is the name of the input from the previous page.

So, if you have:
&lt;input type="text" name="first_name"/&gt;
&lt;input type="text" name="last_name"/&gt;

You can get those values on submit through $this->input->post('first_name') and $this->input->post('last_name')
#9

[eluser]rhand_ci[/eluser]
Thanks a lot Jeffpeck! Going to work on this a bit more this weekend. Will post back as soon as I have some results.
#10

[eluser]rhand_ci[/eluser]
With the following code data is stored in my table and sent to the designated email address:
Code:
&lt;?php

class Rentalform extends Controller {
    // Extend controller with new one
    
    function Rentalform()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url')); // load two helpers
        $this->load->library('form_validation'); // load form validation from the library
        $this->load->library('email');
        
    }
    
    function index()
    {
        $data['title'] = "Rental Form";
        $data['heading'] = "Rental Form Heading";
        $this->load->view('rentalform_view', $data);
        
    }
    
    function insert_rentalform()
    {
        $this->db->insert('rentalform', $_POST);
        //redirect('rentalform');
        $message = $this->input->post('company_name');
        $this->load->library('email');
        $config['protocol'] = 'sendmail';  
        $this->email->initialize($config);
        $this->email->from('[email protected]', 'Test address');
        $this->email->to('me@localhost');
        $this->email->subject('Email Test');
        $this->email->message($message);
        $this->email->send();
        echo $this->email->print_debugger();
        
    }
}

Just what I needed :-) Now I just need to work on adding more field data to the body in a nice way, validate the form data and improving the controller.




Theme © iAndrew 2016 - Forum software by © MyBB