从html表单

时间:2017-08-25 10:14:04

标签: php html forms email

平。我非常努力地让这个表单发送电子邮件,但无法使其正常工作。我希望表单在填写正确的字段时发送电子邮件。我对PHP不是很好,并且从其他人那里找到了大部分代码。我按下提交按钮并在表单下获得评论但没有电子邮件。尝试过错误检查,但我找不到怎么做。

<?php
header('Content-Type: text/html; charset=ISO-8859-1');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//NB"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="nb" xml:lang="nb">

此部分将检查填写的表格并提交。它将在网站上发布评论,将其存储在kommentarer.txt中,并发送带有评论详细信息的电子邮件。

<?php
$nameErr = $comErr = $emailErr = "";
$name = $comment = $email = "";
$server = 0;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (empty($_POST['email'])) {
        $emailErr = "Fyll ut din e-post adresse!";
        $server += 1;
        die ("Fyll ut din e-post adresse!");
    } else {
        $email = test_input($_POST['email']);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Skriv inn en e-post adresse!";
            $server += 1;
            die ("Skriv inn en e-post adresse!");
        }
    }
    if (empty($_POST['name'])) {
        $nameErr = "Fyll ut ditt navn, behøver ikke være ditt ekte!";
        $server += 1;
        die ("Fyll ut navne feltet!");
    } else {
        $name = test_input($_POST['name']);
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Bare bokstaver og mellomrom!";
            $server += 1;
            die ("Bare bokstaver og mellomrom!");
        }
    }
    $comment = test_input($_POST['comment']);
    if (empty($_POST['comment'])) {
        $comErr = "<br />Skriv din kommentar!";
        $server += 1;
        die ("Skriv din kommentar!");
    }

    $email = $_POST['email'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    if (!$email == "" && !$name == "" && !$comment == "") {
    $EmailSubject = 'something';
    $Email = 'alben_94@hotmail.com';
    $date=date("Y-m-d H:i:s");

    $EmailField = $_POST['email'];

$Body = <<<EOD

$name har kommentert på nettsiden din! http://xn--nes-entreprenr- 2qb.no/Kommenter.php

Kommentar: $comment
Navn: $name
Email: $EmailField
Dato: $date\r\n \r\n
EOD;

    $headers = "From: $EmailField\r\n";
    $headers .= "Content=type: text/html\r\n";
    $success = mail($Email, $EmailSubject, $Body, $headers);
    } else if ($server == 3){
        die ("Fyll ut feltene markert med *");
    }
}

   function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

HTML

<head>
    <!-- CSS sheets -->
    <link href="./csssheets/indexsheet.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="container">

<h1>NES-ENTREPRENØR A/S</h1>

<div id="nav">
<ul>
    <li><a href="./index.php">Hjem</a></li>
    <li><a href="./Om%20Oss.html">Om Oss</a></li>
    <li><a href="./Tjenester.html">Tjenester</a></li>
    <li><a href="./Priser.html">Priser</a></li>
    <li><a href="./Kontakt%20Oss.html">Kontakt Oss</a></li>
    <li><a href="./Kommenter.php">Kommenter</a></li>
    <li><a href="./Galleri.html">Galleri</a></li>
</ul>
</div>

<div id="inhold">
<h2>Legg gjerne dine kommentarer her!</h2>
<hr />
<div id="inhold2">
<div id="php">

<script>
function clearContents(element) {
element.value = '';
}
</script>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label> Din E-mail: <br><input type="text" name="email" id="email" onfocus="focusFunction()" onblur="blurFunction()" maxlength="50" size="30"></label>
<span class="error">* <?php echo $emailErr;?></span><br>

<label> Ditt Navn: <br><input type="text" id="clearinput" name="name" maxlength="20" onfocus="focusFunction2()" onblur="blurFunction2()" maxlength="30" size="30" value="<?php echo $name;?>"></label>
<span class="error">* <?php echo $nameErr;?></span>
<br>

<label> Din Kommentar: <br><textarea name="comment" id="comment" class="comment" rows="10" cols="50" maxlength="1000" style="font-family: arial; resize: none;" onfocus="focusFunction3()" onblur="blurFunction3()"><?php echo $comment;?></textarea><label>
<span class="error">* <?php echo $comErr;?></span>
<br>

<input id="button" type="submit" name="post" value=" Send ">
</form>

这将在网站上发表评论并将其存储在txt文件中。

<?php
$post = $_POST["post"];

if($post){

$write = fopen("Kommentarer.txt", "a+");
$date=date("Y-m-d H:i:s");
fwrite($write, "<b>Navn:</b> $name<br /><b>Kommentar:</b><br>$comment<br><b>Sendt:</b> $date\r\n \r\n<hr style='width: 500px; background: #175B7D; padding-top: 1px; padding-bottom: 1px;'>");
fclose($write);

$read = fopen("Kommentarer.txt", "r+t");
echo "";

while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
else{
$read = fopen("Kommentarer.txt", "r+t");
echo "";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
?>

</div>
</div>
</div>
</div>
</body>
</html>

感谢您的帮助,如果有的话!

0 个答案:

没有答案