我有一个用户发布表单的页面。它位于php
。
如果用户已登录,则不会显示该表单。 如果用户未登录,则会显示该表单。
现在,当用户首次访问该网站时,会显示该表单。然后他填写表格,submits
填写为POST
。成功登录后,他将被重定向到同一页面。
问题是,在重定向到同一页面后,表单仍然可见。但是,如果我刷新它,表格就会消失。
如何更改/修改以实现我想要的目标。
我不知道给我的代码是否有帮助。虽然,根据我的说法,我附上了我的基本代码:
if(!isset($_SESSION['id'])){
//show form
<form action="" method="post">
<br><button class="lfloat" type="submit" id="submit" value="register0" name="submit">Register</button><br><br>
</form>
<?php if(isset($_POST['submit'])&&$_POST['submit']=='register0'){
//do all the insertion of data into database here only.
....
....
echo "Success!!";
}
}
if(isset($_SESSION['id'])){
//don't show form
}
答案 0 :(得分:0)
类似
if(isset($_POST['submit'])&&$_POST['submit']=='register0'){
//do all the insertion of data into database here only.
....
....
echo "Success!!";
$_SESSION['id'] = 'some value';
header(Location: 'yourForm.php');
exit;
}
应该适合你...也不要忘记写
session_start();
位于form.php的顶部
答案 1 :(得分:0)
我就是这样做的:
<?php $hideForm = isset($_SESSION['id']); ?>
<form action="" method="post" style="<?= $hideForm ? 'display:none;' : '' ?>" >
...
或者更简洁:
<form action="" method="post" style="<?= isset($_SESSION['id']) ? 'display:none;' : '' ?>" >
答案 2 :(得分:0)
嗯,您确实将ID保存在数据库中,但我没有看到您重新加载页面,因此在保存ID后,SESSION
中没有该页面。
您可以在保存新信息后执行<form action="pageURL.php?action=save">
,但是也许您应该进行切换并通过查询字符串传递操作,以便帖子在刷新时不会重新提交。我们的想法是使用这种模式:Post/Redirect/Get。然后你的页面看起来像这样:
$action = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
switch ($action) {
case 'save':
//Do your the registration and then
header('Location:YourPagePath.php?action=saved');
break;
case 'save':
/*FALL TRHOUGH*/
default:
//The rest of the page code
break;
}
此外,成功登录后,解决方案的快速但不是那么好:
echo '<script>
alert("Save successful");
location.href="YourPagePath.php"
</script>';
exit;