Jump to content

PHP/HTML Form sent through E-mail Problem


Bloodmorphed

Recommended Posts

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.

Link to comment
Share on other sites

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";
}
}
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.