Bloodmorphed Posted April 18, 2011 Share Posted April 18, 2011 It works like a charm but the only problem I get when the page loads it SENDS an email without info of course because it sends it without someone pushing the submit button. Hereis my HTM Land PHP code <form method="post" action="viewpage.php?page_id=5"> Email: <input name="email" type="text"><br> Message:<br> <textarea name="message" rows="15" cols="40"></textarea><br> <input type="submit"> </form> (note the action is in the page itself because thats where the php code is, now for the PHP code:) <?php $to = "hidden for privacy"; $subject = "LoL Recruitment"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) {print "Your mail was sent successfully"; } else {print "We encountered an error sending your mail"; } ?> For some reason I go to the page and it auto sends an email without waiting for me to "submit" the form... Is there any suggestions?? EDIT: I noticed its automatically doing the PHP script as soon as the page opens, because the HTML and PHP script is in the same "page" and not in different files, I understand this and I want it like this I dont want to have to create another file just for it to stop this bug. Theres got to be a way to pause the PHP script and activate it when the "Submit" button is pressed. Quote Link to comment https://forums.phpfreaks.com/topic/234076-phphtml-form-sent-through-e-mail-problem/ Share on other sites More sharing options...
Skewled Posted April 18, 2011 Share Posted April 18, 2011 use isset() to make sure the submit button was clicked. EDIT: Update your form: <input type="submit" name="submit" value="Send E-Mail"> Then Your Code: <?php if (isset($_POST['submit'])) { $to = "hidden for privacy"; $subject = "LoL Recruitment"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) { print "Your mail was sent successfully"; } else { print "We encountered an error sending your mail"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234076-phphtml-form-sent-through-e-mail-problem/#findComment-1203092 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 Give the submit button a name and check to see if the form was submitted before sending the email: <form method="post" action="viewpage.php?page_id=5"> Email: <input name="email" type="text"><br> Message:<br> <textarea name="message" rows="15" cols="40"></textarea><br> <input type="submit" name="submit"> </form> <?php if (isset($_POST['submit'])) { $to = "hidden for privacy"; $subject = "LoL Recruitment"; $email = $_REQUEST['email'] ; $message = $_REQUEST['message'] ; $headers = "From: $email"; $sent = mail($to, $subject, $message, $headers) ; if($sent) {print "Your mail was sent successfully"; } else {print "We encountered an error sending your mail"; } } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234076-phphtml-form-sent-through-e-mail-problem/#findComment-1203095 Share on other sites More sharing options...
Bloodmorphed Posted April 18, 2011 Author Share Posted April 18, 2011 okay thanks I'll try that and let ya'll know if it works, thanks agian Quote Link to comment https://forums.phpfreaks.com/topic/234076-phphtml-form-sent-through-e-mail-problem/#findComment-1203098 Share on other sites More sharing options...
Bloodmorphed Posted April 18, 2011 Author Share Posted April 18, 2011 Ah works like a charm now, thanks i forgot all about isset Quote Link to comment https://forums.phpfreaks.com/topic/234076-phphtml-form-sent-through-e-mail-problem/#findComment-1203103 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.