每次单击“提交”时添加列表项

时间:2013-05-18 20:54:41

标签: php mysql sql phpmyadmin

我想要一个脚本,每次单击提交按钮时,它都会将表单中的文本回显到div标签。

我能够立刻做到这一点,但我希望文本仍然显示在div中,即使我提交另一个。我希望每个新提交的文本都能创建一个列表。将其添加到上一个列表中。

这可能与数据库有关,但我想知道每次点击提交时我只会提交当前文本。

此类脚本的示例

<?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>

我想在每次点击提交时将条目添加到<li>列表。

4 个答案:

答案 0 :(得分:1)

如果列表是临时的,您可以使用sessions来处理此问题:

<?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 -->

答案 1 :(得分:1)

我很高兴你很开心。这是一个快速的“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 :(得分:0)

您需要继续添加会话变量并最好使用数组。

像这样;

<?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>

编辑:这仅适用于当前会话,如果您想稍后返回并查看相同的列表。您需要将值存储在数据库的某个位置。

答案 3 :(得分:0)

我以为我也会参与其中。这是一个简单的解决方案,只能在页面的当前实例中使用。

<?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>

所以这将做你想要的,但不存储到数据库中。如果您想要存储到数据库中,那么您可能需要对MySQL或其他永久存储列表项的方法进行一些研究。

希望我的帮助很多,我确信很多其他人在我打字的时候已经找到了答案......