【问题标题】:Php Contact form portion of HTML5 site not workingHTML5 网站的 PHP 联系表单部分不起作用
【发布时间】:2016-06-21 02:19:10
【问题描述】:

我目前有我的网站www.vivascoaching.com,当我在那里填写表格时,我似乎无法让页面的联系表格部分给我发电子邮件。目前我有一个 index.html 文件和一个名为 callback.php 的单独 php 文件我是 php 新手,所以我不确定这是否是正确的方法。

这是 index.html 文件:

<!DOCTYPE HTML>
<html>
<head>
   <title>Vivas Coaching-Main</title>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="../style/main.css">
   <link rel="stylesheet" href="../style/normalize.css">

</head>


<body>
   <header>
      <img src="Images/logo.png" alt="logo" width="465px" height="135px" align="middle">
   </header>
   
   <div class="left_column">
   <nav id="secondarylinks">
      <ul>
         <li><a href="register.html" title="Register">Register</a></li>
         <li><a href="dates.html" title="Dates">Dates</a></li>
         <li><a href="pricing.html" title="Pricing">Pricing</a></li>
         <li><a href="forms.html" title="Forms">Forms</a></li>
      </ul>
   </nav>
   </div>
   
   <div class="middle_column">
   <nav id="mainlinks">
      <ul>
         <li><a href="index.html" title="Main">Main</a></li>
         <li><a href="classes.html" title="Classes">Classes</a></li>
         <li><a href="teambuilding.html" title="Team Building">Team Building</a></li>
      </ul>
   </nav>

  <img src="Images/SAT summer flyer.jpg" alt="SAT summer flyer" width="800px" height="800px">
   <footer>
      <p>&copy;VivasCoaching 2016</p>
   </footer>
   </div>

   <div class="right_column">
   <h2>Contact Us</h2>
   <p>(646)316-8481/<br>(403)718-0159</p>
   <p>Please fill out the information below and we will get back to you as soon as possible!<p>

   <form method="post" action="callback.php">
      <label for="firstname">First Name: </label>
      <input type="text" name="firstname"/>

      <label for="lastname">Last Name: </label>
      <input type="text" name="lastname"/>

      <label for="email">Email: <span class="required"></label>
      <input type="text" name="email"/>

      <label>*What is 2+2? (Anti-spam)</label>
      <input name="human" placeholder="Type Here">
      
      <label for="message"> Message:  <span class="required"></label>
      
      <textarea id="message" name="message" cols="25" rows="10" placeholder="Type your message here!"></textarea>
      <input type="submit" id="submit"/>
   </form>
   </div>

</body>
</html>

这是callback.php:

<?php
   
   $firstname = $_POST['firstname'];  
   $lastname = $_POST['lastname']; 
   $email = $_POST['email']; 
   $message = $_POST['message'];
   $to = 'vivascoaching@gmail.com';
   $subject = 'inquiry';
   $human = $_POST['human'];
   $submit = $_POST['submit'];


   $body ="From: $firstname\n $lastname\n Email: $email\n Message:\n $message";


   if ($_POST['submit'] && $human == '4') {				 
        if (mail ($to, $subject, $body)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    } else if ($_POST['submit'] && $human != '4') {
	echo '<p>You answered the anti-spam question incorrectly!</p>';
    }  

?>

【问题讨论】:

  • 好吧,如果你不共享代码,我不知道有什么人可以帮助你。
  • 显示你的代码,Bruh
  • 很抱歉刚刚添加了它。

标签: php html css server filezilla


【解决方案1】:

已编辑

我已经编辑了你的代码,伙计。而且输入不正确。我的意思是你可以使用 &lt;input type="email" name="email" placeholder="email" /&gt; 用于电子邮件。 (该输入需要@domain。) 此外,您可以使用必需的。类似&lt;input type="text" name="test" required /&gt;

我已编辑您的回调。使用这个。

检查编辑 3

我摆脱了 isset($_POST['submit']),这很头疼!


编辑 2:

永远不要在 php 中为数字使用引号('")。不需要。


编辑 3(希望是最后一个。)

如果您想在索引上显示消息,我使用标头通过 GET 请求重定向。

例如:index.php

<!doctype html>
<html>
<head>
</head>
<body>
<?php
error_reporting(0) // prevent undefined index.
if(isset($_GET['success']) && $_GET['success']) {
echo "<p>Your message has been sent!</p>";
} elseif(isset($_GET['error']) && $_GET['error'] && isset($_GET['message'])) {
echo $_GET['message']; // Print message like ?error=true&message=test.
//  echo '<p>'.$_GET['message'].'</p>'; if you don't know how to add html before it.
}
?>
</body>
</html>

(索引需要是.php)!

callback.php 将是:

<?php
if(isset($_POST['human']) && isset($_POST['email'])) {  
   $firstname = $_POST['firstname'];  
   $lastname = $_POST['lastname']; 
   $email = $_POST['email']; 
   $message = $_POST['message'];
   $to = 'vivascoaching@gmail.com';
   $subject = 'inquiry';
   $human = $_POST['human'];


   $body ="From: $firstname\n $lastname\n Email: $email\n Message:\n $message";


   if ($human == 4) {                
        if (mail ($to, $subject, $body)) { 
        header('location:index.php?success=true');
    } else { 
        header('location:index.php?error=true&message=Something went wrong, go back and try again!');
    } 
    } else if ($human != 4) {
        header('location:index.php?error=true&message=You answered the anti-spam question incorrectly!');
    }  
} else { header('location:index.php?error=true&message=Form is incomplete.'); }
?>

【讨论】:

  • 好吧,如果有人要填写该联系表,它应该通过电子邮件将信息传递给我,但是现在我得到的只是一堆错误,就像你发布的那些错误跨度>
  • 天哪,非常感谢!这让它工作了!我很感激!现在假设我希望这些回显消息出现在同一页面上,而不是重定向到新页面。我该怎么做呢?
  • 你救了我一命。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2016-12-15
相关资源
最近更新 更多