如果发现会话错误,如何在单击提交按钮后保留文本框中的值

时间:2016-11-19 19:43:56

标签: php session

我创建了一个注册表单,我想在点击提交按钮时,如果某个文本框有问题,其他文本框保留值。我写这段代码,但它不起作用,刷新网站后,所有文本框都变为空...

<?php 
if(isset($_POST['submit'])){
session_start();
$_SESSION['name']=$_POST['name'];
$_SESSION['email']=$_POST['email'];
$name=$_POST['name'];
$email=$_POST['email'];
$pas = hash_value($_POST['pass']);  
$pas2 = hash_value($_POST['pass2']);

if(empty($name)|| empty($email)|| empty($pass) || empty($pass2))
{
    header("location:register.php?msg=10");
}
?>

这是我的表单代码:

<form method="post" action="">
<input  value="<?php echo isset($_SESSION['name']) ? $_SESSION['name'] : ''; ?>" type="text" name="name" id="name" />
<input  value="<?php echo isset($_SESSION['email']) ? $_SESSION['email'] : ''; ?>" type="text" name="email" id="email" />
<input type="password" name="pass" id="pass" />
<input type="password" name="pass2" id="pass2" />
</form>
<?php
if(isset($_GET['msg'])){
$msg = $_GET['msg'];
switch($msg){
case 10;
$err ="please check all of the textboxes!";
$color = "red";
$colorback = "#ffd0e2"; 
break;
}
echo '<div id="error" style="text-align:center; padding:5px 0; margin:0px 0 10px 0;; width:98%;background-color:'.$colorback.'; color:'.$color.'">'.$err.'</div>;
}

0 个答案:

没有答案