2014-09-24 37 views
-2

我有这个现有的代码,我想知道如何使名称和电子邮件字段所需?如何制作表单字段?

<?php 
if(isset($_POST['submit'])){ 
$to = "[email protected]"; // 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

使用谷歌搜索您的标题= *大约1,770,000结果(0.31秒)* – 2014-09-24 03:14:22

回答

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

它是'$ _POST'而不是'$ _post'并且您缺少分号 – 2014-09-24 03:15:32

+0

'$ _POST'是一个[**超全球**](http ://php.net/manual/en/language.variables.superglobals.php)和**必须是**大写。 – 2014-09-24 03:16:54

+0

你忘了分号。它会抛出一个解析错误。 – 2014-09-24 03:18:36

1

一两种基本方法可以做到这一点: -

  • 在php程序检查每个必填表单字段是否已填写发送新页面,如果不是,则返回错误消息。请务必返回已填写的任何字段的内容,否则您的用户将希望在您的人身上发生瘟疫。
  • 验证码在javascript中。有一个由“onsubmit”条件触发的函数,该条件检查所有需要的表单字段被填充并突出显示没有填写的字段。见here

在实践中,一个健壮的网站将做到这一点。这似乎是重复的,但是javascript函数响应性更强,用户友好,但是,通过关闭JS或欺骗响应,php服务器端验证无法实现。

+0

html5提供了新选项 – 2014-09-24 03:19:16