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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