CyberShot Posted April 18, 2009 Share Posted April 18, 2009 I am trying to make a form for a church website. for prayer requests. I need the form to be sent over email. I filled out the form and all went as expected, but I didn't get an email. Can you check it out to see what I did wrong? The form <form action="handle_form.php" method="post"> <fieldset> <legend>Enter your prayer request below</legend> <p>Name:</p> <input type="text" name="name" id="name" /> <p>Email:</p> <input type="text" name="email" id="email" /> <p>Gender:<input type="radio" name="gender" value="M" />Male <input type="radio" name="gender" value="F" />Female</p> <p>would you like to be contacted?</p> <select name="contact"> <option value="y">Yes</option> <option value="n">NO</option> </select> <p>Your Prayer Request:<br /><textarea name="comments" rows="10" cols="40"></textarea></p> </fieldset> <div><input type="submit" name="submit" value="Send"/> </div> </form> the code to handle the form <?php if(!empty($_REQUEST['name'])) { $name = $_REQUEST['name']; } else { $name = NULL; echo "Please put in your name"; } if(!empty($_REQUEST['email'])) { $email = $_REQUEST['email']; } else { $email = NULL; echo "Please put in your email"; } if(!empty($_REQUEST['comments'])) { $comments = $_REQUEST['comments']; } else { $comments = NULL; echo "Please add some comments"; } if(isset($_REQUEST['contact'])) { $contact = $_REQUEST['contact']; if($contact == 'y') { echo 'we will contact you shortly'; }elseif ($contact == 'n') { echo 'you will not be contacted'; } else { $contact = NULL; echo 'you should say yes or no to being contacted'; } } $mailto = "my-email.net"; mail($mailto, $name, $email, $contact, $comments); or die("failure"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/ Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 How is your mail server connected? Can you send an email without doing it through PHP? Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812911 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 I have other forms on the server that work just fine. thought my form might just be missing something Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812913 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 You have a problem with your logic. The email function gets called no matter what, even if user gets 1 or more error messages. You need to rewrite your conditions to add error messages to a variable, and then only call the mail function if there is no error message variable. (and echo the variable if there is). But that's not why you are not getting an email. You have your arguments all wrong. Read the manual entry for mail to see what the arguments are supposed to be. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812915 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 I don't get any error messages when I send the form. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812925 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 so then I guess you must not really have a problem then, huh. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812931 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 yes, I do have a problem somewhere, just trying to figure out where. I have been trying to add stuff from that link you gave me, but it don't seem to be working. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812933 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 This is what it's supposed to be: mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) This is what you have: mail($mailto, $name, $email, $contact, $comments); I color coded them so you can compare. Judging by the names of yours vs. theirs, does it really look like to you that you have the right vars being used as the right arguments? Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812934 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 I have been googling this stuff for a while, I also have been going through a book. I have also been looking at other forms that I know work. forms that I am using right now at this very min on another site. here is the code for that form <?PHP $email = $HTTP_POST_VARS[email]; $mailto = "my-email.net"; $mailsubj = "information from mysite.com"; $mailhead = "From: $email\n"; reset ($HTTP_POST_VARS); $mailbody = "Values submitted from mysite.com :\n"; while (list ($key, $val) = each ($HTTP_POST_VARS)) { if ($key!="submit") { $mailbody .= "$key : $val\n"; } } mail($mailto, $mailsubj, $mailbody, $mailhead); ?> So I figured that I could keep the same pattern and all would be well. when I look at what you posted above, it makes no sense because it first, I am a very new to php and second, it doesn't follow the same logic that the web and the books are telling me. I am trying to get it right as you posted Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812935 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 change $HTTP_POST_VARS to $_POST also stupid question, but $mailto = "my-email.net"; is that what you really have in there or did you change it for posting purposes? because if that's what you really have in there, that's also why you're not getting an email. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812937 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 no I changed it for posting purposes. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812938 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 and you have other scripts on your server that sends mail just fine? try using a different email address. maybe your email service is blocking it for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812940 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 I have tested the script above and it works for my email. I usually get it instantly. but when I try using that script on my form rather than the form I got it with. it fails. The $_POST didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812941 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 so post your form. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812944 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 it's in the first post above. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812945 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 so that form at the top points to handle_form.php. And that ^ right there is handle_form.php. put echo "<pre>";print_r($_POST); in your handle_form.php Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812948 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 it printed this to the browser Array ( [name] => adf [email] => adf [gender] => M [contact] => y [comments] => ad [submit] => Send ) of course, I just pushed buttons that don't mean anything. just to put something in the fields Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812952 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 you know..most email service providers would reject emails with stuff like that. particularly the email field. Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812953 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 I will try it again with real values Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812955 Share on other sites More sharing options...
CyberShot Posted April 18, 2009 Author Share Posted April 18, 2009 I found a simple form that works. I will modify it to my needs <form name="form" method="post" action="testform.php"> <p class="bodymd">Your Name<br> <input type="text" name="Name"> </p> <p class="bodymd">Your Email<br> <input type="text" name="Email"> </p> <p class="bodymd">Comments or Questions<br> <textarea name="Comments" rows="5" cols="40"></textarea> </p> <p class="bodymd"> <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset" value="Clear Form"> </p> </form> <?php if (($Name == "") || ($Email == "") || ($Comments == "")) { echo "<form name=form method=post action=contact_thanks.php>"; echo "<p class=bodymd>All three fields of this form are required, I really don't think that's too much to ask...</p>"; echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>"; } if ($Name == "") { echo "<p class=bodymd>Your Name<br><input type=text name=Name></p>"; } else { echo "<input type=hidden name=Name value=$Name>"; } if ($Email == "") { echo "<p class=bodymd>Your Email<br><input type=text name=Email></p>"; } else { echo "<input type=hidden name=Email value=$Email>"; } if ($Comments == "") { echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=5 cols=40></textarea></p>"; } else { echo "<input type=hidden name=Comments value=$Comments>"; } if (($Name == "") || ($Email == "") || ($Comments == "")) { echo "<input type=submit name=Submit value=Submit>"; echo "<input type=reset name=Reset value=Clear Form>"; echo "</form>"; } else { $message = "Name: $Name\nEmail: $Email\nComments: $Comments\n"; $extra = "From: $Email\r\nReply-To: $Email\r\n"; mail ("my-email.net", "mail subject goes here", $message, $extra); echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>"; echo "<p class=bodymd>A response will be sent to $Email as soon as possible.</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154589-solved-help-with-form-code/#findComment-812968 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.