hello forums.
i have small problem im creating a simple page (no php ) just javascript and html.

i have order.html which has a list of items.
and then when you click the drop down menu it will automatically display the price of the item depending on the quantity on a textbox.

now i have form.htm
i have the values name,address,ammount.

now how can i pass the value of the textbox with the price to the form.htm so i can subtract them?
any help? thanks!

Dani AI

Generated

A quick, practical summary that picks up from ’s question and the suggestions already posted by and : for a pure client-side flow the two simplest, reliable options are browser storage (sessionStorage/localStorage) or passing a value on the URL and reading it on the next page. sessionStorage is usually the best fit for a price calculated on the order page because it lives only in the current tab and does not touch the server. URL parameters work fine too and are visible in the address bar (useful for bookmarking or debugging).

Example using sessionStorage (set on order.html, read on form.htm):

// on order.html after you compute price (use cents to avoid float errors)
var cents = Math.round(price * 100);
sessionStorage.setItem('orderPriceCents', String(cents));
location.href = 'form.htm';
// on form.htm
var cents = parseInt(sessionStorage.getItem('orderPriceCents'), 10) || 0;
var price = cents / 100;
var amount = parseFloat(document.getElementById('amount').value) || 0;
var remaining = amount - price;

If you prefer a query string (the approach mentioned), use the modern URLSearchParams API instead of manual splitting:

// send: order.html
location.href = 'form.htm?price=' + encodeURIComponent(price);

// read: form.htm
var params = new URLSearchParams(window.location.search);
var price = parseFloat(params.get('price')) || 0;

Practical tips: always validate and coerce values (check for NaN), store money as integer cents to avoid floating-point rounding, and ensure both pages share the same origin (storage is origin-scoped). sessionStorage/localStorage are not sent to the server and are easiest for client-only logic; cookies are another route but are less convenient for purely client-side transfers. Never trust client-side numbers for final billing—recalculate or verify on the server for any real payments.

Recommended Answers

All 4 Replies

There are many ways of doing this.Once you submit a form with GET method then use one of these methods:-

  • Split the URL by searching for "?" and then take 2nd element of array and then split it by "&".Now you have query parameters.Now split the resultant data by "=" and decodeURIComponent.

    var queryName = decodeURIComponent(queryItem[0]);
    var queryValue = decodeURIComponent(queryItem[1]);

  • Use "location.search" to get query parameters.then store data in JSON format.

    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);
    
    var urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
    

This could easily be improved upon to handle array-style query strings too. An example of this is , but since array-style parameters aren't defined in RFC 3986 I won't pollute this answer with the source code.

Hello thanks for the reply, i dont know how i can integrate that code to my file.

heres the source code for the order ->
and heres the source code for the form - >
heres my JS ->

Have you thought about just storing the values in a cookie? As long as they don't have cookies disabled on the browser it would work fine.

Have a look at this http://www.w3schools.com/js/js_cookies.asp

Although another alternative but have you any reason why you can't parse query parameters????

moreover cookies have following disadvantages

1)Cookies can be disabled on user browsers
2)Cookies are transmitted for each HTTP request/response causing overhead on bandwidth
3)No security for sensitive data(as in some broswers they are not encrypted and stored)

Cookie Limitations:
1)Most browsers support cookies of up to 4096 bytes(4kbytes)
2)Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded.
3)Browser supports 300 cookies towards different websites.
4)Complex type of data not allowed(eg: dataset), allows only plain text (ie, cookie allows only string content)
5)Cookies are browser specific (ie, one browser type[IE] stored cookies will not be used by another browser type[firefox]).

Refer http://books.google.co.in/books?id=NLc_TQiWvo4C&pg=PA546&lpg=PA546&dq=cookie+disadvantages&source=bl&ots=TO5Su3IztL&sig=drx5wX6CW576jGRPH8CJlBM7TJ4&hl=en&sa=X&ei=tjk6Uc_UGMSOrgewm4DgDw&sqi=2&ved=0CGgQ6AEwBw#v=onepage&q=cookie%20disadvantages&f=false

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.