与使用PHP打印HTML相关


Related to Printing HTML using PHP

我正在尝试用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>";

在上面的代码中,我想要一个类似$task["id"]的php变量来代替var_value,我已经尝试将其作为

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

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

只需执行此操作,因为您已经使用了echo。。。

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

如果试图打印类似的值,如果条件使用

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

尝试在HTML中使用PHP

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

尝试字符串串联:

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

或多个回波:

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

单人间怎么样?:

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

由于您已经在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/php代码更容易发现错误

使用字符串串联!像这样:

<?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>';
?>

使其更清洁

在表单之前关闭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"]?> ">

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>

通过使用htmlspecialchar,您还可以确保HTML无论您写什么都是有效的
检查评估代码并使用它进行调整