[英]Php form. Need help to send it when validate
讓我們開始說我還是這個php的新手。 我不會放棄,我會學習的。 我整理的表格代碼有問題。 驗證所有輸入字段后,我無法發送它。 不明白為什么。 也許我可以得到一些幫助? 也許您可以修復它以便工作? 因此,我有一些需要配合的東西。
<?php
// This is your email address
$to = "my@mail.com";
// Subject title
$subject = 'Testing work now pls';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
$headers .= "Reply-To: ".$email."\r\n";
// If press send
if (isset($_POST['send'])) {
// If not empty
$errors = array(
$errorsname = $errorsemail
);
// Start validation
// Check if name not empty
if (empty($_POST["name"])) {
$errorsname = "You must write their name.";
} else {
$name = ($_POST["name"]);
// Check if only letters in name
if (!preg_match("/^[a-zA-Z æøå-ÆØÅ]*$/",$name)) {
$errorsname = "Their name only content letters.";
}
// Check if name to short
if(strlen($_POST['name']) < 2 )
{
$errorsname = 'Their name is too short.'; }
// Check if name to long name
if(strlen($_POST['name']) > 30 )
{
$errorsname = 'Their name is too long'; }
}
// Check email
if (empty($_POST["email"])) {
$errorsemail = "You must enter their email."; }
else {
$email = ($_POST["email"]);
// Check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$errorsemail = "Invalid email address."; }
}
// Check errors if non continue
if (empty($errors))
{
// Make message ready
$message = "
Stupid just work pls! $navn, $email
";
// Then send mail
mail($to,$subject,$message,$headers);
// Redirect to sucesse page
header('Location: mysite.php');
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<style>
.errors {
color: #FF0000;
}
</style>
</head>
<body>
<h2>Hmmm....Help?</h2>
<span class="errors"><?php echo $errormessage; ?></span>
<form name="kontakt" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input name="name" type="text" placeholder="Deres navn.." value="<?php echo $name;?>">
<span class="errors"><?php echo $errorsname;?></span>
<br><br>
E-mail: <input name="email" type="text" placeholder="Din e-mail.." value="<?php echo $email;?>">
<span class="errors"><?php echo $errorsemail;?></span>
<br><br>
<br><br>
<input type="submit" name="send" value="Send">
</form>
</body>
</html>
在進行驗證之前,您需要將$errors
設置為非空數組,因此
if (empty($errors))
將永遠失敗。 您應該將其初始化為:
$errors = array();
然后,您的驗證代碼應以這種方式添加到其中:
if (empty($_POST['name'])) {
$errors['name'] = "You must write their name.";
}
和
if (empty($_POST['email'])) {
$errors['email'] = "You must enter their email.";
}
您在這里有很多問題。 首先,在您的HTML中,您需要檢查是否設置了錯誤變量,如果已設置,請顯示它們,否則,您將得到php錯誤:
<span class="errors"><?php
if (isset($errormessage)) {
echo $errormessage;
}
?></span>
<form name="kontakt" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Name: <input name="name" type="text" placeholder="Deres navn.." value="<?php
if (isset($name)) {
echo $name;
}
?>">
<span class="errors"><?php
if (isset($errorsname)) {
echo $errorsname;
}
?></span>
<br><br>
E-mail: <input name="email" type="text" placeholder="Din e-mail.." value="<?php echo $email; ?>">
<span class="errors"><?php if (isset($errorsemail)) {
echo $errorsemail;
}
?></span>
其次,如上所述,您的$ erros數組應類似於Barmar所示或實例化。
最后,加載頁面時不會實例化$ email變量,您將不得不為其設置默認值:
if(isset($_POST['email'])){
$email = $_POST['email'];}
else{
$email = '';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.