1

我有相当基本的 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";
    }

    ?>
4

2 回答 2

0

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

但是,这会奏效。我已经清理了你的 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>

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

于 2013-07-08T20:48:24.207 回答
0

首先,通过用<div>和替换表格来重构您的 html <p>。表格是表格的,但表格不应用于一般布局。

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

<form>标签视为 div 容器。用它包裹整个表格。因此,<form>首先将所有与表单相关的元素放入其中并</form>在最后关闭。这使得 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 等)。

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

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

快乐编码!

于 2013-07-08T21:07:34.643 回答