我有这个 HTML 文档 ( test1.html ):
<!DOCTYPE html>
<html>
<body>
First name: <input type="text" id="firstname" name="firstname" value="VALUE123"><br>
<?php
echo "Hello<br>";
echo "document.getElementById('firstname').value" ;
?>
</body>
</html>
输出是:
First name: VALUE123
Hello
document.getElementById('firstname').value <=====================
如何更正代码,以便输出的最后一行显示 VALUE123
而不是变量名本身?
我必须使用 PHP(没有 Javascript),并且我需要使用 firstname
变量在 PHP 中设置 cookie()。
最佳答案
<?php
/*
The PHP setcookie() function sends an HTTP cookie to a client.
Syntax: setcookie(name,value,expire,path,domain,secure)
Only name and value are requiered
*/
if ( isset ($_POST['firstname']) )
{ // Client did send (an other) firstname.
$fnValue = $_POST['firstname'];
$expire = time()+60*60*24*3; // 60s 60m 24h 3d
setcookie('firstname',$fnValue,$expire);
}
else
{
if (isset($_COOKIE['firstname']))
$fnValue = $_COOKIE['firstname'];
else
$fnValue = "";
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="POST">
First name: <input type="text" id="firstname" name="firstname"><br>
<input type="submit" value="send">
</form>
<?php
if ( strlen($fnValue) > 0 )
{
echo "Hello<br>";
echo $fnValue;
}
?>
</body>
</html>
关于php - 无法访问 HTML 中的输入 dom 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23662776/