1

I have a contact form, and all the php works just fine with emails being sent successfully, but the issue is that an email is sent as soon as the page is loaded/refreshed. I want the code to execute only when a form (with multiple inputs) has been filled in and the submit button as been entered.

<form>
Full Name:<input required type="text" name="fullname" action="currentFile.php"/>
<input type="submit"/>
</form>

<?php
require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      
$mail->Host = 'smtp.gmail.com';  
$mail->SMTPAuth = true;                                
$mail->Username = 'mail@gmail.com';                 
$mail->Password = 'pass';                          
$mail->SMTPSecure = 'tls';                             
$mail->Port = 587;                                    

$mail->addAddress('mail@gmail.com');     

$mail->Subject = 'Contact Form';
$mail->Body    = "Test";
$mail->AltBody = "Test";

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

Would appreciate it if someone could explain how to make it so the php above only executes when the form is filled and button is clicked. Thank you.

H3ll0
  • 259
  • 1
  • 3
  • 16
  • When you submit a form, you are sending either a GET or POST request, depending on your form's method. All you need to do is write an if statement checking if the request matches your form's request, if so, run your mail code. – CodeGodie Dec 31 '16 at 19:10
  • Some bits from this might be useful - http://stackoverflow.com/questions/33699070/php-mail-form-isnt-working/33704535#33704535 you could wrap the whole email code in `if(!empty($_GET['fullname'])){ ...your code }` as CodeGodie suggests. If you are not using a valid email address on your server for the "FROM" address, you are likely to find your emails will start to get treated as spam. – Steve Jan 01 '17 at 13:24
  • @CodeGodie Turns out this issue was due to a lack of attributes in the form tag, I had tried your proposed solution before posting and it didn't work because of this. – H3ll0 Jan 01 '17 at 22:20
  • @Steve Turns out this issue was due to a lack of attributes in the form tag, I had tried your proposed solution before posting and it didn't work because of this. – H3ll0 Jan 01 '17 at 22:20
  • NOTE: SOLUTION RESOLVED. PLEASE POST WHAT I SUGGESTED AS AN ANSWER AND I'LL MARK YOUR ANSWER POST AS RESOLUTION. – H3ll0 Jan 01 '17 at 22:20
  • Post your solution and I will mark it up ;-) – Steve Jan 02 '17 at 00:39
  • @Steve Done, thank you. – H3ll0 Jan 02 '17 at 12:16

1 Answers1

2

Before posting here I attempted to wrap my execution code in an if(isset($_POST['fullname'])) statement, but this didn't work so I cam here seeking assistance. Thanks to those commenting on my post, I realised what the issue was; I didn't specify the method of data transfer in my form tag. Furthermore, mentioned the action attribute in the input tag as opposed to the form tag, which is incorrect.

The html should instead look like this:

<form action="currentFile.php" method="post">
Full Name:<input required type="text" name="fullname"/>
<input type="submit"/>
</form>

The issue I had was simply a result of poor HTML.

H3ll0
  • 259
  • 1
  • 3
  • 16
  • The `if(isset(...` is only to stop the form submitting if the required input information is not available if the "required" attribute fails to block submission. `if(!empty...` will also work and is good for things like checkboxes, which often show up as set when not actually checked. Also for ie8 if you still support that as required doesn't work for that: http://stackoverflow.com/questions/7959955/html5-input-validation-doesnt-work-in-ie8 – Steve Jan 06 '17 at 23:31