简体   繁体   English

重新加载javascript页面后保留值

[英]Keep values after page reload of javascript

I know this is a topic where you can find tons of answers for... I spent now quite some time to search for the solution I am looking for. 我知道这是一个主题,您可以在其中找到大量答案……我花了很多时间来寻找所需的解决方案。

I have a PHP file with a form. 我有一个带有表单的PHP文件。 This PHP file uses a JavaScript for a drop down pre-selection. 此PHP文件使用JavaScript进行下拉预选。 Means: If a user selects a value in drop-down1 then there is a limited selection in drop-down2. 意思是:如果用户在下拉列表1中选择一个值,则下拉列表2中的选择将受到限制。 This JavaScript definitely needs to reload the PHP file for changing the values of the second drop-down2 list. 此JavaScript绝对需要重新加载PHP文件才能更改第二个drop-down2列表的值。

The question is now: How can I keep the entered values of the user in the form, after the JavaScript has been executed. 现在的问题是:在执行JavaScript之后,如何在表单中保留用户输入的值。 Currently all entered data get lost. 当前,所有输入的数据都会丢失。

One option would be to set all values into the URL and grab them with GET. 一种选择是将所有值设置到URL中,然后使用GET来获取它们。 Yes.. that would be an option but as I am using 13 values, the URL would look not too nice. 是的..那将是一个选择,但是由于我使用的是13个值,因此URL看起来不太好。 I don't want to user to take notice of what happens. 我不想让用户注意到发生了什么。 I cant use the POST, as I don't push the posting button.. it's just the JavaScript that gets executed and reloads the page. 我不能使用POST,因为我不按发布按钮。.只是执行并重新加载页面的JavaScript。

I thought about filling the Session with the entered data but that is not directly possible as JavaScript is on the client side and the session is on the server side. 我考虑过用输入的数据填充Session,但这不可能直接实现,因为JavaScript在客户端,而Session在服务器端。

Do you have any suggestions? 你有什么建议吗?

Form Example: First Name (text box) Last Name (text box) Age (drop-down) Sex (drop-down) 表单示例:名字(文本框)姓氏(文本框)年龄(下拉列表)性别(下拉列表)

Province (JS drop down) District (JS drop down) Commune (JS drop down) Village (JS drop down) 省(JS下拉)地区(JS下拉)公社(JS下拉)村(JS下拉)

Transf.from (drop down) Health Center (text box) Ambulance (drop-down) Self.transferred (drop down) SEND BUTTON 来自(下拉)健康中心(文本框)的转移救护车(下拉)自我转移(下拉)的发送按钮

dynmicoptionlist.js code: dynmicoptionlist.js代码:

   //Set parameters for provinces selection
   function reload1(form) {

      var val1=form.provinces.options[form.provinces.options.selectedIndex].value;

      //Get the current URL
      var url1=window.location.href;

      //Check if the current URL has the search term in it (-1 means it is not in it)
      if(url1.search("&provinces=") != -1) {

         //Now that the search term was found, cut the search term from the URL so that it can be replaced
         //This is necessary for multiple selections in the same drop down box
     var url2 = url1.substr(0,url1.search("&provinces="));

         //If the user has selected "Please select", then dont add the provinces parameter
         if(val1 == "") {

        //Create the new URL
            self.location= url2
         }
         else {

            //Create the new URL
            self.location= url2 + "&provinces=" + val1 ;
         }
      }
      else {

         //The search term was not found, so just add the provinces
         self.location= url1 + "&provinces=" + val1;
      }
   }

   //Set parameters for districts selection
   function reload2(form) {

      var val1=form.provinces.options[form.provinces.options.selectedIndex].value; 
      var val2=form.districts.options[form.districts.options.selectedIndex].value; 

      //Get the current URL
      var url1=window.location.href;

      //Check if the current URL has the search term in it (-1 means it is not in it)
      if(url1.search("&districts=") != -1) {

         //Now that the search term was found, cut the search term from the URL so that it can be replaced
         //This is necessary for multiple selections in the same drop down box
         var url2 = url1.substr(0,url1.search("&districts="));

         //If the user has selected "Please select", then dont add the provinces parameter
         if(val2 == "") {

        //Create the new URL
            self.location= url2
         }
         else {

            //Create the new URL
            self.location= url2 + "&districts=" + val2 ;
         }
      }
      else {

         //The search term was not found, so just add the districts
         self.location= url1 + "&districts=" + val2;
      }
   }

   //Set parameters for communes selection
   function reload3(form) {

      var val1=form.provinces.options[form.provinces.options.selectedIndex].value; 
      var val2=form.districts.options[form.districts.options.selectedIndex].value; 
      var val3=form.communes.options[form.communes.options.selectedIndex].value; 

      //Get the current URL
      var url1=window.location.href;

      //Check if the current URL has the search term in it (-1 means it is not in it)
      if(url1.search("&communes=") != -1) {

         //Now that the search term was found, cut the search term from the URL so that it can be replaced
         //This is necessary for multiple selections in the same drop down box
         var url2 = url1.substr(0,url1.search("&communes="));

             //If the user has selected "Please select", then dont add the provinces parameter
         if(val3 == "") {

        //Create the new URL
            self.location= url2
         }
         else {

            //Create the new URL
            self.location= url2 + "&communes=" + val3 ;
         }
      }
      else {

         //The search term was not found, so just add the communes
         self.location= url1 + "&communes=" + val3;
      }
   }

