I'm fairly new to web development and PHP and i've been having trouble trying to get my contact form to work with the Bootstrap template
I am getting an error:
Error 405(Method Not Allowed):
send # jquery.min.js:2 ajax # jquery.min.js:2
(anonymous) # contact.js:18 dispatch # jquery.min.js:2
y.handle # jquery.min.js:2
contact.php
<?php
$from = "#example.com";
$sendTo = "#example.com";
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
index.html
<div class="main-form">
<div class="container">
<div class="row">
<!-- contact form -->
<div class="col-md-offset-2 col-md-8">
<form class="form" id="contact-form" method="POST" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_name" type="text" name="name" placeholder="Name *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_email" type="email" name="email" placeholder="Email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="subject" name="subject" placeholder="Subject">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" placeholder="Message *" rows="4" required="required" data-error="Message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<input type="submit" class="button" value="Send message">
</div>
</form>
</div>
</div>
</div>
</div>
Thank you for any help
javascript
$(window).on("load",function (){
$(".loading").fadeOut(500);
// contact form
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
});
});
Related
I use this contact form by Boostrapious and works well, but I can't find solution to add some checkbox inputs and send values by email. I added HTML inputs, but I need help with php code. Can anyone help me?
This is a link to this tutorial and code:
https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"> </div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Firstname *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Lastname *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group checkbox">
<label for="checkboxoptions1">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions1" value="checkboxoptions1">
Option1
</label>
<label for="checkboxoptions2">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions2" value="checkboxoptions2">
Option2
</label>
<label for="checkboxoptions3">
<input type="checkbox" name="checkboxoptions[]" id="checkboxoptions3" value="checkboxoptions3">
Option3
</label>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required. Contact form template by
Bootstrapious.</p>
</div>
</div>
</div>
</form>
and php code here
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'Demo contact form <demo#domain.com>';
// an email address that will receive the email with the output of the form
$sendTo = 'Demo contact form <demo#domain.com>';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('checkboxoptions' => 'My options','name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the necessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $_POST['email'],
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
I'm trying to do contact form so my clients can send me a message, but when I press send, everything ok, it says contact form sent, but I don't receive anything in my mail. I use gmail, I can use own domain mail too. Can anyone help me fix it? Thanks.
I have server running with linux ubuntu 18.04, with apache web server.
This is the contact.php file. I see everything ok.
Contact.php:
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'Contact form';
// an email address that will receive the email with the output of the form
$sendTo = 'mymail#gmail.com';
// subject of the email
$subject = 'New message from contact form';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
This code is in index.html, one section.
Contact.html:
<section class="contact-1">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-6 col-md-12">
<img class="img-center" src="images/banner/06.png" alt="">
</div>
<div class="col-lg-6 col-md-12 md-mt-5">
<div class="section-title">
<div class="title-effect title-effect-2">
<div class="ellipse"></div> <i class="la la-info"></i>
</div>
<h2>Свяжитесь с нами</h2>
<p>Свяжитесь с нами и расскажите, чем мы можем вам помочь. Заполните все поля, и мы скоро свяжемся с вами.</p>
</div>
<form id="contact-form" method="post" action="php/contact.php">
<div class="messages"></div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Имя" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Фамилия" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Электронная почта" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Телефон" required="required" data-error="Заполните это поле">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Сообщение" rows="4" required="required" data-error="Заполните это поле"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12 mt-2">
<button class="btn btn-theme btn-circle" data-text="Send"><span>О</span><span>т</span><span>п</span><span>р</span><span>а</span><span>в</span><span>и</span><span>т</span><span>ь</span>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
i have this problem. I made a bootstrap form with a php included and instead of displaying a message if the form is filled and sent successfuly i need it to redirect to a different page (.html).
This is the PHP page for the form:
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
// an email address that will be in the From field of the email.
$from = 'email#email.com';
// an email address that will receive the email with the output of the form
$sendTo = 'email#mail.cz';
// subject of the email
$subject = 'Nová zpráva z Vašeho webu';
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' =>
'Phone', 'email' => 'Email', 'message' => 'Message');
// message that will be displayed when everything is OK :)
$okMessage = 'Děkuji! Vaše zpráva byla úspěšně odeslána. ';
// If something goes wrong, we will display this message.
$errorMessage = 'Omlouvám se, ale došlo k nečekaně chybě. Zkuste to prosím
později.';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by
error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
And here is the HTML for the form:
<form id="contact-form" method="post" action="contact.php">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Jméno *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Vaše křestní jméno *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Příjmení *</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Vaše příjmení *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Váš platný email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_phone">Telefon</label>
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Váš telefon">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Vaše zpráva *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Vaše zpráva *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
<br>
</div>
<div class="col-md-12">
<input type="submit" class="odeslat" value="Odeslat">
</div>
</div>
<div class="row">
<div class="col-md-12">
<br><p class="text-muted"><strong>*</strong> Tyto pole jsou povinná.</p>
</div>
</div>
</div>
</form>
can u pls help me fix it so it doesn't display only the message?
Thank you
You can write in the end a if sentence that shows this echo in the true case and doesn't show in a false case, some like that ->
if(!$ejecutar)
{
echo "whatever you want";
}
else
{
echo "Whatever you want<br><a href='index.php'>Volver</a>";
}
Currently, I'm running a php script, with js validator & success message pop-up, which currently works fine.
I've recently been experimenting with an update to the website, removing the html tags via the htaccess file so the URL looks better in the address bar. The whole site works great, with the new links etc. which all display fine. However, the php email script and js validator now don't work, and the emails are no longer generating. The relevant code is here:
HTML (the page title is contact-us.html):
<form id="contact-form" method="post" action="http://www.mywebsite.co.uk/contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="form_name">Name *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your name *" required="required" data-error="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email address *" required="required" data-error="Please enter a valid email address.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="form_phone">Telephone</label>
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your telephone number">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Your mssage *" rows="4" required="required" data-error="Please enter your message"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<!-- Replace data-sitekey with your own one, generated at https://www.google.com/recaptcha/admin -->
<div class="g-recaptcha" data-sitekey="XXXXXXXX"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12"><br>
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
Contact.php is as follows:
<?php
// require ReCaptcha class
require('recaptcha-master/src/autoload.php');
// configure
$from = 'contact#mywebsite.co.uk';
$sendTo = 'me#mywebsite.co.uk';
$subject = 'New message mywebsite.co.uk';
$fields = array('name' => 'Name', 'phone' => 'Phone', 'e
' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.';
$errorMessage = 'There was an error while submitting the form. Please reload the page and try again.';
$recaptchaSecret = 'SECRETKEYXXX';
{
if (!empty($_POST)) {
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
$emailText = "You have a new message from MyWebsite.co.uk\n============================= \n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
Contact.js (which is linked to after the footer of contact-us.html code):
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
grecaptcha.reset();
}
}
});
return false;
}
})
});
And finally, the part of htaccess shortening the URLs:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Because you are changing .php extension to .html you need to change your form:
<form id="contact-form" method="post" action="http://www.mywebsite.co.uk/contact.html" role="form">
var url = "contact.html";
The normal way would be to remove extensions and create "pretty urls", then you would do:
<form id="contact-form" method="post" action="http://www.mywebsite.co.uk/contact" role="form">
var url = "/contact";
I really suggest creating pretty URLs.
Instead of sending header array
make it string as
// Set content-type header for sending HTML email
$headers= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: your name<my#domain.com>' . "\r\n";
$headers .= 'Return-Path: mail#testsite.com' . "\r\n";
$headers .= 'reply-to: your name<my#domain.com>' . "\r\n";
$headers .= "Cc: another name < name#domain.com >\n";
also i did not get any console error and i check network tab i get 200 ok message that means data is posted.. please help
Here is jquery code
$(document).ready(function() {
$('#mc_subscribe').submit(function() {
$('#response').html('form submitting....');
var form = $(this);
$.ajax({
url: $(this).prop('action'),
type: 'POST',
dataType: 'json',
data: form.serialize()
}).done(function(data){
if (data.success == true){
$('#mc_subscribe')[0].reset();
$('#response').addClass('alert-success').removeClass('alert-danger').html(data.message);
window.setTimeout(function() {
window.location.href = data.redirectURL;
}, 2000);
} else{
$('#response').addClass('alert-danger').removeClass('alert-success').html(data);
}
}).fail(function( jqXHR, textStatus, data ) {
alert( "Request failed: " + data );
});;
return false;
});
});
Here is my codignitor controller code
class Subscribe extends CI_Controller {
private $data;
public function index() {
if ($this->input->is_ajax_request()){
$result = $this->_subscribe();
echo json_encode($result);
exit;
}
$this->load->view('header');
$this->load->view('pages/subscribe', $this->data);
$this->load->view('footer');
}
private function _subscribe(){
$success = true;
$redirectURL = base_url() + '/pricing';
if(!$this->input->post('FNAME')){
$message = "No Full Name provided";
$success = false;
}
if( empty($this->input->post('EMAIL')) || !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_POST['EMAIL']) ){
$message = "No Email address provided";
$success = false;
}
if($success && !$this->input->post('MOBILE')){
$message = "No Phone Number provided";
$success = false;
}
if($success && !$this->input->post('DAYTIME')){
$message = "No Day & Time provided";
$success = false;
}
if ($success){
$this->load->library('Mcapi', array(
'apikey' => $this->config->item('mailchimp_key')
));
$api = new Mcapi($this->config->item('mailchimp_key'));
$list_id = $this->config->item('mailchimp_list_id');
$merge_vars = Array(
'FNAME' => $this->input->post('FNAME'),
'MOBILE' => $this->input->post('MOBILE'),
'DAYTIME' => $this->input->post('DAYTIME')
);
if($api->listSubscribe($list_id, $_POST['EMAIL'], $merge_vars, '') === true) {
// It worked!
$message = 'Success!';
} else {
$success = false;
// An error ocurred, return error message
$message = 'Error: ' . $api->errorMessage;
}
}
return array('message' => $message, 'success' => $success, 'redirectURL' => $redirectURL);
}
}
and here is HTML form
<div class="container">
<div class="col-md-12">
<form id="mc_subscribe" class="form-horizontal" action="http://localhost/CI-2/Subscribe/" method="post">
<fieldset>
<!-- Form Name -->
<legend>SUBSCRIBE</legend>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="FNAME" type="text" placeholder="Full Name" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="EMAIL" type="email" placeholder="Email" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="MOBILE" type="text" placeholder="Phone Number" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<input id="" name="DAYTIME" type="text" placeholder="Day & Time" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-8">
<input type="submit" name="submit" class="btn btn-success" value="SUBSCRIBE">
</div>
</div>
</fieldset>
</form>
<p id="response" class="alert text-center" align="center"></p>
</div>
</div>
change this below line
return array('message' => $message, 'success' => $success, 'redirectURL' => $redirectURL);
TO
$data = array();
$data['message'] = $message ;
$data['success'] = $succcess;
$data['redirectURL'] = $redirectURL;
return json_encode($data);
in your controller.
it seems you are not returning the data in the json format,