如何将 PHP 输入到 HTML 占位符?
How to input PHP into HTML placeholder?
我正在努力让 cookie 保存用户名,然后在他们下次访问时将其放在 HTML 占位符中。最有效的方法是什么?
我搜索了其他线程并尝试使用 'include' 并将 $name 放在输入占位符中,但它最终给我错误,因为 txtName 和 txtAge 未定义。我不确定如何重新组织我的代码以使其正常工作。
PHP&HTML 文档#1:
$doc = "<!DOCTYPE html>
<html lang='en'>
SOME HTML........
<input type='text' name='txtName' id='txtName' placeholder='$name'>
SOME HTML........
</html>";
echo $doc;
PHP/Doc#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" );
?>
一些问题:
无需添加此部分:
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>
您可以连接您的变量...在您的情况下只需这样做...
从 placeholder='$name' 更改为 placeholder='.$name.'
$doc = "<!DOCTYPE html>
<html lang='en'>
SOME HTML........
<input type='text' name='txtName' id='txtName' placeholder='.$name.'>
SOME HTML........
</html>";
echo $doc;
尝试这可能有效:
<input type="text" class="form-control" name="firstname" placeholder="<?php echo isset($_COOKIE['firstname']) ? $_COOKIE['firstname'] : 'Your First Name'; ?>" />
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" );
}
?>
我正在努力让 cookie 保存用户名,然后在他们下次访问时将其放在 HTML 占位符中。最有效的方法是什么?
我搜索了其他线程并尝试使用 'include' 并将 $name 放在输入占位符中,但它最终给我错误,因为 txtName 和 txtAge 未定义。我不确定如何重新组织我的代码以使其正常工作。
PHP&HTML 文档#1:
$doc = "<!DOCTYPE html>
<html lang='en'>
SOME HTML........
<input type='text' name='txtName' id='txtName' placeholder='$name'>
SOME HTML........
</html>";
echo $doc;
PHP/Doc#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" );
?>
一些问题:
无需添加此部分:
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>
您可以连接您的变量...在您的情况下只需这样做... 从 placeholder='$name' 更改为 placeholder='.$name.'
$doc = "<!DOCTYPE html>
<html lang='en'>
SOME HTML........
<input type='text' name='txtName' id='txtName' placeholder='.$name.'>
SOME HTML........
</html>";
echo $doc;
尝试这可能有效:
<input type="text" class="form-control" name="firstname" placeholder="<?php echo isset($_COOKIE['firstname']) ? $_COOKIE['firstname'] : 'Your First Name'; ?>" />
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" );
}
?>