-1

I'm trying to add a form to my wordpress page however the problem I have now is that the form is sending an email to me anytime a page is visiting on my web page. I think its because I've put the php in the header file, so everytime header is loaded it sends an email. How do I make it only send the form submission email when its actually filled out?

HTML

 <form name="infoform" method="post" action="" >

  <br><input type="text" name="ffirstname" placeholder="First Name"/><input type="text" name="lastname" placeholder="Last Name"/>
  <br><br><input type="text" name="address" placeholder="Address"/><input type="text" name="unit" placeholder="Unit"/></br>
  <br><input type="text" name="city" placeholder="City"/><select name="province" >
<option value="ab">AB</option>
  <option value="BC">BC</option> 
  <option value="BC">MB</option>
  <option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="PE">PE</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select><input type="text" name="postal" placeholder="Postal Code"/></br>
<br><input type="text" name="country" placeholder="Country"/></br>
<Br><input type="text" name="email" placeholder="Email"/><input type="number" name="phone" placeholder="Phone#"/></br>
<br> <br><input class="submit" type="submit" value="Next" />

CSS

  <style>

  input[type=text], input[type=number], select[name=province]{ font-family: arial; width:100%;  
  border: 1px solid #E5E5E5; padding: 10px 20px;}
input[name=ffirstname] {width:49%; margin-right:1%; }
input[name=lastname] {width:49%; margin-left:1%; }
input[name=address] {width:65.6667%; margin-right:1%; }
input[name=unit] {width:32.3337%; margin-left:1%; }
input[name=city] {width:49%; margin-right:1%; }
select[name=province] {width:24%; margin-left:1%;}
input[name=postal] {width:24%; margin-left:1%; }
input[name=email] {width:49%; margin-right:1%; }
input[name=phone] {width:49%; margin-left:1%;}

input[class=submit] {

  background-color: #f05a28;
  color: white;
  padding: 8px 20px;
  margin-left: 85%;
  border-radius: 4px;
  border: none; !important;
  outline: none; !important ;
  cursor: pointer;
  box-shadow: none; !important;}

PHP

<?php

$name = $_POST['ffirstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$unit = $_POST['unit'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$formcontent=" From: $name $lastname \n Address: $address 
\n Unit: $unit \n City: $city \n Province: $province \n 
Postal Code: $postal \n Country: $country \n 
Email: $email \n Phone: $phone \n ";
$recipient = "contact@justhired.ca";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

?>
Joon Joonam
  • 78
  • 13
  • A: use conditional `empty()`'s for the inputs and `isset()` on the submit button. This type of question has been asked many a time. – Funk Forty Niner Dec 12 '16 at 16:43
  • the isset() does not work for some reason – Joon Joonam Dec 12 '16 at 16:45
  • How are you using isset() or empty()? – Slico Dec 12 '16 at 16:53
  • I posted something for you below. You can add the other post arrays to it @JoonJoonam – Funk Forty Niner Dec 12 '16 at 16:55
  • Possible duplicate of [Check if $\_POST exists](http://stackoverflow.com/questions/3496971/check-if-post-exists) – Kitson88 Dec 12 '16 at 16:56
  • This question has been answered below, but I would add that a basic implementation like this is open to abuse. If you don't want to spend the time implementing validation, consider using something like http://www.gravityforms.com/ which will handle this for you, provide frontend and backend validation, allow adding a captcha etc. – baku Dec 12 '16 at 16:59

3 Answers3

1

You need to check if the submit is set and inputs are not empty.

Sidenote: You can add in the rest of the POST arrays and giving your <input class="submit" type="submit" value="Next" /> a name:

<input class="submit" name="submit" type="submit" value="Next" />

Example:

if(isset($_POST['submit'])) {

if
    (
    !empty($_POST['ffirstname'])
    && 
    !empty($_POST['lastname']) 

    // ... and any other inputs you wish to check for.

    )
{

    $name = $_POST['ffirstname'];
    $lastname = $_POST['lastname'];
    $address = $_POST['address'];
    $unit = $_POST['unit'];
    $city = $_POST['city'];
    $province = $_POST['province'];
    $postal = $_POST['postal'];
    $country = $_POST['country'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $formcontent=" From: $name $lastname \n Address: $address 
    \n Unit: $unit \n City: $city \n Province: $province \n 
    Postal Code: $postal \n Country: $country \n 
    Email: $email \n Phone: $phone \n ";
    $recipient = "contact@justhired.ca";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

    }

}

The <form name="infoform" method="post" action="" > is the main reason why you're getting empty values in Email since you're using your entire code in the same file as the PHP processor and not checking if any inputs are left empty or not.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0
<?php
if(isset($_POST['submit'])){
$name = $_POST['ffirstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$unit = $_POST['unit'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$formcontent=" From: $name $lastname \n Address: $address 
\n Unit: $unit \n City: $city \n Province: $province \n 
Postal Code: $postal \n Country: $country \n 
Email: $email \n Phone: $phone \n ";
$recipient = "contact@justhired.ca";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
?>

As suggest by Fred you need to check the form is submitted And change your submit button to

<input class="submit" type="submit" name="submit" value="Next" />
Adam Hull
  • 214
  • 1
  • 8
0

What's happening is that your PHP script is running every time the page loads. You need to include a conditional statement in your PHP that says : "When this button is clicked, do this..."

In order to achieve that, you will need to update your HTML and add the name attribute to your submit button. I gave that attribute a value of 'Next'. You can always change that. However, make sure the change is reflected in your PHP script.

<br> <br><input name="Next" class="submit" type="submit" value="Next" />

And then in your PHP script the following will wrap your entire script:

if(isset($_POST['Next'])){
//Your script goes here
}
Gloire
  • 1,103
  • 3
  • 17
  • 26