2013-05-28 157 views
-3

我有一個問題,從cookie中存儲數據到html值字段。我用PHP生成html。PHP存儲變量到html值字段

代碼:

$cookieName = $_COOKIE["username"]; 
$cookiePass = $_COOKIE["password"]; 

if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){ 
     echo "<input type='text' name='name' value='echo $cookieName'>"; 
     echo "<input type='password' name='pass' value= 'echo $cookiePass'>"; 
}else{ 
     echo "<input type='text' name='name'>"; 
     echo "<input type='password' name='pass'>"; 
} 
+0

這些撇號正確嗎? – ragatskynet

+0

那麼?有什麼問題? –

回答

0

你錯誤地輸出變量

更換

if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){ 
    echo "<input type='text' name='name' value='echo $cookieName'>"; 
    echo "<input type='password' name='pass' value= 'echo $cookiePass'>"; 
} 

if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){ 
    echo "<input type='text' name='name' value='{$cookieName}'>"; 
    echo "<input type='password' name='pass' value= '{$cookiePass}'>"; 
} 

您正在使用echo之內echo

0

你有一個語法錯誤。您應該連接字符串和變量,而不是使用echo兩次,而不使用它們之間的分號。我建議用雙引號將雙引號括起來。只要切換到

echo '<input type="text" name="name" value="'. $cookieName.'">'; 
echo '<input type="password" name="pass" value= "'. $cookiePass.'">'; 
0

做這個

echo "<input type='text' name='name' value='".$cookieName."'>"; 
    echo "<input type='password' name='pass' value= '".$cookiePass."'>"; 

,而不是這個 -

echo "<input type='text' name='name' value='echo $cookieName'>"; 
    echo "<input type='password' name='pass' value= 'echo $cookiePass'>"; 
0

只是做

echo "<input type='text' name='name' value='" . $cookieName . "'>"; 

不能使用裏面的PHP函數echo串。所以解決方案只是將一個字符串與你的參數結合起來。

第二種方法是使用{$ VAR}這樣的:

echo "<input type='text' name='name' value='{$cookieName}'>"; 
0
$cookieName = $_COOKIE["username"]; 
$cookiePass = $_COOKIE["password"]; 

if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){ 
    echo "<input type=\"text\" name=\"name\" value=\"{$cookieName}\">"; 
    echo "<input type=\"password\" name=\"pass\" value=\"{$cookiePass\">"; 
} else{ 
    echo "<input type='text' name='name'>"; 
    echo "<input type='password' name='pass'>"; 
}