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.

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集