bolty2uk Posted April 8, 2007 Share Posted April 8, 2007 Hello.. After quite a bit of reading up and playing about i just cant seem to find an answer that will work for something which should be very easy and straight forward. Basically i have a self-validating php form that sends the users input to my email inbox. What i need to do on the form is add a single check box (which i have already done) which the users will just tick if they wish to receive a newsletter.. Now my problem is i just cant get my head around the code i will have to use to send this details to me. All i need to receive is a simple "yes" in the email i get if they checked the box and nothing if they didn't... I just don't know how to achieve this.... Here is the code i am working with.... <?php $to = "admin@mymail.com"; if (!isset($_POST['submit'])) { showForm(); } else { $error = 0; if(empty($_POST['name'])) { $error = 1; $errstr[] = "Please enter a name"; } if(!preg_match("/^(?:[\w\d]+\.?)+@(??:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) { $error = 1; $errstr[] = "Please enter a valid email address"; } if(empty($_POST['message']) || preg_match("/^enter your message here$/i", $_POST['message'])) { $error = 1; $errstr[] = "Please enter a message"; } if(empty($_POST['imagetext'])) { $error = 1; $errstr[] = "Please validate the security code"; } else { include "securimage.php"; $img = new securimage(); $valid = $img->check($_POST['imagetext']); if(!$valid) { $error = 1; $errstr[] = "The security code you entered was incorrect"; } } if ($error == 1) { echo "<center>\n<font style=\"color: #FF0000\">\n"; foreach($errstr as $err) { echo "<li> " . $err . "</li>\n"; } echo "</font>\n</center>\n<br />\n\n"; showForm(); } else { @mail($to, "Enquiry","On" . date("r") . ", " . $_POST['name'] . " from " . $_POST['email'] . " sent the following message.\nEnquiry - " . $_POST['reason'] . "\n\n" . "\n\n" . stripslashes($_POST['message']), "From: " . $_POST['email']); echo "<font style=\"color: #003366\"><left>\n<b>Thank You.</b><br /><br /> Your contact form submission has now been successfully sent to us.<br /><br />One of our team will be responding to your enquiry very shortly.</font>"; } } //else submitted function showForm() { $_POST['message'] = @htmlspecialchars(@$_POST['message']); echo <<<EOD <form method="POST"> <table cellpadding="5" cellspacing="1" width="90%" align="left"> <tr> <td class="style3">Name:</td> <td align="left"><input type="text" name="name" size="40" value="{$_POST['name']}" /></td> </tr> <tr> <td class="style3">Email:</td> <td align="left"><input type="text" name="email" size="40" value="{$_POST['email']}" /></td> </tr> <tr> <td class="style3">Enquiry:</td> <td align="left"><input type="text" name="reason" size="40" value="{$_POST['reason']}" /></td> </tr> <tr> <td class="style3">Message:</td> <td align="left"><textarea name="message" rows="5" cols="35">{$_POST['message']}</textarea></td> </tr> <tr> <td align="right" class="style3"><input name="subscribe" type="checkbox" value="{$_POST['subscribe']}" /></td> <td align="left" class="style2">Subscribe me to the news letter.</td> </tr> <tr> <td align="center" colspan="2"><img src="securimage_show.php" alt="security code"></img></td> </tr> <tr> <td> </td> <td class="style4">(Enter above code) <input type="text" size="20" name="imagetext" /></td> </tr> <tr> <td> </td> <td class="body" align="left" colspan="2" ><input type="submit" name="submit" value="Submit" /> <input name="reset" type="reset" id="reset" tabindex="5" value="Reset" /></td> </tr> </table> </form> EOD; } ?> I know this must be relatively straight forward but im afraid on this occasion i just cant seem to see the wood from the trees... If anyone can offer any advice or know of a good tutorial it would be most appreciated... Thanks in advance for any responses this may produce. Quote Link to comment Share on other sites More sharing options...
metrostars Posted April 8, 2007 Share Posted April 8, 2007 Just make the checkbox checked value Yes. Then in the script, do a simple if statement if($_POST['checkboxname'] == 'Yes') {$newsletter = "Yes";} {else $newsletter = 'No';} then put another bit in the mail function and you'll be flying. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 ($_POST['checkboxname'] == 'yes') ? $newsletter = "Yes" : $newsletter = "No"; Try that, its actually better than the one the guy above posted. Quote Link to comment Share on other sites More sharing options...
bolty2uk Posted April 8, 2007 Author Share Posted April 8, 2007 Thanks Guys.... your code did the trick.. Really is much appreciated. Thank you :-X Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.