【问题标题】:PHP contact form troublesPHP联系表格的麻烦
【发布时间】:2013-07-06 06:41:17
【问题描述】:

我对 HTML 和 CSS 有相当基本的了解,我从头开始构建了我的网站。 我在 iFrame 中使用 JotForm 作为我的联系表单,但想将其更改为 PHP 表单,因此我不依赖这些人,并且我可以更好地控制表单的外观。

我最初确实让表单发送带有空白字段的电子邮件 - 我试图解决这个问题,但现在完全搞砸了。

非常感谢任何帮助。

这是我在表格中列出的表单的 HTML;

    <table width="400" border="0" align="left" cellpadding="5" id="form">

  <tr>
<th width="134" scope="row" align="right">NAME</th>
<th width="240" scope="row">
<form name="name" method="post" action="contact.php">
  <span id="sprytextfield1">
  <input type="text" name="name" id="name" value="<?php print "$nameField"; ?>">
  <span class="textfieldRequiredMsg">A value is required.</span><spanclass="textfieldInvalidFormatMsg">Invalid format.</span></span>
</form></th>
</tr>

<tr>
<th scope="row">&nbsp;</th>
<th scope="row">&nbsp;</th>
</tr>

<tr>
<th scope="row" align="right">EMAIL</th>
<th scope="row">
<form name="email" method="post" action="contact.php">
  <span id="sprytextfield2">
  <input type="text" name="email" id="email" value="<?php print "$emailField"; ?>">
  <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>
</form></th>
</tr>

<tr>
<th scope="row">&nbsp;</th>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th height="128" scope="row" align="right">MESSAGE</th>
<th scope="row">
<form name="message" method="post" action="contact.php">
  <span id="sprytextarea1">
  <textarea name="message" id="message" cols="45" rows="5"><?php print "$messageField"; ?></textarea>
  <span class="textareaRequiredMsg"><br>
  A value is required.</span></span>
</form></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<th scope="row"><form name="submit" method="post" action="contact.php">
  <input type="hidden" name="parse_var" id="parse_var" value="contactform">
  <input type="submit" name="submit" id="submit" value="SEND MESSAGE">
</form></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<th scope="row"><?php print "$sent"; ?></th>
</tr>
</table>

还有我的 PHP:

<?php

        if ($_POST['parse_var'] == "form") {

    $emailTitle = 'New Contact Form Message';
    $yourEmail = 'charlotte@charlottemay.co.uk';

    $emailField = $_POST['email'];
    $nameField = $_POST['name'];
    $messageField = $_POST['message'];

    $body = <<<EOD
    <br><hr><br>
    Email: $emailField <br />
    Name: $nameField <br />
    Message: $messageField <br />

    EOD;

    $headers = "From: $emailField\r\n";
    $headers .= "content-type: text/html\r\n";
    $success = mail("$yourEmail", "$emailTitle", "$body", "$headers");

    $sent = "thankyou, your message has been successfully sent x";
    }

    ?>

【问题讨论】:

  • 请不要用那么多表,90多……
  • 只需删除 " mail($yourEmail, $emailTitle, $body, $headers); 您正在传递变量。
  • 而且,在编辑代码后 - 主要问题,据我所知 - 多个表单标签...您应该有一个,正确打开和关闭。 + html 中的 parse_var 被命名为 'contactform',而不是 'form'。
  • 谢谢大家,它现在再次发送一个空表单。我已经删除了“并将 parse_var 更改为“联系表单”,而不是 100% 下一步该做什么:/
  • 那你对jycr753有什么建议?我同意,有很多不整洁的代码:(

标签: php html forms dreamweaver contact


【解决方案1】:

好的,这是功能性的,但不是好的形式,每个字段都应该经过验证/检查(必填字段、字段格式等...)

但是,这会起作用。我已经清理了你的 HTML 代码,请看看你哪里出错了...

如前所述,您的 HTML 结构是主要问题 - 您周围有多个 FORM 标签 - 对于表单的每个字段 - 这不是正确的方法,请参阅 php 表单的 HTML 基本示例...

代码:

<?php 

    if($_POST['parse_var']=='contactform') {

    $emailTitle = 'New Contact Form Message';
   $yourEmail = 'charlotte@charlottemay.co.uk';


    $emailField = $_POST['email'];
    $nameField = $_POST['name'];
    $messageField = $_POST['message'];



    $body = "
    <br><hr><br>
    Email: $emailField <br />
    Name: $nameField <br />
    Message: $messageField <br />

 ";

    $headers = "From: $emailField\r\n";
    $headers .= "content-type: text/html\r\n";
    $success = mail("$yourEmail", "$emailTitle", "$body", "$headers");

    $sent = "thankyou, your message has been successfully sent x";

    }


    ?>

<table width="400" border="0" align="left" cellpadding="5" id="form">

  <tr>
<th width="134" scope="row" align="right">NAME</th>
<th width="240" scope="row">
<form name="name" method="post" action="contact.php">
  <span id="sprytextfield1">
  <input type="text" name="name" id="name" value="">
  <span class="textfieldRequiredMsg">A value is required.</span></span>
</th>
</tr>

<tr>
<th scope="row">&nbsp;</th>
<th scope="row">&nbsp;</th>
</tr>

<tr>
<th scope="row" align="right">EMAIL</th>
<th scope="row">

  <span id="sprytextfield2">
  <input type="text" name="email" id="email" value="">
  <span class="textfieldRequiredMsg">A value is required.</span></span>
</th>
</tr>

<tr>
<th scope="row">&nbsp;</th>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th height="128" scope="row" align="right">MESSAGE</th>
<th scope="row">
  <span id="sprytextarea1">
  <textarea name="message" id="message" cols="45" rows="5"></textarea>
  <span class="textareaRequiredMsg"><br>
  A value is required.</span></span>
</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<th scope="row">
  <input type="hidden" name="parse_var" id="parse_var" value="contactform">
  <input type="submit" name="submit" id="submit" value="SEND MESSAGE">
</form></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<th scope="row"><?php print "$sent"; ?></th>
</tr>
</table>

附:需要您自担风险使用它。 :) 为避免垃圾邮件,发送空表单 - 必须验证字段 - 有大量关于此的教程,请查看其中一些。