On behalf of JohnP's suggestion here are my changes so far: I've added the onload function to fill the hidden field with a value: 到目前为止,代表JohnP的建议是我所做的更改:我添加了onload函数以用值填充隐藏字段:

<body onload=\"setValue()\">

The JavaScript filling the value looks like this: 填充值的JavaScript如下所示:

<script type=\"text/javascript\">

   function setValue() {

      document.getElementById(\"dyndrpdwnhidden\").value=\"createPatient\";
   }
</script>

In my form I've added the hidden field: 在我的表单中,我添加了隐藏字段:

<input type=\"hidden\" id=\"dyndrpdwnhidden\">

To get the values out of the form's input fields in my JavaScript dynmicoptionlist.js above I've used as an example: 为了从上面的JavaScript dynmicoptionlist.js中从表单的输入字段中获取值,我以一个示例为例:

var entry_date = form.entry_date.value;

Maybe you try to use SESSION? 也许您尝试使用SESSION? The session data will not disappear even on page refresh or redirect. 即使刷新或重定向页面,会话数据也不会消失。

To keep any data: 要保留任何数据:

session_start();
$_SESSION['data']=$_POST['data_from_previous_page'];

To read it at any page: 要在任何页面上阅读:

session_start();
$_SESSION['data'];

To clear all session data: 要清除所有会话数据:

unset($_SESSION);
if (isset($_COOKIE[session_name()])) {
   setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();

Why not just make your JS submit the form? 为什么不让您的JS提交表单? That will make the variables available, and you can activate the second dropdown by looking at the post data instead of the $_GET data that the JS sends. 这将使变量可用,并且您可以通过查看帖子数据而不是JS发送的$ _GET数据来激活第二个下拉列表。

use localStorage https://www.w3schools.com/jsref/obj_storage.asp 使用localStorage https://www.w3schools.com/jsref/obj_storage.asp

Storage Object The Storage object of the Web Storage API provides access to the session storage or local storage for a particular domain. 存储对象Web Storage API的存储对象提供对特定域的会话存储或本地存储的访问。 This allows you to read, add, modify, and delete stored data items. 这使您可以读取,添加,修改和删除存储的数据项。

继续显示一个<div>页面重新加载后的表行内</div><div id="text_translate"><p>我有一个与某些软件交互的用户界面。</p><p> 我的用户界面显示带有数据路径的数据库。 用户选择他们想要模拟/运行的数据路径,然后将数据路径发送到软件。 完成后 output 是 excel 报告保存在同一台计算机上的文件夹中。 这是我对我应该如何向用户显示用户选择模拟/运行的指定数据路径的 excel 报告感到有点困惑。</p><p> 我的想法是创建一个例程,将 excel 报告转换为 pdf,然后将报告的文件路径保存到数据库。 然后,我创建一个 id="result" 的 div,以显示在所选指定路径的表格行内,并加载 pdf_report.php,然后显示所需的报告。</p><p> 问题是每次我重新加载页面时 div 标签都会消失。 我尝试使用 localstorage,但在页面重新加载后它仍然没有显示。</p><p> 我的代码如下:</p><p> **让用户模拟/运行选择的路径</p><p><strong>搜索.php</strong></p><pre> &lt;?php... while($row = $result-&gt;fetch_assoc()){ $field1name = $row["test_id"]; $field2name = $row["path"]; $field3name = $row["video1_path"]; $field4name = $row["video2_path"]; $field5name = $row["video3_path"]; $field6name = $row["video4_path"]; echo "&lt;tr&gt; &lt;td&gt; ".$field1name." &lt;/td&gt; &lt;td&gt; ".$field2name." &lt;/td&gt; &lt;td&gt; ".$field3name." &lt;/td&gt; &lt;td&gt; ".$field4name." &lt;/td&gt; &lt;td&gt; ".$field5name." &lt;/td&gt; &lt;td&gt; ".$field6name." &lt;/td&gt; &lt;td&gt;&lt;div&gt; &lt;button class='edit' id='". $row['test_id']. "' &gt;Run&lt;/button&gt; &lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div id='result'&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;"; } }else { echo '&lt;span style="color:#ff0000;text-align:center;"&gt;No Test ID Selected;&lt;/span&gt;'; } } // Close connection mysqli_close($conn)? :&gt; &lt;/table&gt; &lt;/div&gt;&lt;br&gt; &lt;div style="overflow-x;auto."&gt; &lt;table id=test_data&gt; &lt;tr&gt; &lt;th&gt;Progress&lt;/th&gt; &lt;th&gt;Progress Status&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;div&gt;&lt;progress id='progBar' value='0' max='100'&gt;&lt;/progress&gt;&lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div&gt;&lt;p id='progress-text'&gt;&lt;/p&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var show = localStorage;getItem('showDiv'). if(show === 'true'){ $("#result");show(). } &lt;/script&gt; &lt;,--Uses jquery to run 3 scripts and displays it in a progress bar--&gt; &lt;script&gt; $(document).on('click', ';edit'. function(event){ //set cookie value to 'path' var fcookie='mycookie'. //if button inside row is clicked the path gets saved to a cookie and received in ajax;php var test_id = $(this).attr('id'). if(test_id) { var path = $(this):closest('tr').find("td;nth-child(2)").text(); //Cookie gets saved document;cookie='fcookie='+path. var $this = $(this): //Start of 1st script $.ajax({ url, "ajax:php", type:"POST". success; function(data) { //alert("File 1 Completed") $("#progress-text").text("Executing file 1"); $('#progBar').val(25): //Start of 2nd script $.ajax({ url, "ajax2:php", type:"POST". success; function(data2) { //alert("File 2 Completed") $("#progress-text").text("Executing file 2"); $('#progBar').val(50): //Start of 3rd script $.ajax({ url, "ajax3:php", type:"POST". success; function(data3) { //alert("File 2 Completed") $("#progress-text").text("Complete"); $('#progBar').val(100). //Displays the &lt;div id=result&gt; for the selected data path $this:closest("tr").find("td.nth-child(8)");load("pdf_report.php"); event.preventDefault(); $("#result").show(), localStorage;setItem('showDiv'; true); } }); } }); } }); } }); &lt;/script&gt;</pre></div> - Keep displaying a <div> inside table row after page reload

