PHP while 循环中的 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13882656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:13:06  来源:igfitidea点击:

HTML form in PHP while loop

phphtmlformsloopswhile-loop

提问by TavernSenses

How can I put an html form into a PHP while loop?

如何将 html 表单放入 PHP while 循环中?

It thought something like this, but it doesn't work:

它想到了这样的事情,但它不起作用:

<?php

$i=1;
while ($i<=5){

<form name="X" action="thispage.php" method="POST">
     <input type="text">
     <input type="submit">
</form>;

$i=$i+1;

              }

?>

回答by Rick Calder

You can, you just can't have raw HTML in the middle of the PHP like that. End the PHP statement before the HTML then reopen it after like this:

你可以,你就是不能像那样在 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;
     }

?>

回答by daveyfaherty

<?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;

?>

Use endwhileto make a nice readable separation of php and html. Don't echo blocks of code if you don't have to.

用于endwhile对 php 和 html 进行很好的可读分离。如果不需要,请不要回显代码块。

回答by Alvaro

You can use echo:

您可以使用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;
}
?>

Or otherwise, open and close PHP tags:

否则,打开和关闭 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;
}
?>

回答by Brad

You can do that by closing your PHP block before the HTML with ?>, and then reopening with <?phpbefore the rest of your code.

您可以通过在 HTML 之前用 关闭 PHP 块?>,然后<?php在其余代码之前重新打开来实现。

Personally though, I prefer to echoHTML when within PHP. It makes your code more readable. Also, I suggest using a forloop rather than what you have there.

不过就我个人而言,我更喜欢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>';
}
?>

回答by patman

You should learn PHP first. What you trying to achieve ist pretty simple basic PHP.

你应该先学习PHP。您试图实现的是非常简单的基本 PHP。

But to answer your question echo "[form-html goes here]";inside the while loop. Make sure to escape all the other ".

但是要回答您的问题"[form-html goes here]";,请在 while 循环内回声。确保逃脱所有其他人"

回答by Robin van Baalen

If your goal is trying to output 5 forms with the same name (which I wouldn't recommend in the first place), you could try this:

如果您的目标是尝试输出 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;

Neveruse HTML inside PHP code like you did in your question.

永远不要像在问题中那样在 PHP 代码中使用 HTML。

回答by Ali Mohammadi

<?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>";

?>