简体   繁体   English

如何在 HTML 属性中存储 PHP 变量?

[英]How to store a PHP variable in a HTML attribute?

I want to create a page which allows a user to select colours (among the listed ones, via checkboxes).我想创建一个页面,允许用户选择颜色(在列出的颜色中,通过复选框)。 When the user clicks on the submit button, all the selected colours should get displayed with their corresponding colours as their font colour.当用户点击提交按钮时,所有选定的颜色都应该显示为它们对应的颜色作为它们的字体颜色。

Here is my code:这是我的代码:

<?php 
if(!isset($_POST['button'])) { 
?>
    Colours<br>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=post>
    <input type="checkbox" name="colours[]" value="Red">Red<br>
    <input type="checkbox" name="colours[]" value="Blue">Blue<br>
    <input type="checkbox" name="colours[]" value="Green">Green<br>
    <input type="checkbox" name="colours[]" value="Yellow">Yellow<br>
    <input type="checkbox" name="colours[]" value="Pink">Pink<br>
    <input type="submit" name="button" value="Show"> </form>
    <?php
} else {
    if (isset($_POST['colours'])) {
        echo "Colours selected are:<br><UL type=circle>";
        foreach($_POST['colours'] as $color) 
            echo "<LI><font color='echo $color'; >$color</font>";
        echo"</UL>";
    } else { 
        echo "No colour selected"; 
    }
} 
?>

There is a bug somewhere, the font colors are not as expected.某处存在错误,字体颜色与预期不符。 I want it like this image below:我想要它像下面这张图片:

截屏

Simply write简单地写

echo "<li><font color='$color'> $color </font></li>"; // complete li tag

other mistakes:-其他错误:-

a) form method attrubute should be method='post' a) 表单方法属性应该是method='post'

b) UL type=circle should be ul type='circle' b) UL type=circle 应该是ul type='circle'

Try this;试试这个;

  echo "Colours selected are:<br><UL type=circle>";
  foreach($_POST['colours'] as $color) {
    echo " <LI><font color='".$color."'>'".$color."'</font></LI>";
  }
  echo"</UL>";

When appending a string to variable or another string, you must concatenate the values with a .将字符串附加到变量或另一个字符串时,您必须将这些值与 . (dot). (点)。 for example:例如:

echo("<li><font color=".$color.">".$color."</font>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM