2013-02-22 58 views
0

我有一个用户发布表单的页面。它在php更改仅在刷新后重新发布表单时才会发生更改php

如果用户已经登录,则表单不显示。 如果用户未登录,则会显示表单。

现在,当用户第一次访问该网站时,会显示该表单。然后他填写表格并将其作为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

凡在你的代码你设置$ _SESSION ['ID'] ? – 2013-02-22 17:25:33

+0

@John:在我开始的页面中包含:'session_start();' – xan 2013-02-22 17:28:40

+0

是的,但是你在哪里设置$ _SESSION ['id']?表格发布后?你可以在你的示例代码中包含它吗? – 2013-02-22 17:29:54

回答

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(); 

在top of your form.php

0

这就是我该怎么做的:

<?php $hideForm = isset($_SESSION['id']); ?> 

<form action="" method="post" style="<?= $hideForm ? 'display:none;' : '' ?>" > 
... 

或更简洁:

<form action="" method="post" style="<?= isset($_SESSION['id']) ? 'display:none;' : '' ?>" > 
0

嗯,那你保存ID在数据库中,但我没有看到你重新加载页面它是真实的,所以它不存在于SESSION保存ID后。

一旦你保存了新的信息,你就可以做<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; 
相关问题