I am using html and javascript for a simple application. I made a dropdown list on one page say "homepage.html" I want that the value selected from the dropdown be passed to a new page say "newpage.html" and appear in a text area there. I don't want to use php etc. plz NOTE that i am not going to upload it on internet. These are just going to be offline pages.
i will deeply appreciate any help.
Asad

Recommended Answers

All 5 Replies

You would need to use cookies for this purpose. When the drop down on your page is selected, you can set the required value in the cookie, navigate to that page and read the value from that cookie when the second page loads to populate the text box.

For more information / code snippets on cookies, visit http://www.quirksmode.org/js/cookies.html

Thanx for your reply !
Because i have no experience of this work, so some questions arise in mind regarding using cookies ...
1) should the code for cookie be written in a separate cookie file or should it be written in one of the two files that pass and recieve data.
2) If it is to be written in a separate file that means that once the cookie is deleted, the only way that the whole stuff works is to rewrite the cookie file. what if i hand over my files to a friends who absolutely has no idea about html but he just uses the files to quickly check out some data. If the cookie is deleted, he will do nothing but curse me :(
3)How will the data be received in the target file ? can it be stored in some variable in a target file ?
Kindly guide me through the steps. i would really appreciate ur help

Regards,
Asad

Also how would it be achieved that by selecting a value from a drop down or by clicking a button on one page and the cookie will be created, the second page will be opened with the value being displayed in some text area there ?

> should the code for cookie be written in a separate cookie file or should it be written in
> one of the two files that pass and receive data.

The code for cookie management would come in both the HTML documents; in the first one for writing to the cookie and in the second one for reading from the cookie.

> what if i hand over my files to a friends who absolutely has no idea about html but he just
> uses the files to quickly check out some data. If the cookie is deleted, he will do nothing
> but curse me

The cookie creation code would take care of writing the value to the cookie every time the first page is run.

> How will the data be received in the target file ? can it be stored in some variable in a
> target file ?

If by the target file you mean the second HTML document, then yes, you would call a function when the body loads which would read the required data from the cookie written by the first HTML document.

<!-- second html file -->
<html>
<head>
  <script type="text/javascript">
  function readCookie() {
    var valueReadFromCookie = ... // read data from cookie
    var frm = document.forms["frm"];
    var txtBox = frm.elements["txt"];
    txt.value = valueReadFromCookie;
  }
  </script>
</head>
<body onload="readCookie();">
  <form id="frm" name="frm" action="#">
    <span>Value read: </span>
    <input name="txt" id="txt">
  </form>
</body>
</html>

thanx alot dear !!!
hope it will help others too who r in search of the solution for a similar problem ...
Regards,
Asad

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.