简体   繁体   中英

Write a function that puts a list of items from a form into an array (text field) using 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> 

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM