猿问

HTML 会在 2 个开始和结束 PHP 标记之间消失吗?

我正在尝试在 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 必须呈现 html浏览器上 page.php 上的表单,我错过了什么吗?


Smart猫小萌
浏览 127回答 2
2回答

忽然笑

您的代码存在许多问题。我将尝试逐行指出它们并使用替代方法$max = count($array);除非您已经检查过$array是否存在,否则应检查上述行。如果不这样做,您最终可能会出现错误:$max = (isset($array)) ? count($array) : 0;这两行可以从以下内容压缩:$item = $array[$i];$item = str_replace(' ', '', $item);到以下。这真的是个人的事情。注意:稍后您渲染$item. If $itemcan 是您需要使用的任何东西htmlentities(),以确保您的页面不易受到XSS 攻击。$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属性设置了查询字符串变量。&nbsp;<a class="btn btn-lg btn-primary"><?php echo $item;?> &raquo;</a>我认为您可以拥有以下内容并删除几乎所有其他内容:&nbsp;<a href="page.php?ders=<?=$item?>" class="btn btn-lg btn-primary"><?=$item?> &raquo;</a>根据我认为您正在尝试做的事情,您应该能够将其重写为:<div class="col text-center"><?php&nbsp; &nbsp; $max = (isset($array)) ? count($array) : 0;&nbsp; &nbsp; if (! $max) {&nbsp; &nbsp; &nbsp; &nbsp; echo 'No items in array to render!';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; for($i=0;$i<$max;$i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item = str_replace(' ', '', $array[$i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // make the following one line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<a href="page.php?ders='.$item.'"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="btn btn-lg btn-primary"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target="_blank">'.$item.' &raquo;</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?></div>

元芳怎么了

<div class="col text-center">&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; foreach($array as $value):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item = str_replace(' ', '', $value); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form method="GET" action="page.php?ders=<?= $item;?>" target="_blank" name="f1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="hidden" name="item" value="<?= $item;?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a class="btn btn-lg btn-primary"><?= $item;?> &raquo;</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;<?php endforeach; ?></div>
随时随地看视频慕课网APP
我要回答