This is my first post to this site, and I hope someone can help.
I have a newsletter signup system. The form calls the processing script, but upon running, it outputs to another page.
I would like it to process on same page, and output the appropriate message.
After some research, the closet I could find was this article: similar question
Here is the code i am using:
HTML
<h6 class="mailingHeadine">Join Our Mailing List</h6>
<!-- start newsletter -->
<form id="mailing_list" name="mailing_list" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST" >
<!-- start maillist -->
<div class="maillist">
<input type="text" id="e_mail" name="e_mail" value="Enter Your Email" onfocus="if(this.value=='Enter Your Email')this.value=''; this.className='email'" onblur="if(this.value=='')this.value='Enter Your Email'; this.className='email'" />
<p class="submit"><button type="submit" name="e_submit" value="Subscribe" id="submit">Join</button></p>
</div><!-- end maillist -->
</form><!-- end newsletter -->
<div id="formResponse" style="display: none;"></div>
IN PAGE SCRIPT
<script type="text/javascript">
$("#mailing_list").submit(function() {
$.post('link-to-script.php', {email: $('e_mail').val(), e_submit: 'yes'}, function(data) {
$("#formResponse").html(data).fadeIn('100');
$('#e_mail').val(''); /* Clear the inputs */
}, 'text');
return false;
});
</script>
PHP
<?php include('loader.php'); ?>
<?php include('head.php'); ?>
<?php
if(defined('SMTP') && SMTP == 'true')
{
include('class.phpmailer.php');
}
?>
<!-- MAIN -->
<div id="main" class="container-fluid">
<!-- container content -->
<div class="container-fluid">
<!-- row-fluid -->
<div class="row-fluid">
<?php if(isset($_POST['e_submit']) && strlen($_POST['e_mail']) !== 0) { ?>
<?php
// The system settings
include('settings.php');
// grab the user email
$email = $_POST['e_mail'];
// validate the email
if(substr_count($email,'@') == 1 && substr_count($email,'.') >= 1)
{
// check if user is already subscribed - avoids duplications
$results = $script->checkSubscription($email);
// subscribe the user
if(empty($results))
{
$subscribe_user = $script->subscribeUser($email);
// Notify Admin that an user as been subscribed
if($subscribe_user)
{
if($options['script_notifications'] == 'subscriptions' || $options['script_notifications'] == 'both')
{
$subject = 'Subscription Notification ID-'.mt_rand(0, 5).': Subscription for: '.$email;
$message = str_replace('{EMAIL}', $email, $options['script_notification_message']);
$message_send = str_replace('{ACTION}', $translate->__('cancels subscription'), $message);
if(defined('SMTP') && SMTP == 'false')
{
$from = $options['script_email'];
$headers = "From: {$from}\r\n";
$headers .= "X-Mailer: script\r\n";
$headers .= "Content-Type: text/html; charset=utf-8";
if($subscribed)
{
@mail($from, $subject, $message_send, $headers);
}
} elseif(defined('SMTP') && SMTP == 'true') {
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = SMTP_HOST;
if(defined('SMTP_AUTH') && SMTP_AUTH == 'true')
{
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
} else {
$mail->SMTPAuth = false;
}
$mail->From = $options['script_email'];
$mail->FromName = 'script';
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->AddAddress($options['script_email']);
$mail->Body = $message_send;
}
}
echo '
<div class="alert alert-success tac">
<img src="images/done.png" width="48" height="48" alt="done"/>
<p>Thank you for subscribing to our Newsletter.</p>
</div>
';
} else {
echo '
<div class="alert alert-error tac">
<img src="images/failed.png" width="48" height="48" alt="failed"/>
<p>Error occured while subscribing you.</p>
</div>
';
}
} else {
echo '
<div class="alert alert-error tac">
<img src="images/failed.png" width="48" height="48" alt="failed"/>
<p>You are already subscribed to our newsletter.</p>
</div>
';
}
} else {
?>
<div class="alert alert-error tac">
<img src="images/failed.png" width="48" height="48" alt="failed"/>
<p>Invalid E-mail Address</p>
</div>
<?php
}
?>
<?php } else { ?>
<div class="alert alert-error tac">
<img src="images/failed.png" width="48" height="48" alt="failed"/>
<p>You must have an e-mail address in order to subscribe to newsletter</p>
</div>
<?php } ?>
</div><!-- // .row-fluid -->
</div>
<!-- // end content -->
</div>
<!-- // END MAIN -->
</body>
</html>
It only kinda works. It runs through the PHP script and always outputs "You must have an e-mail address in order to subscribe to newsletter", the last line in the PHP code.
Any help would be greatly appreciated.