简体   繁体   English

使用没有JavaScript的php停留在同一页面上

[英]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.

相关问题 PHP表单保留在同一页面上 - PHP form stay on same page 在PHP中使用die函数停止执行代码,但停留在同一页面上 - using die function in PHP to stop execution of code but stay on the same page 使用 php 提交 html 表单,但保持在同一页面上 - submit html form using php but stay on same page 如何在JavaScript中发出警报后留在同一页面上 - how to stay on same page after alert in JavaScript PHP 表格 - 在同一页面提交 - PHP form - on submit stay on same page 从服务器 php javascript 更新动态表单,但需要保持在浏览器的同一页面上 - Updating a dynamic form from server php javascript but needs to stay on the same page in browser 需要访问使用php输入到表单中的数据,然后将其发送到javascript,并且都在同一页面上而无需重新加载吗? - Need to access data entered into a form using php and then send it to javascript all on the same page and without reloading? 使用ajax在同一文件中将javascript变量传递给php函数,而无需刷新页面 - Pass javascript variable to php function using ajax in same file without page refresh 将Javascript变量传递给同一页面上的php而无需重新加载 - Passing Javascript variable to php on same page without reload 在同一页面上将javascript变量传输到php而不提交 - Transfer javascript variable to php on the same page without submitting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM