我正在尝试创建它,以便cookie保存用户名,然后在下次访问时将其放入HTML占位符。这样做最有效的方法是什么?
我搜索了其他线程并尝试使用'include'并将$ name放在输入占位符中,但它最终给我错误,因为txtName和txtAge未定义。我不确定如何重新组织我的代码以使其正常工作。
PHP& HTML Doc#1:
$doc = "<!DOCTYPE html>
<html lang='en'>
SOME HTML........
<input type='text' name='txtName' id='txtName' placeholder='$name'>
SOME HTML........
</html>";
echo $doc;
PHP /文件#2:
<?php
$name = $_POST["txtName"];
$age = $_POST["txtAge"];
$k = $_POST['radRaceLength'];
///check if cookie exists
if ( isset($_COOKIE["nameData"]) ){
$name = $_COOKIE["nameData"];
}
else{
$name = $name;
}
if ( isset($_COOKIE["ageData"]) ){
$age = $_COOKIE["ageData"];
}
else{
$age = $age;
}
setcookie("nameData", $name, time() + 3600, "/");
setcookie("ageData", $age, time() + 3600, "/");
$fiveK = 10 + ($age / 2);
$tenK = 18 + ($age / 3);
$member = isset($_POST["chkMember"]) ? $fiveK = $fiveK - 5 AND $tenK = $tenK - 5: $member = NULL;
if ( !$name AND $age <= 21 )
echo "Please provide a name and valid age";
else
echo $name.', you are registered for the '.$k.' race. Your fee is: ';
echo ( ($_POST["radRaceLength"] == "5K")? "$fiveK":"$tenK" );
?>
答案 0 :(得分:2)
DOC1:检查是否在doc2中设置了cookie
<$php
$name = "Name";
if(isset($_COOKIE["nameData"])) {$name = $_COOKIE["nameData"];}
?>
<!DOCTYPE html>
<html lang='en'>
SOME HTML
<input type='text' name='txtName' id='txtName' placeholder='<?php echo $name ?>' />
SOME HTML
</html>;
DOC2:
<?php
$name = 'Name'; $age = 'Age'; $k = 0;
//if data in cookie, set.
if (isset($_COOKIE["nameData"])) $name = $_COOKIE["nameData"];
if (isset($_COOKIE["ageData"])) $age = $_COOKIE["ageData"];
//if data in post, set and rewrite cookie.
if(isset($_POST["txtName"])) $name = $_POST["txtName"];
if(isset($_POST["txtAge"])) $age = $_POST["txtAge"];
if(isset($_POST["radRaceLength"])) $k = $_POST["radRaceLength"];
setcookie("nameData", $name, time() + 3600, "/");
setcookie("ageData", $age, time() + 3600, "/");
$fiveK = 10 + ($age / 2);
$tenK = 18 + ($age / 3);
$member = isset($_POST["chkMember"]) ? $fiveK = $fiveK - 5 AND $tenK = $tenK - 5: $member = NULL;
if (!$name AND $age <= 21 ) {
echo "Please provide a name and valid age";
} else {
echo "{$name}, you are registered for the {$k} race. Your fee is: ";
echo ( ($_POST["radRaceLength"] == "5K")? "$fiveK":"$tenK" );
}
?>
答案 1 :(得分:1)
无需添加此部分:
else{
$name = $name;
}
完全没有。也适用于本部分:
else{
$age = $age;
}
如果您想在If-Statement
中执行多行PHP,例如
else
echo $name.', you are registered for the '.$k.' race. Your fee is: ';
echo ( ($_POST["radRaceLength"] == "5K")? "$fiveK":"$tenK" );
你应该把它包在像这样的大括号中:
else{
echo $name.', you are registered for the '.$k.' race. Your fee is: ';
echo ( ($_POST["radRaceLength"] == "5K")? "$fiveK":"$tenK" );
}
读取cookie并将其放入HTML输入的最有效方法是什么?
我不知道为什么所有人都试图将这一点添加到他们的问题中,因为术语有效,在许多情况下都没有意义。您只是在阅读cookie并将其放入HTML输入中。
因此,简单而且更可能正确的答案就是这样做。
不要像这样杀死标记。而是在你真正需要的时候打开PHP标签,并在你不用的时候关闭它!
<!DOCTYPE html>
<html lang="en">
<input type="text" name="txtName" placeholder="<?php echo $name;?>">
</html>
答案 2 :(得分:0)
您可以连接您的变量...在您的情况下只需执行此操作... 从占位符=&#39; $ name&#39; 更改为占位符=&#39;。$ name。&#39;
$doc = "<!DOCTYPE html>
<html lang='en'>
SOME HTML........
<input type='text' name='txtName' id='txtName' placeholder='.$name.'>
SOME HTML........
</html>";
echo $doc;
答案 3 :(得分:0)
尝试这可能有效:
<input type="text" class="form-control" name="firstname" placeholder="<?php echo isset($_COOKIE['firstname']) ? $_COOKIE['firstname'] : 'Your First Name'; ?>" />