【问题标题】:My PHP Contact form is not working correctly. Sends partial information我的 PHP 联系表单无法正常工作。发送部分信息
【发布时间】:2016-02-21 07:42:40
【问题描述】:

我是 PHP 新手,但我有一个需要上线的网站,但我无法让表单正常工作。

基本上它所做的只是发送“消息”字段中的内容。由于某种原因,所有其他字段都是空白的。此外,如果可能的话,我希望帮助它在不重定向到新页面的情况下确认“已发送邮件”,而只是在表单本身中弹出一条消息。

这里是代码。

<form method="post" action="index.php">
    <div class="form-group">
        <input class="form-control" name="name" placeholder="Name" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="email" placeholder="Email" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="text" placeholder="Which class are you interested in?" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="phone" placeholder="What is the best number to you reach you?" type="text">
    </div>
    <div class="form-group">
        <textarea class="form-control" name="message" placeholder="Message" rows="3"></textarea>
    </div>
    <input class="btn btn-block" type="submit" name="submit" value="Submit">
</form>

这里是 PHP

<?php

$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['class'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site'; 
$to = '**********@gmail.com'; 
$subject = 'Message from **** Site';

$body = "From: $Name\n E-Mail: $Email\n Class: $Class\n Phone: $Phone\n      Message:\n $Message";

if ($_POST['submit']) {              
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
}

?>

我们将不胜感激。

【问题讨论】:

  • 我假设您的 HTML 和 PHP 在同一页面上,即“index.php”?我们还能看到它发送的电子邮件信息的样本(只是文本)吗?另外,在你的 PHP 开始做任何事情之前尝试 var_dump'ing $_POST 变量,只是为了仔细检查它是否以良好的顺序接收所有内容。

标签: php html forms email


【解决方案1】:

对此代码进行了一次更改,

<html>

<form method="post" action="mail.php">
    <div class="form-group">
        <input class="form-control" name="name" placeholder="Name" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="email" placeholder="Email" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="class" placeholder="Which class are you interested in?" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="phone" placeholder="What is the best number to you reach you?" type="text">
    </div>
    <div class="form-group">
        <textarea class="form-control" name="message" placeholder="Message" rows="3"></textarea>
    </div>
    <input class="btn btn-block" type="submit" name="submit" value="Submit">
</form>
</html>

然后检查 PHP 文件,

$Name = $_POST['name'];
$Email = $_POST['email'];
$Class = $_POST['class'];
$Phone = $_POST['phone'];
$Message = $_POST['message'];
$from = 'From: **** Site'; 
$to = '**********@gmail.com'; 
$subject = 'Message from **** Site';

var_dump($_POST);

?>

所有变量都在传递。

要在同一页面中获得答案而无需重定向,请使用 jQuery AJAX 请求,如本示例。使用 Javascript。 http://www.formget.com/submit-form-using-ajax-php-and-jquery/

如果您仍然对 AJAX 有疑问,我将发布您的解决方案的答案。

【讨论】:

    【解决方案2】:

    if ($_POST['submit']) 检查中分配$_POST 值。

    <?php
    if ($_POST['submit']) {   
        $Name = $_POST['name'];
        $Email = $_POST['email'];
        $Class = $_POST['text'];
        $Phone = $_POST['phone'];
        $Message = $_POST['message'];
        $from = 'From: **** Site'; 
        $to = '**********@gmail.com'; 
        $subject = 'Message from **** Site';
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    }
    ?>
    

    如果提交表单而不刷新页面,请使用jQuery.post()

    http://api.jquery.com/jquery.post/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      相关资源
      最近更新 更多