0

我正在为我的网站使用联系表格,我对其进行验证,然后通过电子邮件发送给自己,验证工作正常,如果用户第一次正确输入所有详细信息,它会通过电子邮件发送给我。但是,如果用户输入了错误的数据,然后更正它并再次点击发送,它不会发送电子邮件,下面是我到目前为止的表单和 PHP 代码。

联系表格的 HTML 代码

<form action="contact.php" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50" required />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100" required />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" required></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

用于发送表单的 PHP 代码

    function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

{
    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));
}

$send = $_POST['send'];
$message = "";

 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "<br />";

$to = "smokey.12345@hotmail.co.uk";
$subject = "Website contact form";
$message = wordwrap($message, 70, "/r/n");
$headers = "From ". $details['email'];

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

if ($send)
{
    foreach ($details as $field => $detail)
    {
        if (empty($detail) && $field!='number')
            echo "<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>";

    }
}
else
{
    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";
}
4

1 回答 1

1

编辑

为了使代码在同一页面上工作,您需要将操作设置为action=""

否则,您需要使用两个页面。一个用于您的表单,一个用于contact.php您的处理程序。我建议您使用两页,但这里有一个可以在一页内工作的版本。

<?php

if(isset($_POST['send'])) {

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

    foreach ($details as $field => $detail)

    {
        if (empty($detail) && $field!='number')

            die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");

    }

$message = "";
 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "\n";

$send = $_POST['send'];
$email = $_POST['email'];

$to = "smokey.12345@hotmail.co.uk";
$subject = "Website contact form";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";


exit;

} // closing brace for if(isset($_POST['send'])) {

?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

<form action="" method="post">
<fieldset>

<label for="name">Name:<span class="star">*</span></label> <br />
<input type="text" name="name" id="name" placeholder="Enter your name" maxlength="50"  />
<label for="email">Email:<span class="star">*</span></label> <br />
<input type="email" name="email" id="email" placeholder="Enter your email address" maxlength="100"  />
<label for="number">Telephone: </label><input type="tel" name="number" id="number" placeholder="Enter your phone number" maxlength="12" />
<label for="message">Message:<span class="star">*</span></label>
<textarea name="message" id="message" placeholder="Enter your message" cols="54" rows="5" ></textarea>

<p class="small"><span class="star">*</span>&nbsp; Denotes a required field </p>

<input type="submit" id="send" name="send" value="Send" />

</fieldset>
</form>

</body>
</html>

原始答案

此行格式不正确。

$message = wordwrap($message, 70, "/r/n");

将其更改为:

$message = wordwrap($message, 70, "\r\n");

您需要使用\而不是/

编辑

我可以让你的表单工作的唯一方法是添加一个die函数。

现在试试这个:

<?php

function fix_string($var)
{
    if(get_magic_quotes_gpc()) $var = stripslashes($var);
    $var = strip_tags($var);
    return $var;
}

    $details = array('name' => fix_string($_POST['name']),
            'email' => fix_string($_POST['email']),
            'number' => fix_string($_POST['number']),
            'message' => fix_string($_POST['message']));

function trim_value(&$value)
{
    $value = trim($value);
}

array_walk($details, 'trim_value');

    foreach ($details as $field => $detail)

    {
        if (empty($detail) && $field!='number')

            die("<p class='error'>Please fill in the required field: " . ucfirst($field) . "<br /></p>");

    }

$message = "";
 foreach ($details as $field => $detail)
    $message .= $field . ": " . $detail . "\n";

$send = $_POST['send'];
$email = $_POST['email'];

$to = "smokey.12345@hotmail.co.uk";
$subject = "Website contact form";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);
    echo "<p class='success'>Mail was sent successfully</p>";


exit;

?>
于 2013-08-30T10:42:16.640 回答