暂无
暂无

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

相关问题 使用 php 在 selenium 中保持 2 分钟后重新加载页面 - keep reload the page after 2 minutes in selenium with php 重新加载页面后如何保持活动的listGroup? - How to keep active listGroup after reload page? 数据库更改后使用 Javascript 重新加载页面 - Reload Page with Javascript after Database changes 如何防止我的JavaScript计时器重新启动页面重新加载 - How to keep my javascript timer from restarting on page reload 在页面重新加载到自身后,在 php 中保留文本框中的值? - keep value in textbox after page reload to itself in php? 在返回按钮后重新加载页面,但保持滚动位置 - reload page after back button but keep scroll position 重新加载页面后保留文件上传值 - keep file-upload value after page reload 页面重新加载后如何保持最喜欢/不喜欢的按钮颜色? - How to keep favorite / unfavorite button color after page reload? 在PHP中重新加载页面后,请保持选中单选按钮 - Keep radio button checked after page reload in PHP 继续显示一个<div>页面重新加载后的表行内</div><div id="text_translate"><p>我有一个与某些软件交互的用户界面。</p><p> 我的用户界面显示带有数据路径的数据库。 用户选择他们想要模拟/运行的数据路径,然后将数据路径发送到软件。 完成后 output 是 excel 报告保存在同一台计算机上的文件夹中。 这是我对我应该如何向用户显示用户选择模拟/运行的指定数据路径的 excel 报告感到有点困惑。</p><p> 我的想法是创建一个例程,将 excel 报告转换为 pdf,然后将报告的文件路径保存到数据库。 然后,我创建一个 id="result" 的 div,以显示在所选指定路径的表格行内,并加载 pdf_report.php,然后显示所需的报告。</p><p> 问题是每次我重新加载页面时 div 标签都会消失。 我尝试使用 localstorage,但在页面重新加载后它仍然没有显示。</p><p> 我的代码如下:</p><p> **让用户模拟/运行选择的路径</p><p><strong>搜索.php</strong></p><pre> &lt;?php... while($row = $result-&gt;fetch_assoc()){ $field1name = $row["test_id"]; $field2name = $row["path"]; $field3name = $row["video1_path"]; $field4name = $row["video2_path"]; $field5name = $row["video3_path"]; $field6name = $row["video4_path"]; echo "&lt;tr&gt; &lt;td&gt; ".$field1name." &lt;/td&gt; &lt;td&gt; ".$field2name." &lt;/td&gt; &lt;td&gt; ".$field3name." &lt;/td&gt; &lt;td&gt; ".$field4name." &lt;/td&gt; &lt;td&gt; ".$field5name." &lt;/td&gt; &lt;td&gt; ".$field6name." &lt;/td&gt; &lt;td&gt;&lt;div&gt; &lt;button class='edit' id='". $row['test_id']. "' &gt;Run&lt;/button&gt; &lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div id='result'&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;"; } }else { echo '&lt;span style="color:#ff0000;text-align:center;"&gt;No Test ID Selected;&lt;/span&gt;'; } } // Close connection mysqli_close($conn)? :&gt; &lt;/table&gt; &lt;/div&gt;&lt;br&gt; &lt;div style="overflow-x;auto."&gt; &lt;table id=test_data&gt; &lt;tr&gt; &lt;th&gt;Progress&lt;/th&gt; &lt;th&gt;Progress Status&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;div&gt;&lt;progress id='progBar' value='0' max='100'&gt;&lt;/progress&gt;&lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div&gt;&lt;p id='progress-text'&gt;&lt;/p&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var show = localStorage;getItem('showDiv'). if(show === 'true'){ $("#result");show(). } &lt;/script&gt; &lt;,--Uses jquery to run 3 scripts and displays it in a progress bar--&gt; &lt;script&gt; $(document).on('click', ';edit'. function(event){ //set cookie value to 'path' var fcookie='mycookie'. //if button inside row is clicked the path gets saved to a cookie and received in ajax;php var test_id = $(this).attr('id'). if(test_id) { var path = $(this):closest('tr').find("td;nth-child(2)").text(); //Cookie gets saved document;cookie='fcookie='+path. var $this = $(this): //Start of 1st script $.ajax({ url, "ajax:php", type:"POST". success; function(data) { //alert("File 1 Completed") $("#progress-text").text("Executing file 1"); $('#progBar').val(25): //Start of 2nd script $.ajax({ url, "ajax2:php", type:"POST". success; function(data2) { //alert("File 2 Completed") $("#progress-text").text("Executing file 2"); $('#progBar').val(50): //Start of 3rd script $.ajax({ url, "ajax3:php", type:"POST". success; function(data3) { //alert("File 2 Completed") $("#progress-text").text("Complete"); $('#progBar').val(100). //Displays the &lt;div id=result&gt; for the selected data path $this:closest("tr").find("td.nth-child(8)");load("pdf_report.php"); event.preventDefault(); $("#result").show(), localStorage;setItem('showDiv'; true); } }); } }); } }); } }); &lt;/script&gt;</pre></div> - Keep displaying a <div> inside table row after page reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM