3

嗨,我以前成功地使用了这个非常简单的 php 联系脚本,尽管当我尝试在新的 HTML 页面上实现它时,表单不会提交。任何人都可以看到任何明显的错误吗?任何帮助将非常感激

这是表单的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>

这是简单的 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 
}; 
?>
4

5 回答 5

3

尝试

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

但是,您可以使用以下命令检查该页面中发布的内容:

echo '<pre>';
var_dump( $_POST );
于 2011-04-19T10:29:17.447 回答
1

将您的 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>";

} 
?>
于 2011-04-19T10:43:18.027 回答
0

在你的

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

把它改成

 if ($_POST["email"] != '') { 
于 2011-04-19T10:35:51.113 回答
0

你应该试试这个:

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

使用这个:

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

!基本上表示,所以!empty表示不为空

于 2011-04-19T10:40:32.683 回答
0

你也可以只使用:

if ($_POST["email"]) { 

这很像!= ""orempty()检查。PHP 最初是为了很好地处理表单。你可以让它探测传入的表单字段。它对字符串使用了一些神奇的布尔转换规则,大部分时间都可以完成你想要的。

这种更简单样式的另一个优点是,如果您启用 E_ALL 和 E_NOTICE (=debug) error_reporting 模式,它可以简化调试。

于 2011-04-19T10:50:18.100 回答