Reputation: 49
I have this code written already by someone else, the problem is when sending the email it always show "Error Sending!" even that most of the times the email is sent successfully.
View (Form submission):
<!-- Contact Form Starts -->
<div class="col s12 m7 l9 xl9 rightside">
<h6 class="uppercase m-none">Feel free to drop me a line</h6>
<div class="row">
<p class="col s12 m12 l7 xl7 second-font">
If you have any suggestions or consultations, please fill out the form below and I will reply shortly.
</p>
</div>
<form class="contactform" method="post" action="<?php echo site_url('en/send') ?>">
<!-- Name Field Starts -->
<div class="input-field second-font">
<i class="fa fa-user prefix"></i>
<input id="name" name="name" type="text" class="validate" required>
<label class="font-weight-400" for="name">Your Name</label>
</div>
<!-- Name Field Ends -->
<!-- Email Field Starts -->
<div class="input-field second-font">
<i class="fa fa-Email prefix"></i>
<input id="email" type="email" name="email" class="validate" required>
<label for="email">Your Email</label>
</div>
<!-- Email Field Ends -->
<!-- Start Message Textarea Starts -->
<div class="input-field second-font">
<i class="fa fa-comments prefix"></i>
<textarea id="message" name="message" class="materialize-textarea" required></textarea>
<label for="message">Your message</label>
</div>
<!-- Message Textarea Ends -->
<!-- Submit Form Button Starts -->
<div class="col s12 m12 l4 xl4 submit-form">
<button class="btn font-weight-500" type="submit" name="send">
Send Message <i class="fa fa-send"></i>
</button>
</div>
<!-- Submit Form Button Ends -->
<div class="col s12 m12 l8 xl8 form-message">
<span class="output_message center-align font-weight-500 uppercase"></span>
</div>
</form>
</div>
<!-- Contact Form Ends -->
Controller (send function):
function send()
{
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = '
<strong>Name: </strong>'.$this->input->post("name").'<br />
<strong>Email: </strong>'.$this->input->post("email").'<br />
<strong>Message: </strong>'.$this->input->post("message").'<br />
';
$emailto = '[email protected]'; //any email
$this->load->library('email');
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
$this->email->send();
$data['contactus'] = $this->Emodel->contactus('en');
$data['pages'] = $this->Emodel->get_pages('en');
$data['social'] = $this->Emodel->get_header_social();
$data['result'] = "Message Sent Successfully";
$data['_title'] = "Contact";
$this->load->view('en/home',$data);
}
JS file:
(function($) {
"use strict";
$(document).ready(function() {
// PRELOADER
$("body").toggleClass("loaded");
setTimeout(function() {
$("body").addClass("loaded");
}, 3000);
// PORTFOLIO DIRECTION AWARE HOVER EFFECT
var item = $("#bl-work-items>div");
var elementsLength = item.length;
for (var i = 0; i < elementsLength; i++) {
$(item[i]).hoverdir();
}
// TEXT ROTATOR
$("#selector").animatedHeadline({
animationType: "clip"
});
// BOX LAYOUT
Boxlayout.init();
// REMOVE # FROM URL
$("a[href='#']").on("click", (function(e) {
e.preventDefault();
}));
// AJAX CONTACT FORM
$(".contactform").on("submit", function() {
$(".output_message").text("Loading...");
var form = $(this);
$.ajax({
url: form.attr("action"),
method: form.attr("method"),
data: form.serialize(),
success: function(result) {
if (result == "success") {
$(".contactform").find(".output_message").addClass("success");
$(".output_message").text("Message Sent!");
} else {
$(".contactform").find(".output_message").addClass("error");
$(".output_message").text("Error Sending!");
}
}
});
return false;
});
// MATERIAL CAROUSEL
$(".carousel.carousel-slider").carousel({
fullWidth: true,
indicators: true,
});
// RESUME CARDS ANIMATION
if ($(window).width() > 800) {
$(".resume-list-item, .resume-card").on("click", function() {
$(".resume-list-item").removeClass("is-active");
var e = parseInt($(this).data("index"),10);
$("#resume-list-item-" + e).addClass("is-active");
var t = e + 1,
n = e - 1,
i = e - 2;
$(".resume-card").removeClass("front back up-front up-up-front back-back"), $(".resume-card-" + e).addClass("front"), $(".resume-card-" + t).addClass("back"), $(".resume-card-" + n).addClass("back-back"), $(".resume-card-" + i).addClass("back")
});
}
});
})(jQuery);
I don't have much knowledge in ajax and jquery so I copied the whole code
Upvotes: 0
Views: 458
Reputation: 1146
Because of Your Controller doesn't pass success data to the view use this code.
function send()
{
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = '
<strong>Name: </strong>'.$this->input->post("name").'<br />
<strong>Email: </strong>'.$this->input->post("email").'<br />
<strong>Message: </strong>'.$this->input->post("message").'<br />
';
$emailto = '[email protected]'; //any email
$this->load->library('email');
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
if ($this->email->send()) {
echo 'success';
} else {
echo 'error';
}
}
Upvotes: 1