douyi2664 2015-09-17 20:04
浏览 94
已采纳

编写一个函数,使用PhP / HTML将表单中的项列表放入数组(文本字段)

The problem: Write a function that puts a list of items from a form into an array (text field) using PhP/HTML.

Tools to use: PhP and HTML.

So I have been looking around a lot to find a solution to this problem but so far have been unsuccessful. Essentially I want a text field for user input and a submit button. Once the text has been submitted it is added to an array which consists of all previously entered text. After digging around it seems that 2 possible solutions to this problem would be to create PhP Sessions or the use of Hidden fields. I have minimal to no knowledge of how to implement Sessions or hidden fields.

Here is the code I wrote so far(trying many different variations), but I still end up with the same results, the newest text overwrites the existing array so the output is always "Array[0] => string". Any help in learning this would be awesome.

<?php

$item = [];

function itemList() {
  if(isset($_POST['string'])) {
    $pieces = explode(" ", $_POST['string']);
    $item[$pieces] = $_POST['item'];
    return $item[$pieces];
  }
  if(isset($_POST['item'])) {
    foreach($_POST['item'] as $key => $pieces) {
      foreach($pieces as $key => $piece) {
        $item[$pieces] = $piece;
      }
    }
  }
  return "<input type='hidden' name='item' value='<?php $_POST['item']; ?>' />";
}

?>

<html>
<head>
</head>
<body>
<form method="POST">
    <input type="text" name="string" />
    <input type="submit" value="Submit" />
    <input type="hidden" name="item" value="<?php $_POST['item']; ?>" />
</form>
<h1><?php itemList(); ?></h1>
</body>
</html>

</div>
  • 写回答

1条回答 默认 最新

  • dqjgf0982 2015-09-17 20:39
    关注

    It looks like you're doing this a little complicated. There's no obvious need to use a function. You could also just as easily work with string concatenation, without the use of an array. But since you asked for a function and the use of an array, maybe this could work:

    <?php 
    function itemList() {
        $pieces = explode(' ', $_POST['item']);
        array_push($pieces, $_POST['string']);
        return $pieces;
    }
    $pieces = itemList();
    ?>
    <html>
    <head>
    </head>
    <body>
    <form method="POST">
        <input type="text" name="string" />
        <input type="submit" value="Submit" />
        <input type="hidden" name="item" value="<?php echo implode(' ', $pieces) ?>" />
    </form>
    <p><?php echo implode(' ', $pieces) ?></p>
    </body>
    </html>
    

    When the form is sent, the hidden field "item" is exploded into an array, the data from the text field is appended to the array. After that the form is rendered anew and the value will consist of the re-imploded array, including the new value. Since everything is done with superglobals ($_POST) the function doesn't have to accept arguments. It returns the array with its newly appended item.

    In your code, the function would be called after the form was rendered, so PHP, being a serverside language, has no possibility to change the form anymore. To do things in that order you'd have to use javascript. Furthermore in your PHP tags you just stated the names of variables without mentioning that you want to e.g. echo them. If you don't want to write out the echo you could do that in the form:

    <input type="hidden" name="item" value="<?=$somevariable ?>" />
    

    And finally here's a version using a session:

    <?php
    session_start();
    function itemList(){
        if(!isset($_SESSION['pieces'])){
            $_SESSION['pieces'] = array();
        }
        array_push($_SESSION['pieces'], $_POST['string']);
        return $_SESSION['pieces'];
    }
    $pieces = itemList();
    $out = implode(' ', $pieces);
    ?>
    <html>
    <head>
    </head>
    <body>
    <form method="POST">
        <input type="text" name="string" />
        <input type="submit" value="Submit" />
    </form>
    <p><?=$out ?></p>
    </body>
    </html>
    

    session_start() has to be called before any output to the browser is sent. The user is identified per default with a cookie that binds him to his session. The $_SESSION array holds an item that is the pieces array and the new strings are appended to that. $_SESSION will store this data as long as the session is valid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求计算赫斯特(Hurst)指数
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大