我有这个现有的代码,我想知道如何使名称和电子邮件字段成为必需的?
<?php
if(isset($_POST['submit'])){
$to = "xxx@email.com"; // this is your Email address
$from = $_POST['gift_email']; // this is the sender's Email address
$first_name = $_POST['gift_name'];
$subject = "Free Gift Request";
$msg = "A free gift has been requested from the following:"."\n";
$msg .= "Name: ".$_POST["gift_name"]."\n";
$msg .= "E-Mail: ".$_POST["gift_email"];
$headers = "From:" . $from;
mail($to,$subject,$msg,$headers);
//echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header('Location:free_program_thankyou.php');
}
?>
答案 0 :(得分:3)
表单
<input type="text" name="gift_email" required>
<input type="text" name="gift_name" required>
对于Php
if(empty($_POST['gift_email']))
{
echo 'This field is required';
}else {
//Do what you want to do here
}
答案 1 :(得分:1)
执行此操作的两种基本方法: -
在实践中,强大的网站将同时做到这两点。这似乎是重复,但javascript函数更具响应性和用户友好性,但是,php服务器端验证无法通过关闭JS或欺骗响应来进行游戏。