简体   繁体   中英

How do I send email in HTML form using PHP?

when I submit the form all of the fields in the e-mail are blank and I can not figure out why. It started with getting a php error of the variables not being written to, then after searching on here I found a solution to add the "isset" and initiate the variables to a blank string. I don't have anything in the error log any more but when the form sends to the e-mail all of the details are blank.

Here is my HTML Form Code:

  <form action="dish-business.php">

            <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="businessname" class="sr-only control-label">Business Name</label>
                  <input name="businessname" id="businessname" type="text" placeholder="Business Name&#42" required="" data-validation-required-message="Please enter your business name" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="name" class="sr-only control-label">Name*</label>
                  <input name="name" id="name" type="text" placeholder="Full Name&#42" required="" data-validation-required-message="Please enter your full name" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

              <div class="col-md-4">

                   <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="phone" class="sr-only control-label">Phone*</label>
                  <input name="phone" id="phone" type="tel" placeholder="Phone&#42" required="" data-validation-required-message="Please enter phone number" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>

              </div>

              <div class="col-md-4">
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="email" class="sr-only control-label">Email*</label>
                  <input name="email" id="email" type="email" placeholder="Email&#42" required="" data-validation-required-message="Please enter email" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="business" class="sr-only control-label">Type of Business</label>
                <!--  <input id="business" type="text" placeholder="Type of Business" required="" data-validation-required-message="Please enter your type of business" class="form-control input-lg"><span class="help-block text-danger"></span> -->
                  <select name="business" class="form-control input-lg"><span class="help-block text-danger">
    <option value="">Type of Business</option>
    <option value="Airports">Airports</option>
    <option value="Automotive">Automotive</option>
    <option value="Banks" onclick="changeValue(&#39;Banks&#39;);">Banks</option>
    <option value="Bar/Restaurant" onclick="changeValue(&#39;Bar/Restaurant&#39;);">Bar/Restaurant</option>
    <option value="Beauty Services" onclick="changeValue(&#39;Beauty Services&#39;);">Beauty Services</option>
    <option value="Church" onclick="changeValue(&#39;Church&#39;);">Church</option>
    <option value="Firehouse/Oil Rig/EMT" onclick="changeValue(&#39;Firehouse/Oil Rig/EMT&#39;);">Firehouse/Oil Rig/EMT</option>
    <option value="Golf Course" onclick="changeValue(&#39;Golf Course&#39;);">Golf Course</option>
    <option value="Government" onclick="changeValue(&#39;Government&#39;);">Government</option>
    <option value="Grocery/Health Food Store" onclick="changeValue(&#39;Grocery/Health Food Store&#39;);">Grocery/Health Food Store</option>
    <option value="Hair salon" onclick="changeValue(&#39;Hair salon&#39;);">Hair salon</option>
    <option value="Health/Fitness" onclick="changeValue(&#39;Health/Fitness&#39;);">Health/Fitness</option>
    <option value="Health Care" onclick="changeValue(&#39;Health Care&#39;);">Health Care</option>
    <option value="Liquor Store" onclick="changeValue(&#39;Liquor Store&#39;);">Liquor Store</option>
    <option value="Lobbies" onclick="changeValue(&#39;Lobbies&#39;);">Lobbies</option>
    <option value="Medical / Dental" onclick="changeValue(&#39;Medical / Dental&#39;);">Medical / Dental</option>
    <option value="Nail Salon" onclick="changeValue(&#39;Nail Salon&#39;);">Nail Salon</option>
    <option value="Office" onclick="changeValue(&#39;Office&#39;);">Office</option>
    <option value="Private Club" onclick="changeValue(&#39;Private Club&#39;);">Private Club</option>
    <option value="Professional Services" onclick="changeValue(&#39;Professional Services&#39;);">Professional Services</option>
    <option value="Retail Store" onclick="changeValue(&#39;Retail Store&#39;);">Retail Store</option>
    <option value="School" onclick="changeValue(&#39;School&#39;);">School</option>
    <option value="Service Industry" onclick="changeValue(&#39;Service Industry&#39;);">Service Industry</option>
    <option value="Sports Facilities" onclick="changeValue(&#39;Sports Facilities&#39;);">Sports Facilities</option>
    <option value="Stadiums" onclick="changeValue(&#39;Stadiums&#39;);">Stadiums</option>
    <option value="Other" onclick="changeValue(&#39;Other&#39;);">Other</option>

