renegade44 Posted January 30, 2010 Share Posted January 30, 2010 hey guys, here is my contact page: www.glenhealy.com/contact.php I downloaded a contact form and got it set up on my site and working properly. Basically what I am trying to do is add a new field to the form. The new field is "current Web site". I was able to do all of the front end stuff and I basically messed around in the PHP trying to get it to work. I have gotten as far as having the field show up in the email but whatever is put into the actual text field doesn't show up. Can someone help me out and tell me where I need to add more php code so that the form sends whatever is in that field? Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/ Share on other sites More sharing options...
scotmcc Posted January 30, 2010 Share Posted January 30, 2010 Hi Renegade44, It would be helpful if you posted the code that you're having issues with. Thanks, Scot Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/#findComment-1004288 Share on other sites More sharing options...
renegade44 Posted January 30, 2010 Author Share Posted January 30, 2010 Here is the PHP code: <?php //If the form is submitted if(isset($_POST['submit'])) { //Check to make sure that the name field is not empty if(trim($_POST['contactname']) == '') { $hasError = true; } else { $name = trim($_POST['contactname']); } //Check to make sure that the subject field is not empty if(trim($_POST['subject']) == '') { $hasError = true; } else { $subject = trim($_POST['subject']); } //Check to make sure sure that a valid email address is submitted if(trim($_POST['email']) == '') { $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { $hasError = true; } else { $email = trim($_POST['email']); } //Check to make sure comments were entered if(trim($_POST['message']) == '') { $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['message'])); } else { $comments = trim($_POST['message']); } } //If there is no error, send the email if(!isset($hasError)) { $emailTo = '[email protected]'; //Put your own email address here $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nSite: $site \n\nComments:\n $comments"; $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?> Here is my form: <div id="contact-wrapper"> <?php if(isset($hasError)) { //If errors are found ?> <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p> <?php } ?> <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?> <p><strong>Email Successfully Sent!</strong></p> <p>Thank you <span><?php echo $name;?></span> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p> <?php } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform"> <div> <label for="name"><strong>Name:</strong></label> <input type="text" size="50" name="contactname" id="contactname" value="" class="required" /> </div> <div> <label for="email"><strong>Email:</strong></label> <input type="text" size="50" name="email" id="email" value="" class="required email" /> </div> <div> <label for="email"><strong>Current Website (if any)</strong></label> <input type="text" size="50" name="site" id="site" value="" class="" /> </div> <div> <label for="subject"><strong>Subject:</strong></label> <input type="text" size="50" name="subject" id="subject" value="" class="required" /> </div> <div> <label for="message"><strong>Message:</strong></label> <textarea rows="5" cols="50" name="message" id="message" class="required"></textarea> </div> <input type="submit" value="Send Message" name="submit" id="button"/> </form> </div> Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/#findComment-1004299 Share on other sites More sharing options...
scotmcc Posted January 30, 2010 Share Posted January 30, 2010 You'll notice in your provided code that each form field is handled and then set into a variable, for example: $name = trim($_POST['contactname']); ... $subject = trim($_POST['subject']); You have used the variable $site in the $body variable to send as part of your e-mail. However, you never set the $site variable to any value. You should put this code somewhere in your PHP code. I have duplicated the coding standard used in your PHP source code below. //Check to make sure that the site field is not empty if(trim($_POST['site']) == '') { $hasError = true; } else { $site = trim($_POST['site']); } One critique I have of the code is that when an error is found in the form submission I don't see any way that the error is handled... perhaps I just missed it on my quick glance of your code. Have a good day, Scot Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/#findComment-1004315 Share on other sites More sharing options...
renegade44 Posted January 30, 2010 Author Share Posted January 30, 2010 it has Jquery validation prior to being submitted I believe I will double check. Going to try adding this code now. Thanks for the quick responses man!!! Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/#findComment-1004317 Share on other sites More sharing options...
renegade44 Posted January 30, 2010 Author Share Posted January 30, 2010 hey scot, the code worked! thank you! is there a way to make it not "required though? if I remove the if error statement from the code, would that do the trick? Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/#findComment-1004318 Share on other sites More sharing options...
scotmcc Posted January 30, 2010 Share Posted January 30, 2010 Remove the if block and just set $site to the $_post variable. Link to comment https://forums.phpfreaks.com/topic/190371-contact-form-help-adding-a-textfield-to-email/#findComment-1004354 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.