我使用以下标记:
<form action="http://mysite.com/contactmail.php" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Contact us</legend>
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" title="inputName" for="inputName">Name</label>
<div class="controls">
<input type="text" required class="span3" id="Name" placeholder="Name">
</div>
<label class="control-label" title="inputEmail" for="inputEmail">E-mail</label>
<div class="controls">
<input type="email" required class="span3" id="inputEmail" placeholder="E-mail">
</div>
<label class="control-label" title="inputMessage" for="inputMessage">Message</label>
<div class="controls">
<textarea rows="3" required class="span5" id="inputMessage" placeholder="Message"></textarea>
</div>
<button type="submit" class="btn">Send</button>
</div>
</fieldset>
</form>
以及以下contactmail.php:
<?php
$Name = htmlspecialchars($_GET['inputName']);
$Email = htmlspecialchars($_GET['inputEmail']);
$Message=htmlspecialchars($_GET['inputMessage']);
$to="mail@mysite.com";
$subject="Contact form";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $Name <$Email>\r\n";
// $headers .= "Reply-To: $to\r\n";
$SendState = mail($to, $subject, $Message, $headers);
if ($SendState) {
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<mailer>
<response>'Success'</response>
<comment>'Message send'</comment>
</mailer>
XML;
}
else {
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<mailer>
<response>'Failed'</response>
<comment>'An unknown error occurred'</comment>
</mailer>
XML;
};
echo $xmlstr;
?>
更新,我调整了php文件如下,但是在原始页面上没有得到结果:
if ($SendState) {
print "<html><div class=\"alert alert-success\">Message sent successfully</div></html>";
}
else {
print "<html><div class=\"alert alert-error\">Failed: An Error occured</div></html>";
};
?>
当我单击提交按钮时,会发送一封电子邮件,但没有任何数据。我看到了 XML,但我希望用户留在页面上,并在发送失败时在该页面上给出错误消息,并在发送时给出绿色成功消息。
所以问题是,我做错了什么,我该如何传递数据?如何处理返回的 XML?