我有一个带有登录页面的网站,在页面底部有一个表单。我要做的就是显示验证消息,然后再次将用户重定向到表单,这样他就可以查看该消息而无需滚动。
代码如下(位于同一页上):
//validation
if (isset($_POST['submit'])) {
$name = htmlspecialchars(stripslashes(trim($_POST['name'])));
if (!preg_match("/^[A-Za-zÀ-ÖØ-öø-ÿ]/", $name)) {
$name_error = 'invalid name';
}
//i want to redirect as soon as the validation finishes
header('Location: http://localhost/myweb/index.php#contact');
}
<form id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="text" name="name" id="name">
<button type="submit" id="submit" name="submit">Send</button>
</form>
我已经尝试了以下所有方法:
window.location.href = 'index.php#contact';
在按钮(onclick
)和形式(onsubmit
)header
代码的多个部分,也与exit();
但是,这是关键,我不想仅将php的另一页用于验证(因为我将需要使用session_start()
并且希望避免cookie)并且我也不想使用AJAX XMLHttp Request。
有可能做到吗?
我使用#contact在表单动作像这样
<form id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#contact" method="POST">
你有尝试过吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句