doule6314 2016-06-21 02:19
浏览 31
已采纳

HTML5网站的Php联系表单部分无法正常工作

I currently have my site www.vivascoaching.com up and I cannot seem to get the contact form portion of the page to email me back when that form I have there is filled out. Currently I have an index.html file and a separate php file called callback.php I am new to php so I am not sure if this is the correct way to do it.

This is the index.html file:

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

And this is the 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
 $lastname
 Email: $email
 Message:
 $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>';
    }  

?>

</div>
  • 写回答

1条回答 默认 最新

  • dongyun3897 2016-06-21 02:51
    关注

    EDITED

    I've edited your code dude. And the inputs are incorrect. I mean you can use <input type="email" name="email" placeholder="email" /> for email. (a @domain will be needed on that input.) Also, you can use a required. Something like <input type="text" name="test" required />.

    I've edited your callback. Use this.

    check edit 3

    I got rid of isset($_POST['submit']), that was a headache!


    EDIT 2:

    NEVER use quotes (' or ") for numbers in php. Thats not needed.


    EDIT 3 (hope the last one.)

    If you want to show the message on index, i use headers to redirect with GET requests.

    For example: 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>
    

    (index needs to be .php)!

    callback.php will be:

    <?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
     $lastname
     Email: $email
     Message:
     $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.'); }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决