问题描述
我想要一个脚本,每当我单击提交"按钮时,该脚本就会将表单中的文本回显到div标签.
I want a script which will echo a text from a form to a div tag every time i click the submit button.
我很快就可以做到这一点,但是我希望即使我提交另一个文本,文本也仍然可以显示在div中.我希望每个新提交的文本都创建一个列表.将其添加到上一个列表.
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.
此类脚本的示例
<?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>
列表中.
i want to just be adding entries to the <li>
list every time i click submit.
推荐答案
我很高兴您玩得开心.这是一个快速的入门10":)
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>
这篇关于每次单击提交时添加列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!