Here are the steps to use CodeIgniter Ajax to display an error message when sending an email response:
public function send_email() {
$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('recipient@example.com');
$this->email->subject('Email Subject');
$this->email->message('Email Message');
if($this->email->send()) {
$response['status'] = 'success';
} else {
$response['status'] = 'error';
$response['message'] = $this->email->print_debugger();
}
echo json_encode($response);
}
In your view, add a form with appropriate fields for email information and a submit button.
Add a script to the view that will handle the form submission and display a message based on the AJAX response. Example:
$(document).ready(function(){
$('#email-form').on('submit', function(e){
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: '<?php echo base_url('email/send_email'); ?>',
type: 'POST',
dataType: 'json',
data: formData,
success: function(response){
if (response.status == 'success') {
alert('Email sent successfully!');
} else {
alert('Error sending email: ' + response.message);
}
},
error: function(){
alert('Error sending email. Please try again later.');
}
});
});
});
Replace the URL in the AJAX request with the appropriate URL for your controller function.
Style the error message appropriately in your CSS.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-22 20:26:54 +0000
Seen: 10 times
Last updated: Jun 22 '23
How can a HTTP header be inserted in WordPress?
How can data be stored in a data attribute and utilized through Ajax?
How can an array be passed using typo3 flexform xml and itemsProcConfig?
Does JSON encode fail to retrieve data from the database?
Can the GS1 128 barcode decoder in PHP or Jquery be utilized?
How can Xdebug be used in conjunction with VSCode for Laravel on Sail and WSL2?
In PHP, what is the method for finding encrypted data using the "like" operator?
What is the method to pass a variable from PHP back to JavaScript?
Why do PHP variable variables display unexpected behavior when used with arrays?