将表单文本字段w.r.t更改为cookie(php)

时间:2014-01-13 18:56:02

标签: php web

我有一个问题,我想更改名称文本字段w.r.t cookie,但是当我在提交按钮名称文本字段中输入新条目时,再次显示cookie名称而不是新条目

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
Name: <input type="text" name="name" 
value="<?php
if((isset($_COOKIE["user"]))||($_COOKIE["user"]==$abc)) 
 echo $_COOKIE["user"];
 else
 $abc;
 ?>"> 
 <br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

<?php 
$abc="";
if($_SERVER['REQUEST_METHOD']=="POST")
{$abc=""; 
 If(preg_match("/^[a-zA-Z ]*$/",$_POST["name"]))
 {$abc = clean_data($_POST["name"]);
 echo "hello"." ".$abc;
setcookie("user",$abc,time()+360000);

     }
else
echo "Error name conatin invalid charcters" ; 
}        



/*if(isset($_COOKIE["user"])) 
 echo "<br>".$_COOKIE["user"]."<br>";*/

if(isset($_COOKIE["user"]))
{if(($_COOKIE["user"]!=$abc)&&($_SERVER['REQUEST_METHOD']=="POST"))
    echo "<br>Welcome ".$abc."<br>";
else    
echo "<br>Welcome ".$_COOKIE["user"]."<br>";
}
else
echo "Welcome new user";


function clean_data($data)
{
    $data=trim($data);
    $data=stripcslashes($data);
    $data=htmlspecialchars($data);
    return $data;
}

?>

</body>

</html>

结果是:

名:汤姆

欢迎哈利

///当我在文本字段中输入harry时,tom显示在文本字段中,因为tom在cokie中但是我希望文本字段显示harry to as in enter harry

0 个答案:

没有答案