admin管理员组

文章数量:1315258

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. This PHP file uses a JavaScript for a drop down pre-selection. Means: If a user selects a value in drop-down1 then there is a limited selection in drop-down2. This JavaScript definitely needs to reload the PHP file for changing the values of the second drop-down2 list.

The question is now: How can I keep the entered values of the user in the form, after the JavaScript has been executed. Currently all entered data get lost.

One option would be to set all values into the URL and grab them with GET. Yes.. that would be an option but as I am using 13 values, the URL would look not too nice. 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.

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.

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)

Transf.from (drop down) Health Center (text box) Ambulance (drop-down) Self.transferred (drop down) SEND BUTTON

dynmicoptionlist.js code:

   //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 munes 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=formmunes.options[formmunes.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("&munes=") != -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("&munes="));

             //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 + "&munes=" + val3 ;
         }
      }
      else {

         //The search term was not found, so just add the munes
         self.location= url1 + "&munes=" + 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:

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

The JavaScript filling the value looks like this:

<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:

var entry_date = form.entry_date.value;

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. This PHP file uses a JavaScript for a drop down pre-selection. Means: If a user selects a value in drop-down1 then there is a limited selection in drop-down2. This JavaScript definitely needs to reload the PHP file for changing the values of the second drop-down2 list.

The question is now: How can I keep the entered values of the user in the form, after the JavaScript has been executed. Currently all entered data get lost.

One option would be to set all values into the URL and grab them with GET. Yes.. that would be an option but as I am using 13 values, the URL would look not too nice. 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.

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.

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)

Transf.from (drop down) Health Center (text box) Ambulance (drop-down) Self.transferred (drop down) SEND BUTTON

dynmicoptionlist.js code:

   //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 munes 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.munes.options[form.munes.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("&munes=") != -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("&munes="));

             //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 + "&munes=" + val3 ;
         }
      }
      else {

         //The search term was not found, so just add the munes
         self.location= url1 + "&munes=" + 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:

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

The JavaScript filling the value looks like this:

<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:

var entry_date = form.entry_date.value;
Share Improve this question edited May 31, 2011 at 7:56 Daniel asked May 9, 2011 at 11:33 DanielDaniel 1,2194 gold badges18 silver badges33 bronze badges 1
  • If you know JQuery couldnt you use an ajax call to call the php script without refreshing the form page? Or just normal ajax call, just JQuery makes it very simple to use ajax calls. – John Commented May 10, 2011 at 0:30
Add a ment  | 

3 Answers 3

Reset to default 4

Maybe you try to use 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? 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.

use localStorage https://www.w3schools./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. This allows you to read, add, modify, and delete stored data items.

本文标签: phpKeep values after page reload of javascriptStack Overflow