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

编写一个函数,使用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>

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

nn

使用工具:PhP和HTML。 p>

所以我一直在寻找解决这个问题的方法,但是 远远没有成功。 基本上我想要一个用于用户输入的文本字段和一个提交按钮。 文本提交后,它将添加到一个由所有先前输入的文本组成的数组中。 在挖掘之后,似乎这个问题的两个可能的解决方案是创建PhP会话或使用隐藏字段。 我对如何实现Sessions或隐藏字段知之甚少。 p>

这是我到目前为止编写的代码(尝试了许多不同的变体),但我仍然得到了相同的结果 ,最新的文本覆盖现有数组,因此输出始终为“Array [0] =&gt; string”。 学习这方面的任何帮助都很棒。 p>

p>

 &lt;?php 
 
 
 
 $ item =  []; 
 
 
 
 
函数itemList(){
 
 if(isset($ _ POST ['string'])){
 
 $ pieces = explode(“”,$ _POST ['string  ']); 
 
 $ item [$ pieces] = $ _POST ['item']; 
 
返回$ item [$ pieces]; 
 
} 
 
 if(isset($ _ POST)  ['item'])){
 
 foreach($ _ POST ['item'] as $ key =&gt; $ pieces){
 
 foreach($ pieces as $ key =&gt; $ piece){\  r 
 $ item [$ pieces] = $ piece; 
 
} 
 
} 
 
} 
 
返回“&lt; input type ='hidden'name ='item'value ='  &lt;?php $ _POST ['item'];?&gt;'  /&gt;“中; 
 \ N} 
 
 
 
&GT;吗?
 
 
 
&LT; HTML&GT; 
 
&LT; HEAD&GT; 
 
&LT; /头&GT; 
 \  n&lt; body&gt; 
 
&lt; form method =“POST”&gt; 
 
&lt; input type =“text”name =“string”/&gt; 
 
&lt; input type =“submit”value  =“提交”/&gt; 
 
&lt; input type =“hidden”name =“item”value =“&lt;?php $ _POST ['item'];?&gt;”  /&gt; 
 
&lt; / form&gt; 
 
&lt; h1&gt;&lt;?php itemList();  ?&GT;&LT; / H1&GT; 
 
&LT; / BODY&GT; 
 
&LT; / HTML&GT; 代码>  PRE> 
 
  DIV> 
 
  DIV> 
  
 
  div>

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.