每次我刷新我的网页时如何preserve我的输入值? [英] How preserve my inputs values each time that I refresh my page?
问题描述
我想输入一个名称和标志。作为进入商标是小于或等于100,进入了名称和标志将被存储在一个关联数组,当我点击提交按钮,它应该要求我输入另一个名称和标记。但是,如果我输入一个大于100丢弃名称进入,当我点击提交按钮标记,PHP脚本应该显示我所有的名称和标志previously输入。在这里,我做了什么,但我没有收到预期的效果。请帮忙。我的code:
< PHP
如果(使用isset($ _ POST ['LNAME'])及和放大器;使用isset($ _ POST ['标记'])){$ NAME = $ _ POST ['L-NAME'];
$标记= $ _ POST ['标记'];$名单=阵列($名=> $分);
的foreach($列为$名=> $标记){
回声$名称。 '< BR>';
回声$标记;
}
}?><形式的行动=< PHP的echo $ _ SERVER ['PHP_SELF'];?>中方法=POST>名称:< BR>
<输入类型=文本名称=lname的>< BR>< BR>
马克斯:其中; BR>
<输入类型=文本名称=标记>< BR>< BR>
<输入类型=提交值=提交>< /表及GT;
您可以使用会话来实现你想要什么,看到这个code:
< PHP
在session_start();如果(使用isset($ _ POST ['LNAME'])及和放大器;使用isset($ _ POST ['标记'])){如果($ _ POST ['标记']→100){
$ _SESSION ['信息'] [] =阵列($ _ POST ['LNAME'] => $ _ POST ['标记']);
}
}如果(使用isset($ _ SESSION ['信息'])){
为($ I = 0; $ I<计数($ _ SESSION ['信息']); $ I ++){
的foreach($ _ SESSION ['信息'] [$ i]为$名=> $标记){
呼应'< P>' 。 $名称。 '< BR>';
回声$标记。 '< / P>';
}
}
}
?><形式的行动=< PHP的echo $ _ SERVER ['PHP_SELF'];?>中方法=POST>名称:< BR>
<输入类型=文本名称=lname的>< BR>< BR>
马克斯:其中; BR>
<输入类型=文本名称=标记>< BR>< BR>
<输入类型=提交值=提交>< /表及GT;
您应该阅读一些文档有关会话:
http://php.net/manual/en/intro.session.php
块引用>I want to enter a name and a mark. As the mark entered is less or equal to 100, the names and marks entered is to be stored in an associative array when I click the submit button and it should request me to enter another name and marks. But if I enter a mark greater than 100 discarding the name entered, when I click the submit button, the php script should display me all the names and marks previously entered. Here what I have done but I am not getting the desired results. Please help. My code:
<?php if(isset($_POST['lname']) && isset($_POST['marks'])){ $name = $_POST['lname']; $marks = $_POST['marks']; $lists = array($name => $marks); foreach($lists as $name => $marks){ echo $name . '<br>'; echo $marks; } } ?> <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST"> Name:<br> <input type = "text" name = "lname"><br><br> Marks:<br> <input type = "text" name = "marks"><br><br> <input type = "submit" value = "Submit"> </form>
解决方案You can use session to achieve what you want, see this code:
<?php session_start(); if(isset($_POST['lname']) && isset($_POST['marks'])){ if($_POST['marks'] > 100) { $_SESSION['info'][] = array($_POST['lname'] => $_POST['marks']); } } if(isset($_SESSION['info'])) { for($i = 0; $i < count($_SESSION['info']); $i++) { foreach($_SESSION['info'][$i] as $name => $marks){ echo '<p>' . $name . '<br>'; echo $marks . '</p>'; } } } ?> <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST"> Name:<br> <input type = "text" name = "lname"><br><br> Marks:<br> <input type = "text" name = "marks"><br><br> <input type = "submit" value = "Submit"> </form>
You should read some docs about session:
这篇关于每次我刷新我的网页时如何preserve我的输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!