Add list item every time i click submit

2020-03-31 06:33发布

问题:

I want a script which will echo a text from a form to a div tag every time i click the submit button.

i was able to do that in no time but i want the text to still be displayed in the div even when i submit another. i want every new submitted text to create a list. adding it to a previous list.

may be this got to do with database but i will like to know as every time i click the submit i only get the current text been submitted.

example of such script

<?php 
$text = $_POST['text'];

?>
<html>
<div>
<?php echo "<ul>";
echo "<li>".$text."</li>";
echo "</ul>";
?>
</div>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="text" /><br/>
<input type="submit" value="Submit"/>
</form>

</html>

i want to just be adding entries to the <li> list every time i click submit.

回答1:

I'm happy you're having fun. Here's a quick "starter for 10" :)

<?php
$items = array();
if('POST' === $_SERVER['REQUEST_METHOD']) {
    if( ! empty($_POST['item'])) {
        $items[] = $_POST['item'];
    }
    if(isset($_POST['items']) && is_array($_POST['items'])) {
        foreach($_POST['items'] as $item) {
            $items[] = $item;
        }
    }
}
?>
<html>
    <head>
        <title>Demo</title>
    </head>
    <body>
        <?php if($items): ?>
            <ul>
                <?php foreach($items as $item): ?>
                    <li><?php echo $item; ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
        <form method="post">
            <input type="text" name="item" />
            <input type="submit" value="Add Item" />
            <?php if($items): ?>
                <?php foreach($items as $item): ?>
                    <input type="hidden" name="items[]" value="<?php echo $item; ?>" />
                <?php endforeach; ?>
            <?php endif; ?>
        </form>
    </body>
</html>


回答2:

You could use sessions to handle this if the list is temporary:

<?php 
session_start();

if(isset($_POST['text']) && trim($_POST['text']) != "")
{
    // add to a session array
    $_SESSION['text'][] = $_POST['text'];
}

?>
<html>
<div>
    <ul>
    <?php if(isset($_SESSION['text']) && !empty($_SESSION['text'])): foreach($_SESSION['text'] AS $text): ?>
        <li><?php echo $text; ?></li>
    <?php endforeach; endif; ?>
    </ul>
?>
<!-- rest of your html here -->


回答3:

thought I would chime in too. This is a simple solution that will work only in the current instance of a page.

<?php 
if ( isset( $_POST['text'] ) ) { # Find out if the form had been submitted
    $text = $_POST['text']; # If so then store the submitted text in the $text var

    if ( isset( $_POST['previous'] ) ) { # Find out if there were any previous inputs
        $current = $_POST['previous'] . "," . $_POST['text']; # If there were then pop the latest one on the end of the previous ones with a comma and make that our current set of text
    } else {
        $current = $_POST['text']; # Otherwise the current set of text just comprises our most recently input text
    }
}
?>
<html>

<div>
<?php 
if ( isset( $_POST['text'] ) ) { # Find out if some text was input
    $text_arr = explode(",", $current); # If it was then take our current set of text and make an array from it
    echo "<ul>"; # Start the list
    foreach ( $text_arr as $text ) { # For each item of text that has previously been input
        echo "<li>".$text."</li>"; # Print out the list item with the text in it
    }
    echo "</ul>"; # End our list
}
?>
</div>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">

<?php if ( isset( $_POST['text'] ) ) { ?> # If the previous form submitted some text
<input type="hidden" name="previous" value="<?php echo $current ?>" /> # Store it in a hidden input field to be submitted later
<?php } ?>

Name: <input type="text" name="text" /><br/>
<input type="submit" value="Submit" />
</form>

</html>

So this will do what you want but without any storing into a database. If storing into a database is what you want to do then you might want to do some research into MySQL or some other method of permanently storing list items.

Hope mine has been of some help, I'm sure many others have popped an answer on while I have been typing this...



回答4:

You would need to keep adding to a session variable and best to use an array.

Like so;

<?php 

session_start(); // This is needed to keep your session

if(!isset($_SESSION['text'])){
   // set session array if not already set previously
   $_SESSION['text'] = array();
}

if($_SERVER['REQUEST_METHOD'] == 'POST' && strlen($_POST['text']) > 0){
   // add text to session array and escape for security
   $_SESSION['text'][] = htmlspecialchars($_POST['text'], ENT_QUOTES);
}

?>

<html>
<div>
<ul>
<?php foreach($_SESSION['text'] AS $text): ?>
<li><?php echo $text; ?></li>
<?php endforeach; ?>
</ul>
</div>

<form method="POST">
Name: <input type="text" name="text" /><br/>
<input type="submit" value="Submit"/>
</form>

</html>

Edit: This only works for the current session, if you want to come back later and see the same list. You would need to store the values somewhere like a database.