简体   繁体   English

PHP联系表格未提交

[英]PHP Contact Form not submitting

Hi I've previously used this very simple php contact script with success though when I've tried implementing it on a new HTML page with the form won't submit. 嗨,我以前使用这个非常简单的PHP联系脚本成功,但是当我尝试在新的HTML页面上实现它时,表单将无法提交。 Can anyone see any obvious errors? 谁能看到任何明显的错误? Any help would be much appreciated 任何帮助将非常感激

Here is the html of the form: 这是表单的html:

<div id="formContainer">

    <form action="form.php" method="post" id="contactForm"> 
    <fieldset>

    <legend>Your details</legend>    

    <label for="name">Name *</label>
    <input type="text" id="name">

    <label for="email">Email *</label>
    <input type="email" id="email">

    <label for="tel">Telephone</label>
    <input type="tel" id="tel">

    </fieldset>

    <fieldset> 
    <legend>Tutoring</legend>

    <label for="type">Type of lesson</label>
    <select name="type" id="type">
    <option>Individual</option>
    <option>Group</option>
    </select>

    <label for="subject">Subject</label>
    <input name="subject" list="subjects" id="subject">
    <datalist id="subjects">
    <option>English</option>
    <option>Biology</option>
    <option>Geography</option>
    </datalist>

    <label for="level">Your level</label>
    <select name="level" id="level">
    <option>Beginner</option>
    <option>GCSE</option>
    <option>A-Level</option>
    <option>University</option>
    </select>

    <label for="hours">Hours/week</label>
    <input type="number" id="hours">

    <label for="info">Additional Information</label>
    <textarea name="info" id="info" rows="10"               cols="6"></textarea>


    </fieldset>

    <input type="submit" name="submit" value="Send" id="sendButton">

    <input type="hidden" name="submit_check" value="1" />   
    </form>

    </div>

And here is the simple php script: 这是简单的PHP脚本:

<?php 

if ($_POST["email"]<>'') { 
    $ToEmail = 'onjegolders@gmail.com'; 
    $EmailSubject = 'Site contact form '; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
    $MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
    $MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
    $MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
    $MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
    $MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
    $MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>"; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 

?> 

<html>

    <h3>Thanks for your email</h3>
    <h4>I'll get back to you as soon as possible</h4>
    <a href="index.html"><p>Click here to go back to previous page</p></a>

</html>


<?php 
} else { 
?> 

<html>Sorry, this form didn't work</html>

<?php 
}; 
?>

Try with 试试吧

if ( !empty($_POST["email"]) )

However you can check, what is posted in that page using: 但是,您可以使用以下命令检查该页面中发布的内容:

echo '<pre>';
var_dump( $_POST );

Change your form.php as the following 将form.php更改为以下内容

    <?php 

if ($_POST["email"]<>'') { 
    $ToEmail = 'onjegolders@gmail.com'; 
    $EmailSubject = 'Site contact form '; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
    $MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
    $MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
    $MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
    $MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
    $MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
    $MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>"; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 

echo &lt;&lt;&lt;EXCERPT

<html>

    <h3>Thanks for your email</h3>
    <h4>I'll get back to you as soon as possible</h4>
    <a href="index.html"><p>Click here to go back to previous page</p></a>

</html>

EXCERPT;

} else { 


echo "<html>Sorry, this form didn't work</html>";

} 
?>

in your 在你的

if ($_POST["email"] <> '') { 

change that into 改变成

 if ($_POST["email"] != '') { 

You should try this: 你应该试试这个:

if(!empty($_POST["email"])){
  //your email preparation code
}

Insted of using this: 使用这个:

if($_POST["email" <> ''){
  //your email preparation code
}

The ! ! basically means not , so the !empty means not empty 基本上不是 ,所以!empty意味着不是空的

You could also just use: 你也可以使用:

if ($_POST["email"]) { 

This works much like the != "" or empty() check. 这很像!= ""empty()检查。 PHP was incepted to handle forms well. PHP被接受以很好地处理表单。 And you can just let it probe incoming form fields. 你可以让它探测传入的表单字段。 It uses some magic boolean conversion rules for strings, which most of the time will accomplish what you want. 它对字符串使用了一些魔术布尔转换规则,大多数情况下它会完成你想要的。

Another advantage of this simpler style is that it eases debugging if you enable the E_ALL and E_NOTICE (=debug) error_reporting mode. 这种更简单风格的另一个优点是,如果启用E_ALL和E_NOTICE(= debug)error_reporting模式,它可以简化调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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