【问题标题】:Keep input value after refresh page刷新页面后保留输入值
【发布时间】:2016-12-20 04:29:02
【问题描述】:

我有一个带有输入字段的表单,这个输入包含一个从数据库读取信息的下拉菜单。 如果用户输入值并且当他到达下拉菜单时他没有找到他想要的内容,他会转到另一个页面将此信息添加到下拉菜单中,然后转到第一页继续输入信息。 如果他转到另一个页面将信息添加到下拉菜单,我该如何保留此信息,以及在将信息添加到下拉菜单后如何在不刷新且不提交的情况下找到此信息。

这是表单的第一页

<form name='' method='post' action='<?php $_PHP_SELF ?>'>
<input name='txt_name' id='' type='text'>

这个下拉菜单从数据库中读取

 <select id="groups" name="txt_label" class="form-control">
   ';?>
     <?php 
    $sql=mysqli_query($conn,"select DISTINCT db_label from tbl_label")or die(mysqli_error($conn));
 echo'<option  value="">-- Select --</option>';
    while($row=mysqli_fetch_array($sql)){
        $label=$row['db_label'];
        echo "<option value='$label'>$label</option>"; 
    }echo'</select>';?><?php echo'
  </div>
</form>

另一个页面中的第二个表单

<form class="form-inline" role="form" name="form" method="post" action="';?><?php $_PHP_SELF ?><?php echo'">
    <div class="form-group">
    <label for="pwd">Label</label>
  <input id="txt_label" name="txt_label" type="text" placeholder="Label" class="form-control input-md">
  </div>
   <div class="form-group">
    <label for="pwd">Sub Label</label>
  <input id="txt_sublabel" name="txt_sublabel" type="text" placeholder="SubLabel" class="form-control input-md">
  </div>
   <input type="submit" name="addlabel" value="Add" class="btn btn-default">';

【问题讨论】:

  • 您可以在发送标头之前使用session_start();,然后使用$_SESSION 超级全局。当然,我建议你使用 AJAX,除非你更喜欢被困在石器时代。
  • 如果我想使用 ajax 我该怎么做
  • 以及如何在不提交的情况下使用会话
  • JavaScript 现在有localStoragesessionStorage。 JavaScript 的document.cookie 应该适用于所有浏览器,但对初学者来说更复杂。你对 JavaScript 了解多少?
  • 我是 javascript 的初学者,你能帮我做吗

标签: javascript php jquery ajax


【解决方案1】:

编辑:保持更多输入的价值

HTML:

<input type="text" id="txt_1" onkeyup='saveValue(this);'/> 
<input type="text" id="txt_2" onkeyup='saveValue(this);'/> 

Javascript:

<script type="text/javascript">
        document.getElementById("txt_1").value = getSavedValue("txt_1");    // set the value to this input
        document.getElementById("txt_2").value = getSavedValue("txt_2");   // set the value to this input
        /* Here you can add more inputs to set value. if it's saved */

        //Save the value function - save it to localStorage as (ID, VALUE)
        function saveValue(e){
            var id = e.id;  // get the sender's id to save it . 
            var val = e.value; // get the value. 
            localStorage.setItem(id, val);// Every time user writing something, the localStorage's value will override . 
        }

        //get the saved value function - return the value of "v" from localStorage. 
        function getSavedValue  (v){
            if (!localStorage.getItem(v)) {
                return "";// You can change this to your defualt value. 
            }
            return localStorage.getItem(v);
        }
</script>

【讨论】:

  • @user3018000 然后您需要编辑函数以使其还接收要保存的名称,值。还要 getSavedValue (v) 使其返回 "v" 的值。
  • lgbaryya 如果我有 2 个或更多输入,我该如何修改这个函数
  • @Igbaryya 你解决了我持续了很多天的搜索。多年后也是如此……谢谢伙计!
【解决方案2】:

如果上面的代码不起作用,试试这个:

<input type="text" id="txt_1" onchange='saveValue(this);'/> 
<input type="text" id="txt_2" onchange='saveValue(this);'/>

【讨论】:

    【解决方案3】:

    如果您使用钩子,也可以使用 react context() 中的 useContext()

    【讨论】:

      【解决方案4】:

      在 MVC/Razor 中, 首先你应该在你的模型类中添加一个变量 像这样的文本框:

      命名空间 MVCStepByStep.Models { 公共类 CustomerClass { 公共字符串 CustomerName { get;放; }
      } }

      然后在 Views --> Index.cshtml 文件中确保 Textbox 是这样创建的: @Html.TextBoxFor(m => m.CustomerName)

      如需完整示例,请查看此网站:

      How to update a C# MVC TextBox By Clicking a Button using JQuery – C# MVC Step By STep[^]

      【讨论】:

      • 由于问题是用 php 标签提出的,我认为 C# 的解决方案不会有助于解决这个特定问题
      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多