</select>


                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="locations" class="sr-only control-label">Locations*</label>
                  <input name="locations" id="locations" type="text" placeholder="Number of Locations&#42" required="" data-validation-required-message="Please enter the number of locations" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

               <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="units" class="sr-only control-label">Units*</label>
                  <input name="units" id="units" type="text" placeholder="Number of Units&#42" required="" data-validation-required-message="Please enter the number of units" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

              <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="dishcustomer" class="sr-only control-label">Are you a dish customer</label>
                <!--  <input id="business" type="text" placeholder="Type of Business" required="" data-validation-required-message="Please enter your type of business" class="form-control input-lg"><span class="help-block text-danger"></span> -->
                  <select name="dishcustomer" class="form-control input-lg"><span class="help-block text-danger">
    <option value="">Are you a current DISH Customer?</option>
    <option value="yes">Yes</option>
    <option value="no">No</option>
    <option value="idk">Don't Know</option>

</select>


                </div>
              </div>
              </div>

                         <div class="col-md-4">
                              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="zip" class="sr-only control-label">Zip Code</label>
                  <input name="zip" id="zip" type="text" placeholder="Business Zip Code&#42" required="" data-validation-required-message="Please enter your business zip code" class="form-control input-lg"><span class="help-block text-danger"></span>
                </div>
              </div>
              </div>

           <div class="col-md-12">
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label for="message" class="sr-only control-label">Details</label>
                  <textarea name="message" id="message" rows="2" placeholder="Any Other Details" class="form-control input-lg"></textarea><span class="help-block text-danger"></span>
                </div>
              </div>
              <div id="success"></div>
              <button type="submit" class="btn btn-dark btn-lg text-center">Send</button>
              </div>

            </form>

Here is my PHP Code:

<?php

$EmailFrom = "mail@.com";
$EmailTo = "@gmail.com";
$Subject = "Mail";

$Name = "";
$Businessname="";
$Tel="";
$Email="";
$Business="";
$Locations="";
$Units="";
$DishCustomer="";
$Zip="";
$Message="";

if (isset($_POST['businessname'])) {
    $BusinessName = Trim(stripslashes($_POST['businessname'])); 
}
if (isset($_POST['name'])) {
    $Name = Trim(stripslashes($_POST['name'])); 
}
if (isset($_POST['phone'])) {
$Tel = Trim(stripslashes($_POST['phone'])); 
}
if (isset($_POST['email'])) {
$Email = Trim(stripslashes($_POST['email'])); 
}
if (isset($_POST['business'])) {
$Business = Trim(stripslashes($_POST['business'])); 
}
if (isset($_POST['locations'])) {
$Locations = Trim(stripslashes($_POST['locations'])); 
}
if (isset($_POST['units'])) {
$Units = Trim(stripslashes($_POST['units'])); 
}
if (isset($_POST['dishcustomer'])) {
$DishCustomer = Trim(stripslashes($_POST['dishcustomer'])); 
}
if (isset($_POST['zip'])) {
$Zip = Trim(stripslashes($_POST['zip'])); 
}
if (isset($_POST['message'])) {
$Message = Trim(stripslashes($_POST['message'])); 
}

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Business: ";
$Body .= $Business;
$Body .= "\n";
$Body .= "Number of Locations: ";
$Body .= $Locations;
$Body .= "\n";
$Body .= "Units: ";
$Body .= $Units;
$Body .= "\n";
$Body .= "Currently a Dish Customer: ";
$Body .= $DishCustomer;
$Body .= "\n";
$Body .= "Zip Code: ";
$Body .= $Zip;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=404.html\">";
}
?>

Any help would be appreciated please. Thank you.

Forms default to method="GET" . Change your form to:

<form action="dish-business.php" method="POST">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM