umdart35 Posted August 3, 2010 Share Posted August 3, 2010 Hey I'm a beginner at this and I was hoping the answer might be something simple. I'll post the code if needed, but it might just be something simple. I have a contact form with a php script to email me when a user enters information. I have a checkbox for whether or not a user wants to receive our newsletter. When a user fills in his/her information. Clicks the checkbox to receive our newsletter, then clicks submit they are then taken to a thank you page and I receive an email with their info, etc. When a user fills in his/her information. DOES NOT click the checkbox, then clicks submit, nothing on the screen changes, but I still receive the email with their info. Why is this happening? What am I missing with this checkbox? How do I get the thank you page to still display if they DO NOT click the checkbox? Any thoughts would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/ Share on other sites More sharing options...
Skewled Posted August 3, 2010 Share Posted August 3, 2010 Post some code for what your working with, you'd have to put a condition on the submit action to determine what to do with the form contents if that button is checked or unchecked. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094619 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 <?php $visitorf = $_POST['visitorf']; $visitorl = $_POST['visitorl']; $visitorphone = $_POST['visitorphone']; $visitormail = $_POST['visitormail']; $visitorzip = $_POST['visitorzip']; $notes = $_POST['notes']; $attn = $_POST['attn']; if (eregi('http:', $notes)) { die ("Do NOT try that! ! "); } if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) { echo "<h2>Please hit back and enter a valid email address</h2>\n"; $badinput = "<h2>Your information was not sent</h2>\n"; echo $badinput; die ("Thank you"); } if(empty($visitorf) || empty($visitorl) || empty($visitorphone) || empty($visitormail) || empty($visitorzip)) { echo "<h2>Please fill in all fields marked with a *</h2>\n"; die ("Please hit back"); } $todayis = date("l, F j, Y, g:i a") ; $attn = $attn ; $subject = $attn; $notes = stripcslashes($notes); $message = " $todayis [EST] \n From: $visitorf $visitorl \n Phone: $visitorphone \n Email: $visitormail \n Zip: $visitorzip \n Model: $attn \n Message: $notes \n "; $from = "From: $visitormail\r\n"; mail("[email protected]", $subject, $message, $from); ?> <p align="center"> Information Sent: <?php echo $todayis ?> <br /> Thank You <?php echo $visitorf ?> <br /><br /> Some one will contact you shortly about <br /> the 2011 John Deere Gator Model: <?php echo $attn ?> <br /><br /> Your Comments/Questions:<br /> <?php $notesout = str_replace("\r", "<br/>", $notes); echo $notesout; ?> <br /> Would you like to recieve special offers from Atlantic Tractor? <?php if (isset($_POST['specialoffers'])): echo "Yes"; else: echo "No"; endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094624 Share on other sites More sharing options...
Skewled Posted August 3, 2010 Share Posted August 3, 2010 eregi is depreciated and you should look into using preg_match. What is the variable from the form that's posted to your script? I see first name, last name, phone, mail, zip, notes and attn but nothing about the checkbox? Edit: I'm blind Would you like to recieve special offers from Atlantic Tractor? <?php if (isset($_POST['specialoffers'])): echo "Yes"; else: echo "No"; endif; ?> Should be: <?php if (isset($_POST['specialoffers'])) { echo 'yes'; } else { echo 'no'; } // End If ?> Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094627 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 I'm must be really stupid that I can't figure this out...I changed the code you sent and no change. Where is the code that says go to "thank you page info" after clicking submit. The emails are all coming to me, I just can't get the thank you information to display if the user does not click the checkbox. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094639 Share on other sites More sharing options...
Skewled Posted August 3, 2010 Share Posted August 3, 2010 Sorry I'm sleepy, that code is essentially the same I just am used to writing it out that way. I'm assuming that the checkbox is specialoffers? Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094641 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 yes Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094644 Share on other sites More sharing options...
Skewled Posted August 3, 2010 Share Posted August 3, 2010 error_reporting(E_ALL); post that after your <?php open tag at the top, run the script and see what we get. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094648 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 Nothing. I'm on Firefox on Mac. On Internet Explorer on a PC it says page cannot be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094653 Share on other sites More sharing options...
PFMaBiSmAd Posted August 3, 2010 Share Posted August 3, 2010 The most important piece of information would be your form that contains the html for the checkbox fields? They are likely not getting set because of an error in the form. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094654 Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 Another question.. Your checkbox, does it have a value set? In otherwords...... <input type="checkbox" name="specialoffers" value="yes" /> or does it look more like this.... <input type="checkbox" name="specialoffers" /> If its more like the second one then I am going to assume that you're thinking along the lines of a text box with the checkbox. If there is a value then its going to get posted with said value. Which is not the case with checkboxes. With checkboxes if the checkbox is clicked on and only if the checkbox is clicked will the browser use the value as part of the submitted or "$_POST" (if you will) content. Not clicked (checked) tells the browsers not to post it. I guess thats the easiest way I could put it. This also applies to radio inputs as well.. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094660 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 <input name="specialoffers" type="checkbox" value="YES"/> This is what I have on the form. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094663 Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 hmm, well, now.. I seem to be stumped. Would you mind either sharing all the HTML/PHP related to this issue with the forum, or just me if you like. Cause now I wanna see what the issue is but I need more detail than whats been provided thus far coding wise. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094667 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 Here is the form <form method="post" action="contact.php"> <?php $ipi = getenv("REMOTE_ADDR"); $httprefi = getenv ("HTTP_REFERER"); $httpagenti = getenv ("HTTP_USER_AGENT"); ?> <input type="hidden" name="ip" value="<?php echo $ipi ?>" /> <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" /> <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" /> </span><strong><span class="location">CONTACT US</span></strong><br> <br> <span class="FormText">First Name: <br /> <input type="text" name="visitorf" size="35" /><br /> Last Name:<br /> <input type="text" name="visitorl" size="35" /><br /> Phone Number: <br /> <input type="text" name="visitorphone" size="35" /><br /> Email:<br /> <input type="text" name="visitormail" size="35" /><br /> Zip:<br /> <input type="text" name="visitorzip" size="10" /><br /><br /> </span> <p> <input name="specialoffers" type="checkbox" value="YES"/> <span class="FormText">Please click if you would like to receive <br> special offers from **** via email.<br /> <br /> Which ****** are you interested in?<br /> <select name="attn" size="1"> <option value=" * ">* </option> <option value=" * ">* </option> <option value=" * ">* </option> </select> <br /><br /> Questions or Comments? <br /> <textarea name="notes" rows="4" cols="33"></textarea> <br /> <input type="submit" value="Submit" /> </span> </form> Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094673 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 Here is the php <?php error_reporting(E_ALL); $visitorf = mysqli_real_escape_string($visitorf, trim($_POST['visitorf']; $visitorl = mysqli_real_escape_string($visitorl, trim($_POST['visitorl']; $visitorphone = mysqli_real_escape_string($visitorphone, trim($_POST['visitorphone']; $visitormail = mysqli_real_escape_string($visitormail, trim($_POST['visitormail']; $visitorzip = mysqli_real_escape_string($visitorzip, trim($_POST['visitorzip']; $notes = mysqli_real_escape_string($visitornotes, trim($_POST['notes']; $attn = mysqli_real_escape_string($visitorattn, trim($_POST['attn']; if (eregi('http:', $notes)) { die ("Do NOT try that! ! "); } if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) { echo "<h2>Please hit back and enter a valid email address</h2>\n"; $badinput = "<h2>Your information was not sent</h2>\n"; echo $badinput; die ("Thank you"); } if(empty($visitorf) || empty($visitorl) || empty($visitorphone) || empty($visitormail) || empty($visitorzip)) { echo "<h2>Please fill in all fields marked with a *</h2>\n"; die ("Please hit back"); } $todayis = date("l, F j, Y, g:i a") ; $attn = $attn ; $subject = $attn; $notes = stripcslashes($notes); $message = " $todayis [EST] \n From: $visitorf $visitorl \n Phone: $visitorphone \n Email: $visitormail \n Zip: $visitorzip \n Model: $attn \n Message: $notes \n "; $from = "From: $visitormail\r\n"; mail("*****", $subject, $message, $from); ?> <p align="center"> Information Sent: <?php echo $todayis ?> <br /> Thank You <?php echo $visitorf ?> <br /><br /> Some one will contact you shortly about <br /> **** Model: <?php echo $attn ?> <br /><br /> Your Comments/Questions:<br /> <?php $notesout = str_replace("\r", "<br/>", $notes); echo $notesout; ?> <br /> Would you like to recieve special offers from **? <?php if (isset($_POST['specialoffers'])) { echo 'yes'; } else { echo 'no'; } // End If ?> Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094674 Share on other sites More sharing options...
umdart35 Posted August 3, 2010 Author Share Posted August 3, 2010 Thanks for the help...I'm a beginner to all this and am getting very frustrated at what should be a simple task. That thank you information at the bottom of the php page displays on a new page when the user clicks the checkbox. The user is not redirected (page does not change) when the checkbox is left unclicked. Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094676 Share on other sites More sharing options...
Skewled Posted August 3, 2010 Share Posted August 3, 2010 I tested it locally for you, everything worked fine here for me. Except escaping the variables needs to be done differently but your not using a database to store the information so I don't know how to do it otherwise. I bypassed it from sending an e-mail and it works correctly, try that on your end and see what you get. edit: old values $visitorf = $_POST['visitorf']; $visitorl = $_POST['visitorl']; $visitorphone = $_POST['visitorphone']; $visitormail = $_POST['visitormail']; $visitorzip = $_POST['visitorzip']; $notes = $_POST['notes']; $attn = $_POST['attn']; Quote Link to comment https://forums.phpfreaks.com/topic/209674-php-contact-form-checkbox/#findComment-1094691 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.