HTMLフォームをPHP whileループに入れるにはどうすればよいですか?
次のように考えましたが、機能しません。
<?php
$i=1;
while ($i<=5){
<form name="X" action="thispage.php" method="POST">
<input type="text">
<input type="submit">
</form>;
$i=$i+1;
}
?>
HTMLフォームをPHP whileループに入れるにはどうすればよいですか?
次のように考えましたが、機能しません。
<?php
$i=1;
while ($i<=5){
<form name="X" action="thispage.php" method="POST">
<input type="text">
<input type="submit">
</form>;
$i=$i+1;
}
?>
そのように PHP の途中で生の HTML を使用することはできません。HTML の前に PHP ステートメントを終了し、次のようにしてから再度開きます。
<?php
$i=1;
while ($i<=5){
?>
<form name="X" action="thispage.php" method="POST">
<input type="text" name="trekking">
<input type="submit">
</form>
<?php
$i=$i+1;
}
?>
<?php
$i=1;
while ($i<=5):?>
<form name="X" action="thispage.php" method="POST">
<input type="text" name="trekking">
<input type="submit">
</form>
<?php $i=$i+1;
endwhile;
?>
endwhile
php と html を読みやすく分離するために使用します。必要がない場合は、コード ブロックをエコーしないでください。
使用できますecho
:
<?php
$i=1;
while ($i<=5){
echo '
<form name="X" action="thispage.php" method="POST">
<input type="text" name="trekking">
<input type="submit">
</form>;
';
$i=$i+1;
}
?>
または、PHP タグを開いたり閉じたりします。
<?php
$i=1;
while ($i<=5){
//closing PHP
?>
<form name="X" action="thispage.php" method="POST">
<input type="text" name="trekking">
<input type="submit">
</form>;
<?php
//opening PHP
$i=$i+1;
}
?>
まずPHPを学ぶべきです。あなたが達成しようとしているのは、非常に単純な基本的な PHP です。
ただし、質問に答える"[form-html goes here]";
には、while ループ内でエコーします。他のすべてを必ずエスケープしてください"
。
<?php
$i=1;
echo"<form name="X" action="thispage.php" method="POST">";
while ($i<=5)
{
echo"<input type="text">";
echo"<input type="submit">";
$i++;
}
echo"</form>";
?>
これを行うには、HTML の前で PHP ブロックを で閉じてから、残りのコードの前で で?>
再度開きます。<?php
個人的には、echo
PHP 内では HTML の方が好きです。コードが読みやすくなります。また、for
そこにあるものではなく、ループを使用することをお勧めします。
<?php
for ($i=1; $i<=5; $i++) {
echo '<form name="x" action="thispage.php" method="POST">',
'<input type="text" name="trekking">',
'<input type="submit"',
'</form>';
}
?>
あなたの目標が同じ名前の 5 つのフォームを出力しようとしている場合 (そもそもこれはお勧めしません)、これを試すことができます:
$i=1;
$strOutput = "";
while ($i<=5){
$strOutput .= '<form name="X" action="thispage.php" method="POST">';
$strOutput .= '<input type="text" name="trekking">';
$strOutput .= '<input type="submit">';
$strOutput .= '</form>';
$i=$i+
}
echo $strOutput;
質問で行ったように、PHP コード内で HTML を使用しないでください。