我一直在尝试在我为我的网站开发的注册表单上实现isset表单验证......
基本上我需要填写正确的所有字段或形式不会$ _post .....
当我发布到自己(表单操作="")我将需要我的表单重新加载并显示上面的错误消息,如果没有正确填写....
这是我的表格代码....
<?php
if(!isset($_POST['submit'])) {
echo '
<form action="" method="post" name="signup">
<table border="1" width="100%">
<tr><td>
<p style="text-align: right;">First Name: </p>
</td><td>
<input name="first_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Last Name: </p>
</td><td>
<input name="last_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Desired Username: </p>
</td><td>
<input name="username" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Password: </p>
</td><td>
<input name="password" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Confirm Password: </p>
</td><td>
<input name="confpassword" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Date Of Birth: </p>
</td><td>
<select name="dob_day">
<option value="000">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="dob_month">
<option value="000">Month</option>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
Year:
<input name="dob_year" type="text" maxlength="4" size="10" value="eg: 1964">
</td></tr>
<tr><td>
<p style="text-align: right;">Email Address: </p>
</td><td>
<input name="email" type="text" maxlength="50">
</td></tr>
<tr><td>
<p style="text-align: right;">Gender: </p>
</td><td>
Male: <input type="radio" name="gender" value="male" />
Female: <input type="radio" name="gender" value="female" />
</td></tr>
</table><p>
<center><input type="submit" name="submit" value=" Sign-Up "></center>
</font>
';}
else
{
echo "Form Submitted";
}
?>
我知道这个表单代码看起来很乱,我讨厌凌乱的代码,但是我不得不截断它来发布在这里,或者我会在数百个代码行之前键入4个空格lol
如果有人能想出一个解决方案(假设相当简单,但无法想象如何编码&#34;)我将再次非常感激:D
谢谢你们
:低头:堆叠交换所有方式:低头:
答案 0 :(得分:1)
else
{
$accept=true;
if(!isset($_POST["value1"])){
$accept=false;
$_SESSION["error"] .= "Please fill in value1\n";
}
//.... Other values
if(!$accept){
header("Location: Current file name"); //Check if the session has the error value filled in and display it on the form.
exit;
}
echo "Form Submitted";
}
完整示例:
<?php
session_start();
if(!isset($_POST['submit'])) {
echo '
<form action="" method="post" name="signup">
<table border="1" width="100%">
<tr><td colspan="2">' . $_SESSION['error'] . ' </td></tr>
<tr><td>
<p style="text-align: right;">First Name: </p>
</td><td>
<input name="first_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Last Name: </p>
</td><td>
<input name="last_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Desired Username: </p>
</td><td>
<input name="username" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Password: </p>
</td><td>
<input name="password" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Confirm Password: </p>
</td><td>
<input name="confpassword" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Date Of Birth: </p>
</td><td>
<select name="dob_day">
<option value="000">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="dob_month">
<option value="000">Month</option>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
Year:
<input name="dob_year" type="text" maxlength="4" size="10" value="eg: 1964">
</td></tr>
<tr><td>
<p style="text-align: right;">Email Address: </p>
</td><td>
<input name="email" type="text" maxlength="50">
</td></tr>
<tr><td>
<p style="text-align: right;">Gender: </p>
</td><td>
Male: <input type="radio" name="gender" value="male" />
Female: <input type="radio" name="gender" value="female" />
</td></tr>
</table><p>
<center><input type="submit" name="submit" value=" Sign-Up "></center>
</font>
';}
else
{
$accept=true;
if(!isset($_POST["first_name"])){
$accept=false;
$_SESSION["error"] .= "Please fill in your first name.<br />";
}
//.... Other values
if(!$accept){
header("Location: " . $_SERVER["PHP_SELF"]); //Check if the session has the error value filled in and display it on the form.
exit;
}
echo "Form Submitted";
}
?>
答案 1 :(得分:1)
表格验证可以使用javascript完成。
<强> HTML 强>
<input type="submit" onclick="return validate();" />
<强> JAVASCRIPT 强>
<script type="text/javascript">
function validate()
{
if(document.getElementsByName('username') == "")
{
alert("Please Enter username");
return false;
}
...
}
</script>
答案 2 :(得分:1)
目前所有代码都会验证是否有人点击了提交按钮,但实际上并未验证他们是否输入了有效值。理解isset没有检查变量是否有值,只是变量存在,因为所有表单元素都将存在于post数组中,而不管是什么值,如果有任何值被发布。
为您提供2个提示:
1)使用foreach循环来评估post数组的每个元素。您可以创建一个单独的预期值条件数组,以在循环中进行检查。你可以插入大量现成的验证过滤器脚本来检查你的期望值是char,int,float等。请在插入数据库或要求SQL注入攻击之前验证用户输入。
2)如果您不明白提交的确切内容,请使用print_r($ _ POST)调试您的表单提交....
我也发现将PHP放在文件顶部更容易,并将html保留在下面。如果您不使用PHP值预填表单值,则无需回显您的html。这样可以更容易地编辑你的html ...在else语句的开头和结尾观察php标签:
<?php
if(isset($_POST['submit'])) {
echo "Form Submitted";
}
else{ ?>
<form action="" method="post" name="signup">
<table border="1" width="100%">
<tr><td>
<p style="text-align: right;">First Name: </p>
</td><td>
<input name="first_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Last Name: </p>
</td><td>
<input name="last_name" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Desired Username: </p>
</td><td>
<input name="username" type="text" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Password: </p>
</td><td>
<input name="password" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Confirm Password: </p>
</td><td>
<input name="confpassword" type="password" maxlength="25">
</td></tr>
<tr><td>
<p style="text-align: right;">Date Of Birth: </p>
</td><td>
<select name="dob_day">
<option value="000">Day</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="dob_month">
<option value="000">Month</option>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
Year:
<input name="dob_year" type="text" maxlength="4" size="10" value="eg: 1964">
</td></tr>
<tr><td>
<p style="text-align: right;">Email Address: </p>
</td><td>
<input name="email" type="text" maxlength="50">
</td></tr>
<tr><td>
<p style="text-align: right;">Gender: </p>
</td><td>
Male: <input type="radio" name="gender" value="male" />
Female: <input type="radio" name="gender" value="female" />
</td></tr>
</table><p>
<center><input type="submit" name="submit" value=" Sign-Up "></center>
</font>
<?php } ?>
答案 3 :(得分:0)
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if (isset($_GET['submit']))
{
$name = $_GET['name'];
$age = $_GET['age'];
echo "Welcome ".$name."<br />" ;
echo "You are ".$age." years old<br/>";
}
else
{
echo "enter the appropriate details";
exit();
}
?>
</body>
</html>
答案 4 :(得分:0)
<html>
<body>
if
(isset($_GET['actie']) && $_GET['actie']== "registreren") {
//registreerformulier
echo "<form method='post' action='".$_SERVER['PHP_SELF']."?actie=aanmaken'>
Voornaam <input name='voornaam' type='text' /> <br />
Familienaam <input name='familienaam' type='text' /> <br />
Wachtwoord <input name='wachtwoord' type='password' /> <br />
Wachtwoord-controle <input name='wachtwoordControle' type='password' /> <br />
<input name='submit' type='submit' value='Maak gebruiker aan' />
</form>";
</body>
</html>