![](/img/trans.png)
[英]How to keep PHP opening and closing tags in ckeditor source as html entity?
[英]Do HTML disappear between 2 opening and closing PHP tags?
我正在嘗試在 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;?> »</a>
</form>
<br>
<?php
}
?>
</div>
當我使用命令 php script.php 在控制台上運行它時,它會顯示所有具有正確值的表單,當我將輸出放在 page.html 上時,它會顯示按鈕,但我需要 page.php 並且 php 必須呈現 html瀏覽器上 page.php 上的表單,我錯過了什么嗎?
您的代碼存在許多問題。 我將嘗試逐行指出它們並使用替代方法
$max = count($array);
除非您已經檢查過$array
是否存在,否則應該檢查上面的行。 如果你不這樣做,你可能會得到一個錯誤:
$max = (isset($array)) ? count($array) : 0;
這兩行可以壓縮為:
$item = $array[$i];
$item = str_replace(' ', '', $item);
到以下。 這真的是個人的事情。 注意:稍后您渲染$item
。 如果$item
可以是任何你需要使用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
屬性。
<a class="btn btn-lg btn-primary"><?php echo $item;?> »</a>
我認為您可以擁有以下內容並刪除幾乎所有其他內容:
<a href="page.php?ders=<?=$item?>" class="btn btn-lg btn-primary"><?=$item?> »</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.' »</a>
<br />';
}
}
?>
</div>
<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;?> »</a>
</form>
<br>
<?php endforeach; ?>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.