我需要创建一个表单,它将检查输入是否为空。如果它是空的,那么应该有一个像“必填字段”这样的文本。有一条通知说,surName
有一个未定义的索引。
这是我的PHP代码
<?php
$name_error="";
$sname_error="";
$f_name="";
$s_name="";
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name=$_POST['firstName'];
}
else {
$name_error="Required Field *";
}
}
if ($_POST['surName']!=='') {
$s_name=$_POST['surName'];
}
else {
$sname_error="Please fill this out";
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?
php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
</form>
</body>
</html>
答案 0 :(得分:0)
对于初学者来说,你可以使用内置的HTML5输入验证器required
<input type="text" required>
这将在提交时提供好的用户消息,要求他们填写该字段。
其次,您可以使用empty
代替if ($_POST['surName']!=='')
if ( !empty($_POST['surName']) ) {
// your logic here
}
答案 1 :(得分:0)
功能基本上已经存在......
var possibleChoices = new Dictionary<string,string>{
{"1", "Latte"},
{"2", "Cappuccino"},
{"3", "Espresso"},
{"4", "Double espresso"}
};
string value;
var message = possibleChoices.TryGetValue(choice, out value)
? string.Format("You have selected: {0}", value)
: "Incorrect value, please try again";
//In your code, the fall-through case misses the leading \n .
//By avoiding repetition, consistency in formatting is achieved
//with a single line of code
Console.WriteLine("\n{0}", message);
的{{1}}括号应该在}
if (isset($_POST['submit_button'])) {
因此,如果我对代码进行了更改1和2,则可能如下所示:
?>
答案 2 :(得分:0)
这是因为你把surName验证放在提交条件的外部; `
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name=$_POST['firstName'];
}else{
$name_error="Required Field *";
}
if ($_POST['surName']!=='') {
$s_name=$_POST['surName'];
}
else{
$sname_error="Please fill this out";
}
}
?>`
您可以使用像errors = array();
这样的关联数组,然后使用$errors['surName'] = "this is field is required"
来改善这一点;
答案 3 :(得分:-1)
您没有提交按钮。另一件事是您提交后未使用if
$_POST['surName']
语句<?php
error_reporting(1);
$name_error = "";
$sname_error = "";
$f_name = "";
$s_name = "";
if (isset($_POST['submit_button']))
{
if ($_POST['firstName'] != '')
{
$f_name = $_POST['firstName'];
}
else
{
$name_error = "Required Field *";
}
if ($_POST['surName'] != '')
{
$s_name = $_POST['surName'];
}
else
{
$sname_error = "Please fill this out";
}
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
<input type="submit" name="submit_button" value="Register">
</form>
</body>
</html>
。您可以像这样使用它:
TGA