Php 在对表单数据进行验证和清理之后,我该如何处理它?
(我发现了这个,但还是不明白){} 如果这个问题在其他地方解释得更好,我很抱歉,但我已经被困了几个小时,已经搜索过了,而且刚刚放弃。我将学习W3c网站教程,了解如何使用PHP验证、清理和处理表单。一切都进展顺利(至少我认为是这样),直到该对这些数据做点什么的时候了。我现在将向您展示代码,并在代码之后进一步解释我的位置和问题:Php 在对表单数据进行验证和清理之后,我该如何处理它?,php,html,forms,validation,Php,Html,Forms,Validation,(我发现了这个,但还是不明白){} 如果这个问题在其他地方解释得更好,我很抱歉,但我已经被困了几个小时,已经搜索过了,而且刚刚放弃。我将学习W3c网站教程,了解如何使用PHP验证、清理和处理表单。一切都进展顺利(至少我认为是这样),直到该对这些数据做点什么的时候了。我现在将向您展示代码,并在代码之后进一步解释我的位置和问题: <form method="POST" name="signup" action="<?php echo htmlspecialchars($_SERV
<form method="POST" name="signup" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="first name"></label><input id="first name" name="first_name" placeholder="First Name" type="text" value="<?php echo $firstname;?>" /> <span class="error">* <?php echo $firstnameErr;?></span>
<label for="last_name"></label><input id="last name" name="last_name" placeholder="Last Name" type="text" value="<?php echo $lastname;?>" />
<span class="error">* <?php echo $lastnameErr;?></span>
<br><br>
<label for="email"></label><input id="email" name="email" placeholder="Email" type="text" value="<?php echo $email;?>" />
<span class="error">* <?php echo $emailErr;?></span>
<br /><br />
<label for="password"></label><input id="password" name="password" placeholder="Create Password" type="password" />
<span class="error">* <?php echo $passwordErr;?></span>
<br /><br />
<label for="male"><strong>Male</strong></label>
<input id="male" value="male" <?php if (isset($gender) && $gender=="male") echo "checked";?> name="gender" type="radio" />
<label for="female"><strong>Female</strong></label> <input id="female" value="female"
<?php if (isset($gender) && $gender=="female") echo "checked";?> name="gender" type="radio" />
<span class="error">* <?php echo $genderErr;?></span>
<br /><br />
<label for="submit">"I Agree To <a href="#">Terms And Conditions"</a></label> <input id="submit" value="Submit" type="submit" name="submit"/><br /><br />
<p><span class="error">* required field.</span></p>
<hr>
哦,从哪里开始
一开始我想
“Post to self”指的是使用相同的脚本呈现表单并接收表单数据。表单操作使用服务器变量$\u server['php\u SELF']
指向同一个php脚本
这意味着您可以执行以下操作:
<?php
if (!empty($_POST)) { // if $_POST isn't empty, the user submitted the form
// validate
if ($validationPassed) {
// insert to db
} else {
// tell the user they messed up
$error = 'Hey, you! Email address was incorrect.';
}
}
//
?>
<html> ...
<?php if (isset($error)) { echo $error; } ?>
// form
谢谢您的回复。那么,您是否有两种不同的方法来验证和清理信息?“welcome.php”是我在尝试用php验证和清理“index.php”文件(我写下的第一个代码)之前所做的事情。我非常感谢这些链接,也会查看它们。另外,我应该在行动表格下放置什么?它是否应该是另一个文件的路径,向其发送表单数据,然后对其进行进一步清理和验证?比如“welcome.php”?然后,一旦我发送到那里,就开始进一步验证和消毒它?我真的非常感谢你的帮助和耐心。好吧,这取决于你想要什么样的用户体验。一种常见的方法是使用外部脚本(welcome.php),将其中的状态信息放入会话变量($\u session['success']=true
),并使用页面中的会话数据重定向回原始脚本。这被称为POST-Redirect-Get:Awesomeness。我真的很感激。我真的希望它成为一个社交网络。我觉得我还有很长的路要走。学习新东西需要时间。还有很多东西要学。这也是一种宝贵的资源。在掌握基本知识时,有很多东西需要研究:框架等,它们可以帮助您更好地构建代码,以便于维护和可读性。下面是一些进一步(抱歉!)的内容:
<?php
if (!empty($_POST)) { // if $_POST isn't empty, the user submitted the form
// validate
if ($validationPassed) {
// insert to db
} else {
// tell the user they messed up
$error = 'Hey, you! Email address was incorrect.';
}
}
//
?>
<html> ...
<?php if (isset($error)) { echo $error; } ?>
// form