简体   繁体   English

如何在页面刷新时保留随机生成的HTML文本字段的值?

[英]How to retain values of randomly generated HTML text fields on page refresh?

I am creating HTML text at runtime like my following code. 我正在像下面的代码一样在运行时创建HTML文本。

<?php
session_start();
for(var i=1;i<=10;i++)
{ 
?>
    <div id="mainDiv<?php echo $i; ?>">
        <input type="text" id="myText<?php echo $i; ?>">
        <button type="button" id="myButton<?php echo $i; ?>" onclick="myButtonClicked()">Click Me</button>       
    </div>
<?php
}
?>

It is working fine. 一切正常。 Problem is now in retaining the values of these textboxes. 现在的问题是保留这些文本框的值。 When I refresh the page, all the values previously entered are gone. 当我刷新页面时,以前输入的所有值都消失了。 Do I need to create 10 sessions to retain the values of these 10 texts? 我是否需要创建10个会话来保留这10个文本的值? Or, is there any other way of doing this? 或者,还有其他方法吗?

You can use the locaStorage api for retain the user input by store/get to/from localStorage after page refresh. 您可以使用locaStorage api在页面刷新后通过存储/从localStorage获取/从localStorage获取来保留用户输入。 setItem() and getItem() are used for store and get item from localStorage respectively. setItem()getItem()分别用于存储和从localStorage获取项目。

Example

This html will be created by php 这个html将由php创建

<input type="text" id="text1" value="" />
<button type="button" id="myButton1" onclick="myButtonClicked('text1')">Click Me</button>

<input type="text" id="text2" value="" />
<button type="button" id="myButton1" onclick="myButtonClicked('text2')">Click Me</button>       
//like this other input and button will be created.

JS JS

if(localStorage.getItem('resArr') == null){
    var resultArr = [];
}else{
    resultArr = JSON.parse(localStorage.getItem('resArr'));
    //---- get stored item from localStorage by json parsing
}

alert('the last item you entered is ' + resultArr[resultArr.length-1]);
function myButtonClicked(id){
    var userVal = document.getElementById(id).value;
    resultArr.push(userVal);
    localStorage.setItem('resArr', JSON.stringify(resultArr));
    //stored the entered value in localStorage by doing stringify
}

DEMO DEMO

I don't know if this will help you exactly 我不知道这是否会对您有帮助
but you can achieve what you want through a form that submits to php_self.. 但您可以通过提交给php_self的表单来实现所需的功能。

<?php

//create blank array for storing text box values
for($i=1;$i<=10;$i++){
   $textValue[$i]='';}

//if page reached via submission of button, store posted text box values in array
if(isset($_POST['myButton'])){
   for($i=1;$i<=count($textValue);$i++){
      $textValue[$i]=$_POST['myText'.$i];}}

?>

 <!--create form that submits to same page-->
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data" name="main">

<?php

//loop creates text boxes and buttons while getting text box default values from $textValue Array
for ($i=1;$i<=count($textValue);$i++){ 
   echo '<div id="mainDiv'.$i.'">
   <input type="text" id="myText'.$i.'" name="myText'.$i.'" value="'.$textValue[$i].'"/>
   <button id="myButton'.$i.'" name="myButton" value="myButton'.$i.'">Click Me</button>    
</div>
';}

?>

</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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