Php Web表单消息出错后消失
我的网络表单工作正常,除了 如果用户提交缺少1个或多个字段的表单 用户将看到错误消息和所有输入/消息消失。 用户必须再次键入消息 1) 如何保留消息,以便用户不必再次键入 2) 我的web表单是否安全,不受垃圾邮件发送者的攻击 我的网络表单 我的表格代码:Php Web表单消息出错后消失,php,Php,我的网络表单工作正常,除了 如果用户提交缺少1个或多个字段的表单 用户将看到错误消息和所有输入/消息消失。 用户必须再次键入消息 1) 如何保留消息,以便用户不必再次键入 2) 我的web表单是否安全,不受垃圾邮件发送者的攻击 我的网络表单 我的表格代码: <?php // check for a successful form post if (isset($_GET['s'])) echo "<div class=\"alert
<?php
// check for a successful form post
if (isset($_GET['s'])) echo "<div class=\"alert alert-success\">".$_GET['s']."</div>";
// check for a form error
elseif (isset($_GET['e'])) echo "<div class=\"alert alert-danger\">".$_GET['e']."</div>";
?>
<form role="form" method="POST" action="contact-form-submission.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number/WhatsApp</label>
<input type="phone" name="contact_phone" class="form-control" id="input3">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
my php:
<?php
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact.php'); exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header('Location: contact.php?e='.urlencode($error)); exit;
}
$headers = "From: $email_address\r\n";
$headers .= "Reply-To: $email_address\r\n";
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";
// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('xxxx@gmail.com', 'Jewelryxxx.com Online Form', $email_content, $headers);
// send the user back to the form
header('Location:jewelry-indonesia-thankyou.html?s='.urlencode('Your Message Has Been Sent ! Thank you for your message.')); exit;
?>
名称
电子邮件地址
电话号码/WhatsApp
消息
提交
我的php:
为了避免用户在脚本跳入错误验证时不必再次键入,您可以将$\u POST字段存储到一个变量,然后在html字段值上再次调用该变量
通过放置capcha和更多验证输入,可以避免Web表单垃圾邮件发送者,如上所述,只需使用isset即可
isset($\u POST['keterangan'])
例如
!!isset()和$_POST[var]==“”;相同的意思
在1页代码contact.php上编辑所有进程
<?php
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];
if ($name=="" || $email_address="" || $phone=="" || $message==""){
if ($_POST['save']=="contact"){
echo "semua field harus diisi untuk melanjutkan";
}
}else{
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (!isset($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
$error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
$error = 'You must enter a message.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
echo "<div class=\"alert alert-danger\">".$error."</div>";
}else{
$headers = "From: $email_address\r\n";
$headers .= "Reply-To: $email_address\r\n";
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";
mail ('xxxx@gmail.com', 'Jewelryxxx.com Online Form', $email_content, $headers);
// send the user back to the form
echo "<div class=\"alert alert-success\">Your Message Has Been Sent ! Thank you for your message.</div>";
}
}
echo '<form role="form" method="POST" action="contact.php">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1" value="'.$name.'">
</div>
<div class="form-group col-lg-4">
<label for="input2">Email Address</label>
<input type="email" name="contact_email" class="form-control" id="input2" value="'.$email_address.'">
</div>
<div class="form-group col-lg-4">
<label for="input3">Phone Number/WhatsApp</label>
<input type="phone" name="contact_phone" class="form-control" id="input3" value="'.$phone.'">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4">'.$message.'</textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>';
?>
“1)如何保存邮件,以便用户不必再次键入?”=>使用isset
-“2)我的web表单不受垃圾邮件发送者的攻击吗?”=>我们/我不知道,您没有提供任何代码。感谢NekoMata我的php技能很差,请给我isset的php代码。我花了几个小时试图找到要使用的php代码。terima kasih.对不起,我试过了,但不起作用,不知道为什么…在提交失败后,邮件仍然不见了