gpt4 book ai didi

php - 使用 PHP 进行表单验证

转载 作者:行者123 更新时间:2023-12-03 22:31:22 26 4
gpt4 key购买 nike

我想验证我的表单,因此所有字段都是必填项。如果一个字段未插入或留空,它将在提交后显示一条错误消息。有人能帮忙吗?

表格

<html>  
<head>
<title>Form Input Data</title>
</head>
<table>
<body><table border="1">
<table bgcolor="lightblue"></body>

<form method="post" action="insert_ac.php">
<br>
<tr><td align="left"><strong>Nurse Information</strong></td></tr>
<tr>
<td><font color="red">Please select your name</font></td>
</tr>
<tr>
<td>Fullname</td>
<td><select name="valuelist">;
<option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option>
<?php
$value=$_POST ["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');

$fetch_nurse_name = mysql_query("SELECT DISTINCT Fullname FROM nurse");

while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
echo '<option value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
}
echo "</select>";

?>
</td>
</tr>
<tr>
<td>Please register name here:</td>
<tr>

<td>Fullname</td>

<td><input type="text" name="nurse_forename" size="30"> </td>

</tr>
</tr>

最佳答案

我会做这样的事情:

$req = ['field1', 'field2', 'field...'];
$status = true;
foreach ($req as $field) {
if (empty($_POST[$field])) {
echo 'Field ' . $field . ' is empty';
$status = false;
}
}
if ($status) {
// ok
} else {
// not okay!
}

您创建一个数组 ($req),其中包含所有字段名称并循环遍历它们。根据 empty() 检查每个字段(检查此函数的 php 手册)。

这是一个更好(并且大部分)正确的 HTML 片段...请正确缩进并阅读任何 HTML 教程以获得格式正确的代码。您的 HTML 是 **。

<?php

$value=$_POST["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');

$fetch_nurse_name = mysql_query("SELECT DISTINCT Fullname FROM nurse");

?>
<html>
<head>
<title>Form Input Data</title>
</head>
<body>

<form method="post" action="insert_ac.php">

<table border="1" bgcolor="lightblue">
<tr>
<td align="left"><strong>Nurse Information</strong></td>
</tr>
<tr>
<td><font color="red">Please select your name</font></td>
</tr>
<tr>
<td>Fullname</td>
<td>
<select name="valuelist">
<option value="valuelist" value="<?php echo $nurse_name; ?>"></option>
<?php

while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
echo '<option value="'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td>Please register name here:</td>
</tr>
<tr>
<td>Fullname</td>
<td><input type="text" name="nurse_forename" size="30"> </td>
</tr>
</table>
</form>
</body>
</html>

如果您只有两个给定的字段,可以这样做:

$status = false;
$name = '';

if (!empty($_POST['nurse_forename'])) {
$name = $_POST['nurse_forename'];
$status = true;

} elseif (!empty($_POST['valuelist'])) {
$name = $_POST['valuelist'];
$status = true;

} else {

$status = false;
// none of nurse_forname OR valuelist is filled
// abort.
}

关于php - 使用 PHP 进行表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14915424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com