请阅读以下代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP CORRECT CODE</title>
<style type="text/css">
.error{
position:absolute;
color:#FF0000;
}
</style>
<?php
$nameErr = $emailErr = $mobileErr = $cityErr = "";
$name = $email = $mobile = $city = "";
if(isset($_GET['submit']))
{
if($_SERVER["REQUEST_METHOD"] == "GET")
{
//check name
if(empty($_REQUEST['name']))
{
$nameErr = "Name can not be empty";
}
else
{
$name = $_REQUEST['name'];
}
//check email
if(empty($_REQUEST['email']))
{
$emailErr = "Emails can not be empty";
}
else
{
$email = $_REQUEST['email'];
}
//check mobile
if(empty($_REQUEST['mobile']))
{
$mobileErr = "Mobile number can not be empty";
}
else
{
$mobile = $_REQUEST['mobile'];
}
//check city
if(empty($_REQUEST['city']) or $_REQUEST['city']=="")
{
$cityErr = "Please select your city";
}
else
{
$city = $_REQUEST['city'];
}
}
else
{
echo "<script>alert('Aplogies we can not proceed this time. Thier is some issues in the form submission. Please refresh the page or try after some time.')</script>";
}
}
?>
</head>
<body bgcolor="#CCCCCC">
<center>
<h1>CORRECT FORM INSERTION WITH VALIDATION</h1>
<form name="correct-form" method="GET" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" >
NAME : <input type="text" name="name" id="name" value="<?php echo htmlspecialchars($name); ?>" /><span class="error"><?php echo $nameErr; ?></span>
<br/><br/>
EMAIL : <input type="email" name="email" id="email" value="<?php echo htmlspecialchars($email); ?>" /><span class="error"><?php echo $emailErr; ?></span>
<br/><br/>
MOBILE : <input type="text" name="mobile" id="mobile" value="<?php echo htmlspecialchars($mobile); ?>" /><span class="error"><?php echo $mobileErr; ?></span>
<br/><br/>
CITY : <select name="city" id="city" required="required"><span class="error"><?php echo htmlspecialchars($cityErr); ?></span>
<option selected="selected" value=""> --Select-- </option>
<option value="Jodhpur">JODHPUR</option>
<option value="Pali">PALI</option>
<option value="Jaipur">JAIPUR</option>
<option value="Udaipur">UDAIPUR</option>
</select>
<br/><br/>
<input type="submit" name="submit" id="submit" value="SUBMIT" /> <input type="reset" name="reset" id="reset" value="RESET" />
</form>
</center>
</body>
</html>
我已经完成了强制字段检查的完整编码,但是最后一个城市字段对我来说没有显示出错误消息,因为其他字段正在显示。
提前致谢
答案 0 :(得分:0)
错误 :
CITY : <select name="city" id="city" required="required"><span class="error">Please select your city</span>
<option selected="selected" value=""> --Select-- </option>
<option value="Jodhpur">JODHPUR</option>
<option value="Pali">PALI</option>
<option value="Jaipur">JAIPUR</option>
<option value="Udaipur">UDAIPUR</option>
</select>
但是浏览器不会渲染它。您无法在<select>
控件中插入任意内容。
答案 1 :(得分:0)
我得到了答案,我只是在完全结束标记之前显示错误消息。现在正确的代码是。
CITY : <select name="city" id="city" required="required">
<option selected="selected" value="">--Select--</option>
<option value="Jodhpur">JODHPUR</option>
<option value="Pali">PALI</option>
<option value="Jaipur">JAIPUR</option>
<option value="Udaipur">UDAIPUR</option>
</select><span class="error"><?php echo htmlspecialchars($cityErr); ?></span>