【问题标题】:How to display multiple values in text area in php?如何在php的文本区域中显示多个值?
【发布时间】:2018-06-16 15:22:27
【问题描述】:

如何使用 php 在 textarea 字段中显示多个值?

我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Generate Random Numbers</title>
</head>
<body>

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
    <?php

        if(isset($_POST['submit']) && !empty($_POST['start']) && !empty($_POST['end']) && !empty($_POST['how_many'])){
            $start = $_POST["start"];
            $end = $_POST["end"];
            $how_many = $_POST["how_many"];
            /* function hw(){
                $how_many = $_POST["how_many"];
                $start = $_POST["start"];
                $end = $_POST["end"];*/

            for ($x=1; $x<=$how_many; $x++){
            $output= rand($start,$end);
            }
        }
            //The rand() function generates a random integer.
            //Tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100).

        else{
            $start="";
            $end="";
            $how_many="";
            $output="";
        }
    ?>

    Range start: <input type = "text" name = "start" value = "<?php echo $start; ?>"><br>
    Range end: <input type = "text" name = "end" value = "<?php echo $end; ?>"><br>
    How many? <input type = "text" name = "how_many" value = "<?php echo $how_many; ?>"><br>
    Output: <textarea><?php echo $output;?></textarea>
    <input type = "submit" name = "submit" value ="Generate Random Numbers">

</form>
</body>
</html>

如下图,文本区只出现了1个输出,虽然是循环的。

【问题讨论】:

  • 请添加代码而不是图片。
  • 努力并将您的代码粘贴为文本。
  • 如果你说的是为输出变量赋值的for循环,难道你是在覆盖“输出”值而不是附加到它上面吗?
  • 你会在那里放更多的价值,好吧,在那里放更多的价值。你试过什么?什么不工作?
  • 在 SO 上发布问题之前,您应该通过 the tour,然后转到 Help Section 阅读 What types of questions should I avoid asking?。最后,如果您确定您的问题符合规则,请阅读How to Ask a question on StackOverflow,以便能够提出一个有用、格式正确/格式正确且切题的问题。

标签: php loops textbox concatenation


【解决方案1】:

您需要在 for 循环内使用连接(在最初将 $output 声明为空字符串之后)。

我建议对用户输入设置一些限制,以免事情变得荒谬。您可以将这些设置为任何您想要的。

我将所有 POST 数据验证和变量声明打包到 if 语句中。

我将输入的type 值更改为数字,并对这些字段实施了限制。

我个人不喜欢跳进跳出 php,所以我一直呆在 php 里面,写回声,直到我用完变量。如果您愿意,欢迎您弹跳。

未经测试的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Generate Random Numbers</title>
</head>
<body>

<?php
// subjective developer configurations
$random_min=0;  // developer must set this limit
$random_max=100;  // developer must set this limit
$iterations_min=1;  // developer must set this limit
$iterations_max=20;  // developer must set this limit

echo "<form method=\"POST\" action=\"",htmlspecialchars($_SERVER['PHP_SELF']),"\">";
    if(isset($_POST['submit'],$_POST['start'],$_POST['end'],$_POST['how_many'])
       && ($start=$_POST['start'])>=$random_min && $_POST['start']<=$random_max
       && ($end=$_POST['end'])>=$random_min && $_POST['end']<=$random_max
       && ($how_many=$_POST['how_many'])>=$iterations_min && $_POST['how_many']<=$iterations_max){
        $output='';
        for($x=0; $x<$how_many; ++$x){
            $output.=rand($start,$end)."\n";
        }
    }else{
        $start=$end=$how_many=$output='';
    }
    echo "Range start: <input type=\"number\" name=\"start\" min=\"$random_min\" max=\"$random_max\" value=\"$start\"><br>";
    echo "Range end: <input type=\"number\" name=\"end\" min=\"$random_min\" max=\"$random_max\" value=\"$end\"><br>";
    echo "How many? <input type=\"number\" name=\"how_many\" min=\"$iterations_min\" max=\"$iterations_max\" value=\"$how_many\"><br>";
    echo "Output: <textarea>$output</textarea><br>"; // you may want to add a conditional rows attribute here to improve display
    ?>
    <input type="submit" name="submit" value="Generate Random Numbers">
</form>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多