简体   繁体   中英

PHP script does not send the email after form submits

After the form submits, I want the PHP script to send the contents of the form to email. I have a similar PHP script that links to a smaller form and that works just fine. The only difference between the two scripts are the variables.

PHP Code

<?php
    if ($_POST["submit"]) {

         $name=$_POST['name'];
         $address=$_POST['address'];
         $firstContactName=$_POST['firstContactName'];
         $firstContactEmail=$_POST['firstContactEmail'];
         $firstContactPhone=$_POST['firstContactPhone'];
         $secondContactName=$_POST['secondContactName'];
         $secondContactEmail=$_POST['secondContactEmail'];
         $secondContactPhone=$_POST['secondContactPhone'];
         $vehicleOrgName=$_POST['vehicleOrgName'];
         $vehicle1=$_POST['vehicle1'];
         $vehicle2=$_POST['vehicle2'];
         $bus=$_POST['bus'];
         $motorcycle=$_POST['motorcycle'];
         $float=$_POST['float'];
         $horse=$_POST['horse'];
         $marchingunit=$_POST['marchingunit'];

         $subject="Parade Application";

         $body = "From: $name\n
    <br /> Address: $address\n
    <br /> First Contact Name: $firstContactName\n
    <br /> First Contact Email: $firstContactEmail\n
    <br /> First Contact Phone: $firstContactPhone\n
    <br /> Second Contact Name: $secondContactName\n
    <br /> Second Contact Email: $secondContactEmail\n
    <br /> Second Contact Phone: $secondContactPhone\n 
    <br /> Entry Type: $vehicle1\t$vehicle2\t$bus\t$motorcycle\t$float\t$horse\t$marchingunit\n 
    <br /> Organization Name for Magnetic Sign: $vehicleOrgName\n";

        /* Set e-mail recipient */

        $ToEmail = 'cbonnette112@rams.wssu.edu'; 
        $subject = trim($subject);
        $mailheader = "From: cbonnette112@rams.wssu.edu\r\n"; 
        $mailheader .="MIME-Version: 1.0" . "\r\n" . 
        $mailheader .="Content-type: text/html; charset=UTF-8" . "\r\n";
        mail(trim($ToEmail), $subject, $body, $mailheader) or die ('failure'); 
    }
?>

Html Form Code

