Welcome Guest, Not a member yet? Register   Sign In
Contact Form - Help?
#1

[eluser]ceneb[/eluser]
I'm pretty stuck at a small hurdle and would appreciate any help.

Basically, I'm trying to create a contact form using CI.

This is it at the moment: http://www.ceneb.com/index.php/contact

It seems to work but I don't see how it sends to me.

My current code is on the view:

Code:
<div id="main-content">
    <div id="content">
        <h3>Contact Us</h3>
        &lt;?php echo validation_errors(); ?&gt;
        &lt;form action="contact" method="post" id="contactForm"&gt;
           <div class="padding"> Your Name: </div>
           &lt;input type="text" name="name" id="name" size="35"/&gt; <br />
           <div class="padding"> Your Email: </div>
           &lt;input type="text" name="email" id="email" size="35"/&gt; <br />
           <div class="padding"> Comments: </div>
           &lt;textarea id="comments" name="comments" &gt;&lt;/textarea> <br />
           &lt;input type="submit" id="submit" value="Submit"/&gt;
        &lt;/form&gt;
    </div>
</div>

and on the controller:

Code:
&lt;?php
class Contact extends Controller {

function Contact() {
        parent::Controller();
    $this->load->helper(array('form', 'url'));

    
    $this->load->library('form_validation');
        
    $this->form_validation->set_rules('name', 'Name', 'required|min_length[1]');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('comments', 'Comments', 'required|min_length[5]');
      
}

    function index()
    {

        $this->load->view('header');
            if ($this->form_validation->run() == FALSE) {
        $this->load->view('contact');
    } else {
        $this->load->view('contactsuccess');
    }
        $this->load->view('footer');
    }
}
?&gt;

Thanks in advance,
Peter
#2

[eluser]Kromack[/eluser]
Rather than to only load the success view, you also have to send the email that will list all form post data.

For exemple : http://ellislab.com/codeigniter/user-gui...email.html

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

Good luck Wink
#3

[eluser]ceneb[/eluser]
Thanks for your reply. I looked at that documentation before and I though perhaps I just needed to add $this->email->to('[email protected]'); to the controller but it gave an error. I think it's going to take me some time to get used to CI. I've added:

Quote:$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();

to it now and it does now send but the subject doesnt change nor does the senders email and it seems to send out twice.

This is the code now:

Code:
&lt;?php
class Contact extends Controller {

function Contact() {
        parent::Controller();
    $this->load->helper(array('form', 'url'));

$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();
    
    $this->load->library('form_validation');
        
    $this->form_validation->set_rules('name', 'Name', 'required|min_length[1]');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('comments', 'Comments', 'required|min_length[5]');
      
}

    function index()
    {

        $this->load->view('header');
            if ($this->form_validation->run() == FALSE) {
        $this->load->view('contact');
    } else {
        $this->load->view('contactsuccess');
    }
        $this->load->view('footer');
    }
}
?&gt;

EDIT: forgot to add it has additional text in the top right of the page.

Thanks!
#4

[eluser]summery[/eluser]
Hi Ceneb,

Maybe you are receiving the email multiple times because you have the same email address in the to:, cc: and bcc: fields?

About the subject not changing, have you tried changing this line of code:
Code:
$this->email->subject('Email Test');

to something like:
Code:
$this->email->subject('This is my new subject line');

Hope this helps, good luck!
#5

[eluser]ceneb[/eluser]
What I need is the subject and email etc to be what the user fills out in the contact form if that makes sense.

Thanks,
Peter
#6

[eluser]Twisted1919[/eluser]
Your problem is your constructor .
in the constructor just load the email and the form-validation library .
The rest of the logic goes to your method .
In the way you posted , an email is send everytime when the contact page is accessed, even if the form validation fails or not .
#7

[eluser]summery[/eluser]
Oh, of course.

Your initial form includes the following fields: name, email, comments. Since the user isn't entering a subject line, just use a generic one in your email.

Try:
Code:
$this->email->subject('New website comment');

$message  = "Name: " . $this->input->post('name') . "\n";
$message .= "Email: " . $this->input->post('email') . "\n\n";
$message .= "Comments: " . $this->input->post('comments') . "\n";

$this->email->message($message);




Theme © iAndrew 2016 - Forum software by © MyBB