与使用PHP打印HTML有关

时间:2014-10-25 08:58:10

标签: php html forms web

我试图用php变量填充隐藏属性。

echo"<form method=\"get\" action=\"page2.php\">";
echo "<input type=\"hidden\" name=\"varname\" value=\"var_value\">";
echo"<span class=\"Icon Icon--delete Icon--large\"> </span>";
echo "<input type=\"Submit\" value=\"Delete\" style=\" background: transparent; border: 0;      font-family: Tahoma;   border-radius: 4px; width: 50px; height:30px;\">";
echo"</form>";

在上面的代码中代替var_value我想要一个像$task["id"]这样的php变量,我试着把它作为

echo "<input type=\"hidden\" name=\"varname\" value=\" "; . $task["id"]; echo "\">";

....但它不起作用,我该怎么办?谢谢你的帮助。

6 个答案:

答案 0 :(得分:3)

这样做,因为你已经使用过echo ...

echo "<input type=\"hidden\" name=\"varname\" value=\" ". $task["id"]. "\">";

如果尝试打印如果条件使用

之类的值
echo "<input type=\"hidden\" name=\"varname\" value=\" ". (($task["id"] != "")?'$task["id"]':"empty id"). "\">";

尝试使用 PHP IN HTML

<input type="hidden" name="varname" value=" <? echo $task["id"];?> " \>;

答案 1 :(得分:2)

尝试字符串连接:

echo "<input type=\"hidden\" name=\"varname\" value=\"" . $task["id"] ."\">";

或多个回声:

echo "<input type=\"hidden\" name=\"varname\" value=\""; echo $task["id"]; echo "\">";

单个qoutes怎么样?:

echo '<input type="hidden" name="varname" value="' . $task['id'] . '">';

答案 2 :(得分:2)

由于您已经在echo语句的echo字符串中,所以您需要做的就是将字符串连接为:

 echo "<input type=\"hidden\" name=\"varname\" value=\"" . $task["id"] . "\">";

或者你可以按原样输出普通的html代码,只打开php标签输出所需的变量:

<input type="hidden" name="varname" value="<?php echo $task["id"];?>">

使代码看起来更清晰,并允许您使用的IDE将html标记作为html使用,并且通过所有引用转义和混合html / {{更容易发现错误1}}代码

答案 3 :(得分:1)

使用字符串连接!像这样:

<?php

    echo '<form method="get" action="page2.php">';
        echo '<input type="hidden" name="varname" value="' . $task["id"] . '">';
        echo '<span class="Icon Icon--delete Icon--large"></span>';
        echo '<input type="Submit" value="Delete" style=" background: transparent; border: 0;      font-family: Tahoma;   border-radius: 4px; width: 50px; height:30px;">';
    echo '</form>';
?>

答案 4 :(得分:1)

让它更清洁

关闭表格前的php tag

?>

<form method="get" action="page2.php">
    <input type="hidden" name="varname" value="var_value">
<span class="Icon Icon--delete Icon--large"> </span>
<input type="Submit" value="Delete" style=" background: transparent; border: 0;      font-family: Tahoma;   border-radius: 4px; width: 50px; height:30px;">
</form>


<input type="hidden" name="varname" value="<?php echo $task["id"]?> ">

答案 5 :(得分:1)

printf怎么办?

<?php $myvar = 'hi'; ?>

<form method="get" action="page2.php">
  <?php printf('<input type="hidden" name="varname" value="%s" />', htmlspecialchars((string)$myvar)); ?>

  <span class="Icon Icon--delete Icon--large"> </span>
  <input type="Submit" value="Delete" style=" background: transparent; border: 0;      font-family: Tahoma;   border-radius: 4px; width: 50px; height:30px;">
</form>

<强>结果

<form method="get" action="page2.php">
  <input type="hidden" name="varname" value="hi &amp; I use quote &quot; too" />
  <span class="Icon Icon--delete Icon--large"> </span>
  <input type="Submit" value="Delete" style=" background: transparent; border: 0;      font-family: Tahoma;   border-radius: 4px; width: 50px; height:30px;">
</form>

使用htmlspecialchars,您也可以确保HTML无论您写什么都有效 检查eval'd code并用它调整