PHP脚本不会在表单提交后发送电子邮件


PHP script does not send the email after form submits

表单提交后,我希望PHP脚本将表单的内容发送到电子邮件。我有一个类似的PHP脚本,它链接到一个较小的表单,工作得很好。这两个脚本之间的唯一区别是变量。

PHP代码

<?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 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>

有两件事-你没有为这个表单列出操作-如果它与表单在同一个php页面中,那么它可能会重新加载到同一个页面中(但如果你在一个单独的页面中有php,你需要在表单的操作属性中指定该位置),你有这行:

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

它似乎在调用一个模态("#myModal"),但代码中没有列出这个模态,所以它试图调用模态而不是提交表单,因此阻止设置php页面所依赖的$_POST["submit"]。模态应该由表单提交触发,或者从这行代码中删除,以允许正确的表单提交和发送电子邮件。

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

如果你想在表单提交后调用模态,那么你可以使用:

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