Bootstrap将表单提交给php并读取返回的xml

时间:2013-08-30 01:11:20

标签: php xml twitter-bootstrap

我使用以下标记:

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

1 个答案:

答案 0 :(得分:0)

通常,大多数开发人员在HTML页面本身上打印错误/成功(contactmail.php的输出)。我有两种不同的解决方案。

  1. 删除XML并打印HTML,如下所示(兼容Bootstrap):
  2. 为了成功:

    <div class="alert alert-success">Message sent successfully</div>
    

    错误:

    <div class="alert alert-error">Failed: An Error occured</div>
    

    2。如果需要XML输出,则可以与XSL(样式表)关联以适当地显示消息。你可以在这里了解如何完成http://www.w3schools.com/xsl/