<form id="contactForm" name="contact" class="form-horizontal" role="form" method="post" action="">
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">Name of Organization</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="name" name="name" value="" required>
        </div>
    </div>
    <div class="form-group">
        <label for="address" class="col-sm-2 control-label">Organization Address</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="address" name="address" value="" required>
        </div>
    </div>
    <p class="center bold">1st Contact Person</p>
    <div class="form-group">
        <label for="firstContactName" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="firstContactName" name="firstContactName" value="" required>
        </div>
    </div>
    <div class="form-group">
        <label for="firstContactEmail" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-8">
            <input type="email" class="form-control" id="firstContactEmail" name="firstContactEmail" value="" required>
        </div>
    </div>
    <div class="form-group">
        <label for="firstContactPhone" class="col-sm-2 control-label">Telephone</label>
        <div class="col-sm-8">
            <input type="tel" class="form-control" id="firstContactPhone" name="firstContactPhone" value="" required>
        </div>
    </div>
    <p class="center bold">2nd Contact Person</p>
    <div class="form-group">
        <label for="secondContactName" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-8">
            <input type="text" class="form-control" id="secondContactName" name="secondContactName" value="" required>
        </div>
    </div>
    <div class="form-group">
        <label for="secondContactEmail" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-8">
            <input type="email" class="form-control" id="secondContactEmail" name="secondContactEmail" value="" required>
        </div>
    </div>
    <div class="form-group">
        <label for="secondContactPhone" class="col-sm-2 control-label">Telephone</label>
        <div class="col-sm-8">
            <input type="tel" class="form-control" id="secondContactPhone" name="secondContactPhone" value="" required>
        </div>
    </div>
    <hr />
    <p class="center bold">TYPE OF ENTRY</p>
    <div class="form-group center">
        <label class="checkbox-inline"><input type="checkbox" value="" id="vehicle1" name="vehicle1">Vehicle 1</label>
        <label class="checkbox-inline"><input type="checkbox" value="" id="vehicle2" name="vehicle2">Vehicle 2</label>
        <label class="checkbox-inline"><input type="checkbox" value="" id="bus" name="bus">Bus</label>
        <label class="checkbox-inline"><input type="checkbox" value="" id="motorcycle" name="motorcycle">Motorcycle</label>
        <label class="checkbox-inline"><input type="checkbox" value="" id="float" name="float">Float</label>
        <label class="checkbox-inline"><input type="checkbox" value="" id="horse"name="horse">Horse</label>
        <label class="checkbox-inline"><input type="checkbox" value="" id="marchingunit" name="marchingunit">Marching Unit</label>
    </div>
    <div class="form-group center">
        <p class="bold">ORGANIZATION NAME AS YOU WANT IT ON YOUR MAGNETIC SIGN IF REGISTERING A VEHICLE</p>
        <div class="col-sm-8 col-sm-offset-2">
            <input type="text" class="form-control" id="vehicleOrgName" name="vehicleOrgName" value="">
        </div>
    </div>

    <div class="center">
        <p class="center bold">APPLICATION FEE PER ENTRY</p>
        <p>Vehicle - $100 (Includes 2 new signs)</p>
        <p>Bus - $500</p>
        <p>Motorcycle - $100</p>
        <p>Motorcycle and Vehicle - $200</p>
        <p>Float - $70 (Responsible for securing your own float)</p>
        <p>Marching Unit - $35</p>
        <p>Horse - $35</p>
        <p>Marching Unit and Vehicle - $135</p>
        <p style="color:#CE0001">ALL FEES ARE NON-REFUNDABLE</p>
        <p style="color:#CE0001">DO NOT SEND CASH OR PERSONAL CHECKS</p>
    </div>
    <div class="center">
        <p>If mailing, make money orders or cashiers checks payable:</p>
        <p>Winston-Salem State University<br/>
                            C/O <strong>WSSU Homecoming Committee</strong><br />
                            601 MLK Dr.<br />
                            TSC-G05<br />
                            Winston-Salem, NC 27110
        </p>
    </div>

    <h4 class="center" style="color:#CE0001">APPLICATION DEADLINE: Thursday, September 15</h4>
    <hr />

    <div>
        <ol>
            <li>All entries must move at a satisfactory rate of speed or be subject to removal from the parade line.</li>
            <li>All audio must meet City Code.</li>
            <li>No items can be thrown from vehicles.</li>
            <li>Horses must be equipped with diaper buckets. There is a $100.00 refundable deposit required for horse units given as a separate check. This fee will not be refunded if the city has to clean horse waste from the parade route. The entry fee for horse groups ae the same as marching entries, $35.</li>
            <li>All entries are required to attend 1 of 2 pre-parade meetings in person or by teleconferencing.</li>
            <li>No trucks or trailers.</li>
        </ol>
    </div>
    <hr />

    <div class="center">
        <h4>Float Contact Information</h4>
        <p>Clines Floats<br />
            Contact Person: Phyllis Caldwell<br />
            336-830-2063</p>
        <p>Southeastern Float Company<br />
            Contact Person: Denver Wright<br />
            803-917-0644</p>
        <h4>For Additional Information Contact</h4>
        <p>Stephen Powell<br />
            Homecoming Parade Coordinator<br />
            336-750-3386<br />
        <a href="mailto:powellsl@wssu.edu">powellsl@wssu.edu</a></p>
    </div>
    <hr />
    <div class="form-group center">
        <div class="col-sm-8 col-sm-offset-2">
            <button id="submit" name="submit" type="submit" class="btn btn-danger" data-toggle="modal" data-target="#myModal">Submit</button>
        </div>
    </div>
</form>

Two things - you do not have an action listed for this form - which may be ok if its in the same php page as the form so it reloads into the same page (but if you have the php in a separate page you will need to specify that location in the action attribute of the form), and you have this line :

<button id="submit" name="submit" type="submit" class="btn btn-danger" data-toggle="modal" data-target="#myModal">Submit</button>

which seems to be calling a modal ("#myModal") but you don't have this modal listed in the code, so it is trying to call the modal instead of submitting the form and therefore prevents setting $_POST['submit'] which the php page relies upon. The modal should either be triggered by the form submission or removed from this line of code to allow correct form submission and emailing.

<button id="submit" name="submit" type="submit" class="btn btn-danger">Submit</button>

if you want to call the modal after the form submission then you can use:

$( "#contactForm" ).submit(function() {
  $('#myModal').modal('show')
});

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