[英]Stay on same page using php without javascript
I need to validate a form using php. 我需要使用php验证表单。 when i go to validate i need it to stay on same page if validation fails.
当我去验证时,如果验证失败,我需要它保持在同一页面上。 is this possible.
这可能吗。 i know it can be done with the use of javascript but im trying to cater to those who turn javascript off.
我知道可以使用javascript来完成,但是我正努力迎合那些关闭javascript的用户。
不。如果您不愿意使用Javascript,则必须提交表单(并转到新页面)以进行验证。
Put all of the variables into strings, then use a post method to validate, and if there was a problem get the variables back out from the strings 将所有变量放入字符串中,然后使用post方法进行验证,如果有问题,请从字符串中取出变量
$name = $_POST['name'];
<form action="validate.php" method="POST" value="$name">
<input type="text" id="name" />
</form>
PHP is server side code. PHP是服务器端代码。 In order to validate the page, you need to do a round trip to the server.
为了验证页面,您需要往返于服务器。 If the form doesn't validate, then you can redirect them back to the same, page, along with some markup that explains the problem.
如果表单未通过验证,则可以将其重定向回同一页,以及一些说明问题的标记。
That said, only ~1% of people disable javascript, so I wouldn't worry about that. 就是说, 只有〜1%的人禁用了javascript,因此我不必为此担心。
Even if you do perform client side form validation in javascript, you should always validate server side as well. 即使您确实使用javascript执行客户端表单验证,也应始终验证服务器端。
While it is not possible to do without client side help it is possible to emulate the result by posting the for to the page that hosts the form. 尽管没有客户的帮助是不可能的,但是可以通过将for发布到托管表单的页面上来模拟结果。
if ($_SERVER["REQUEST_METHOD"] == "POST"){
// validate your form and set placeholder variables for error messages.
if(empty($_POST["username"])) $userError = "You must supply a username";
// if the form is valid do a header redirect
header("Location: http://mysite.com/myaccount/");
}
<form method='post'>
<label for='username'>Username: </label>
<input id='username' name='username' type='text'
value='<?= isset($_POST["username"])?$_POST["username"]:"" ?>'>
<?= isset($userError)?$userError:"" ?>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.