Php将变量存储到html值字段中

时间:2013-05-28 10:49:38

标签: php html

我在将数据存储到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'>";
 }

5 个答案:

答案 0 :(得分:0)

您遇到语法错误。您应该连接字符串和变量,而不是使用echo两次而不使用它们之间的分号。我建议用引号反转双引号。只需更改为

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

答案 1 :(得分: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'>";

答案 2 :(得分:0)

只做

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

你不能在String中使用PHP函数echo。所以解决方案就是将字符串与您的参数结合起来。

第二种方法是使用{$ var},如下所示:

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

答案 3 :(得分: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'>";
}

答案 4 :(得分: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