【问题标题】:Do HTML disappear between 2 opening and closing PHP tags?HTML 会在 2 个开始和结束 PHP 标记之间消失吗?
【发布时间】:2019-12-14 06:42:28
【问题描述】:

我正在尝试在 for 循环中使用 php 打印表单,我的目标是打印与数组元素一样多的按钮,但是当我尝试在浏览器中执行此操作时,html 表单不会显示在页面上,或者在源代码中。

<div class="col text-center">
    <?php
    $max = count($array);
    for($i = 0 ; $i < $max ; $i++){
        $item = $array[$i];
        $item = str_replace(' ', '', $item);
        ?>
        <form method="GET" action="page.php?ders=<?php echo $item;?>" target="_blank" name="f1">
            <input type="hidden" name="item" value="<?php echo $item;?>">
             <a class="btn btn-lg btn-primary"><?php echo $item;?> &raquo;</a>
        </form>
        <br>
        <?php
    }
    ?>
</div>

当我使用命令 php script.php 在控制台上运行它时,它会显示所有具有正确值的表单,当我将输出放在 page.html 上时,它会显示按钮,但我需要 page.php 而 php 必须在浏览器的 page.php 上呈现 html 表单,我错过了什么吗?

【问题讨论】:

  • 据我了解,您有一个文件的 PHP 脚本不工作,对吧?
  • 不,我有它的工作,但我认为我的变量现在我会检查数组

标签: php html


【解决方案1】:

您的代码存在许多问题。我将尝试逐行指出它们并使用替代方法

$max = count($array);

除非您已经检查了$array 是否存在,否则应该检查上面的行。如果不这样做,您可能会遇到错误:

$max = (isset($array)) ? count($array) : 0;

这两行可以从以下位置压缩:

$item = $array[$i];
$item = str_replace(' ', '', $item);

到以下。这真的是个人的事情。注意:稍后您渲染$item。如果$item 可以是任何东西,您需要使用htmlentities() 以确保您的页面不会受到XSS attacks 的攻击。

$item = str_replace(' ', '', $array[$i]);

现在这条线很有趣。您正在为每个 $item 创建一个新表单。这真的是你想做的吗?

<form method="GET" action="page.php?ders=<?php echo $item;?>" target="_blank" name="f1">

不管怎样,你真的要包含属性target="_blank"吗?

下一个问题是您为每个表单指定了与name="f1" 相同的名称。如果不是现在,那么这很可能会导致问题,然后是任何扩展。像下面这样会更好:

<form method="GET" action="page.php?ders=<?=$item?>" name="f1<?=$item?>">

我相信的下一行暗示了你真正想要的东西。基本上,除非您确实需要提交给page.php 的表单,否则它会使您的其余代码看起来毫无意义,我认为您不会这样做,因为您已经将查询字符串变量ders 设置为表单action 属性。

 <a class="btn btn-lg btn-primary"><?php echo $item;?> &raquo;</a>

我认为您可以拥有以下内容并删除几乎所有其他内容:

 <a href="page.php?ders=<?=$item?>" class="btn btn-lg btn-primary"><?=$item?> &raquo;</a>

根据我认为您正在尝试做的事情,您应该能够将其重写为:

<div class="col text-center">
<?php
    $max = (isset($array)) ? count($array) : 0;
    if (! $max) {
        echo 'No items in array to render!';
    } else {
        for($i=0;$i<$max;$i++){
            $item = str_replace(' ', '', $array[$i]);
            // make the following one line
            echo '<a href="page.php?ders='.$item.'" 
                    class="btn btn-lg btn-primary"
                    target="_blank">'.$item.' &raquo;</a>
                    <br />';
        }
    }
?>
</div>

【讨论】:

  • 我的问题是因为错误我得到了一个空数组,现在我有了数组我的主要问题是固定的,但是你已经指出了许多优点和许多多余的东西完成,我会修复它们,感谢它。
  • @handsomejack:不客气。顺便说一句,潘多拉永远属于不法分子:)
【解决方案2】:
<div class="col text-center">
    <?php
        foreach($array as $value):
            $item = str_replace(' ', '', $value); ?>
            <form method="GET" action="page.php?ders=<?= $item;?>" target="_blank" name="f1">
                 <input type="hidden" name="item" value="<?= $item;?>">
                 <a class="btn btn-lg btn-primary"><?= $item;?> &raquo;</a>
             </form>
             <br>
     <?php endforeach; ?>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多