【问题标题】:PHP values from input keep resetting after enter输入后的PHP值不断重置
【发布时间】:2019-04-01 19:12:45
【问题描述】:

我正在尝试创建一个程序,该程序可以打印出由五个用户输入的元素组成的数组。每个输入框都应该从用户那里获取一个值,将其存储在一个数组中,然后打印出该数组值。但是,当您在下一个输入框中输入文本时,这些值会重置并变为空。您可以通过查看该网站了解我的意思。

https://people.emich.edu/ghaines1/cosc231/stringReverse.php

我最终也想反向打印字符串的值。我确信这很容易,但我还没有研究它,因为我需要先完成第一部分。

所以理想情况下,PHP 部分应该打印出来:

$输入[0]

$输入[1]

$输入[2]

$输入[3]

$输入[4]

假设它保留了这些值。同样,我的问题是我不能打印多个值,因为当我在下一个文本框中输入值时它们会重置并变为空。代码如下:

<form action="stringReverse.php" method = "post">
<input name="st1" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st2" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st3" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st4" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>
<form action="stringReverse.php" method = "post">
<input name="st5" type="text" size="50" placeholder="Enter phrase"><br><br>
</form>


<?php


if ($_SERVER["REQUEST_METHOD"]=="POST") {

    $input = array (0, 0, 0, 0, 0);

    if (is_null($_POST["st1"]) == false) {
        $input[0] = $_POST["st1"];
        echo $input[0] . "<br>";
    }

    if (is_null($_POST["st2"]) == false) {
        $input[1] = $_POST["st2"];
        echo $input[1] . "<br>";
    }

    if (is_null($_POST["st3"]) == false) {
        $input[2] = $_POST["st3"];
        echo $input[2] . "<br>";
    }

    if (is_null($_POST["st4"]) == false) {
        $input[3] = $_POST["st4"];
        echo $input[3] . "<br>";
    }

    if (is_null($_POST["st5"]) == false) {
        $input[4] = $_POST["st5"];
        echo $input[4] . "<br>";
    }

}
?>

【问题讨论】:

    标签: php post input


    【解决方案1】:

    因为你要为每个输入定义一个表单

    将 html 更改为:

    <form action="stringReverse.php" method = "post">
    <input name="st1" type="text" size="50" placeholder="Enter phrase"><br><br>
    <input name="st2" type="text" size="50" placeholder="Enter phrase"><br><br>
    <input name="st3" type="text" size="50" placeholder="Enter phrase"><br><br>
    <input name="st4" type="text" size="50" placeholder="Enter phrase"><br><br>
    <input name="st5" type="text" size="50" placeholder="Enter phrase"><br><br>
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      相关资源
      最近更新 更多