【讨论】:

  • 非常感谢!我将好好阅读这篇文章并从我的错误中吸取教训——我还将研究经过验证的表格。 谢谢
【解决方案2】:

首先通过将表格替换为&lt;div&gt;&lt;p&gt; 来重构您的html。表格是表格形式,但表格不应用于一般布局。

还要尽量避免在 php 脚本中使用 html。请改用单独的视图。 PHP 框架可能会帮助您。看看Codeginiter

&lt;form&gt; 标记视为一个 div 容器。用它包裹整个表格。所以从&lt;form&gt;开始,然后将所有与表单相关的元素放在里面,最后用&lt;/form&gt;关闭。这使得 html 文档中的表单部分脱颖而出,因此您可以更轻松地使用它(可读性)。

我已经更改了您的代码示例:

HTML 表单:

<!DOCTYPE html>
<html>
<head>
    <title>Form Page</title>
    <!-- Your header items like meta and link for css stylesheets -->
</head>
<body>

    <div id="container">
        <form id="form-id" action="contact.php" method="POST">
            <input id="parse_var" type="hidden" name="parse_var" value="contactform">

            <div>
                <label for="name">Name</label>
                <input id="name" type="text" name="name" value="<?php echo $nameField; ?>">
                <?php if (empty($nameField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?>
                <?php if (empty($nameField)) echo '<span class="textfield invalidformatmsg">Invalid format.</span>'; ?>
            </div>

            <div>
                <label for="email">E-Mail</label>
                <input id="email" type="text" name="email" value="<?php echo $emailField; ?>">
                <?php if (empty($emailField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?>
                <?php if (empty($emailField)) echo '<span class="textfield invalidformatmsg">Invalid format.</span>'; ?>
            </div>

            <div>
                <label for="message">Message</label>
                <textarea id="message" name="message"><?php echo $messageField; ?></textarea>
                <?php if (empty($messageField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?>
            </div>

            <div class="form-controls">
                <input type="submit" name="submit" id="submit" value="Send message">
            </div>
        </form>
    </div>

</body>
</html>


关于您的 PHP 代码,请查看:

<?php

// Create a view load function
function loadView($view)
{
    // Change this to where you save the view files
    $viewPath = '/'.$view.'.php';

    // Check if the requested view exists
    if ( ! file_exists($viewPath)) return false;

    // This will put the view source in a variable 
    // without flushing to the screen (like echo / print)
    ob_start();
    include $viewPath;
    $buffer = ob_get_contents();
    @ob_end_clean();

    return $buffer;
}

// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['parse_var']))
{
    // Differentiate between forms
    if ($_POST['parse_var'] == 'contactform')
    {
        // Check if all required data was submitted
        if (isset($_POST['name'], $_POST['email'], $_POST['message']) 
            && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
        {
            // Mail Message Settings
            $emailTitle = 'New Contact Form Message';
            $yourEmail  = 'charlotte@charlottemay.co.uk';

            // Get the form data (remember to secure user input against e.g. CSRF)
            $emailField   = $_POST['email'];
            $nameField    = htmlentities($_POST['name']);
            $messageField = strip_tags($_POST['message']);

            // Load the email body view. The variables above 
            // will be accessible in the view.
            $body = loadView('email_body');

            // Set the mail headers
            $headers  = "From: $emailField\r\n";
            $headers .= "Content-type: text/html\r\n";

            // Send the message - I highly recommend you to use something like SwiftMailer!
            $mail = mail($yourEmail, $emailTitle, $body, $headers);

            // Check if the mail was sent
            if ($mail)
            {
                echo 'Sweet! Your message has been successfully sent.';
            }
            else
            {
                echo 'Whoops! The message was not sent.';
            }
        }
        else
        {
            // Here you could return an error message
        }
    }
}


请注意,我将双 " 引号更改为单 ' 引号。为什么?请参阅这个 SO 问题:https://stackoverflow.com/a/3446286/2493918

我强烈建议您使用 SwiftMailer 之类的东西来使用 PHP 发送电子邮件!使用本机 mail() 函数可能并不总是按预期工作,并且大多数发送的邮件将被扔到收件人的垃圾邮件文件夹中(如 gmail 等)。

通读代码 cmets 以了解有关我所做更改的更多信息。

了解 PHP 提供的更多信息的好资源是 PHP 文档,可以在此处找到:例如 ob_start() 函数 http://www.php.net/manual/en/function.ob-start.php

编码愉快!

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多