crabman Posted December 7, 2008 Share Posted December 7, 2008 Hi all, I'm a noob to PHP and I'm hoping I can get some help for an issue I'm trying to resolve, it's been driving me insane... I'm trying to get a simple mailing list subscribe/unsubscribe form to work using a single textbox and two text links to subscribe or unsubscribe, but it's not working. Any advice would be appreciated. Here's the HTML I've got: <script language="JavaScript" type="text/javascript"> <!-- function mailinglist ( selectedtype ) { document.subscription.subtype.value = selectedtype ; document.subscription.submit() ; } --> </script> <form name="subscription" action="subscription.php" method="POST"> <input name="email" type="text"><br /> <input name="subtype" type="hidden"><a href="javascript:mailinglist('subscribe')">subscribe</a> <input name="subtype" type="hidden"><a href="javascript:mailinglist('unsubscribe')">unsubscribe</a> ... and here's the PHP: <?php if ($_POST['subscribe']) { $email = $_POST['email']; $recipient = "[email protected]"; $subject = "Subscribe"; $message = "..."; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "MIME-Version: 1.0\r\n"; mail($recipient, $subject, $message, $mailheader) or die("Failed."); include('header.html'); echo '<div id="featured">'; include('featured.html'); echo '</div>'; echo '<div id="columnone">'; echo "$email has been <b>added</b> to mailing list."; echo '</div>'; echo '<div id="columntwo">'; include('nextflyer.html'); echo '</div>'; echo '</body>'; echo '</html>'; } else if ($_POST['unsubscribe']) { $email = $_POST['email']; $recipient = "[email protected]"; $subject = "Unsubscribe"; $message = "..."; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "MIME-Version: 1.0\r\n"; mail($recipient, $subject, $message, $mailheader) or die("Failed."); include('header.html'); echo '<div id="featured">'; include('featured.html'); echo '</div>'; echo '<div id="columnone">'; echo "$email has been <b>removed</b> from the mailing list."; echo '</div>'; echo '<div id="columntwo">'; include('nextflyer.html'); echo '</div>'; echo '</body>'; echo '</html>'; } ?> If I leave the "else if" line as it is then the PHP page renders blank, and an email isn't sent. If I change the "else if" to "else with the argument removed then it processes the unsubscribe script regardless of which form option is chosen. If I change the POST argument in "if" or "else if", or both, then it processes the subscribe part of the script regardless of which form option is chosen. Thanks in advance, Crabman Link to comment https://forums.phpfreaks.com/topic/135901-solved-php-mail/ Share on other sites More sharing options...
gevans Posted December 7, 2008 Share Posted December 7, 2008 <script language="JavaScript" type="text/javascript"> <!-- function mailinglist ( selectedtype ) { document.subscription.subtype.value = selectedtype ; document.subscription.submit() ; } --> </script> <form name="subscription" action="subscription.php" method="POST"> <input name="email" type="text"><br /> <input name="subtype" type="hidden"><a href="javascript:mailinglist('subscribe')">subscribe</a> <a href="javascript:mailinglist('unsubscribe')">unsubscribe</a> only put the subtype input in once!! <?php if($_POST['subtype'] == 'subscribe') { $email = $_POST['email']; $recipient = "[email protected]"; $subject = "Subscribe"; $message = "..."; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "MIME-Version: 1.0\r\n"; mail($recipient, $subject, $message, $mailheader) or die("Failed."); include('header.html'); echo '<div id="featured">'; include('featured.html'); echo '</div>'; echo '<div id="columnone">'; echo "$email has been <b>added</b> to mailing list."; echo '</div>'; echo '<div id="columntwo">'; include('nextflyer.html'); echo '</div>'; echo '</body>'; echo '</html>'; } elseif ($_POST['subtype'] == 'unsubscribe') { $email = $_POST['email']; $recipient = "[email protected]"; $subject = "Unsubscribe"; $message = "..."; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "MIME-Version: 1.0\r\n"; mail($recipient, $subject, $message, $mailheader) or die("Failed."); include('header.html'); echo '<div id="featured">'; include('featured.html'); echo '</div>'; echo '<div id="columnone">'; echo "$email has been <b>removed</b> from the mailing list."; echo '</div>'; echo '<div id="columntwo">'; include('nextflyer.html'); echo '</div>'; echo '</body>'; echo '</html>'; } ?> and there's a couple of changes to your code!! see how that goes Link to comment https://forums.phpfreaks.com/topic/135901-solved-php-mail/#findComment-708469 Share on other sites More sharing options...
crabman Posted December 7, 2008 Author Share Posted December 7, 2008 only put the subtype input in once!! and there's a couple of changes to your code!! see how that goes That works a treat, thanks very much for the assistance Link to comment https://forums.phpfreaks.com/topic/135901-solved-php-mail/#findComment-708485 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.