Passing variable from external function to php page!

I have a function in an external javascript file (calculate.js) which calculates the price of a product, based on a users preferences in a form (dropdowns, radio buttons etc). Each time the user modifies their choice, the script/function then displays the price on the page (index.html) using:

document.getElementById(“price”).innerHTML = price;

Once the user has finished making their choices, they then click the forms submit button (method=“post”) to be taken to a confirmation page (confirm.php).

Here, I can display the users preferences using php code like:

<?php echo $_POST[‘size’] ?>
<?php echo $_POST[‘color’] ?>
etc, etc…

But I’m not sure how to pass the price over!!!

The preferences come from the form and are picked up with php.
But the price is calculated with javascript! How do I send it to confirm.php?

I have tried adding

window.location = “confirm.php?Quote=” + price;

to the function, but this changes the page before the user may want to go to the confirmation page!

Can I add a hidden input to index.html with the value set as the javascript variable called price? If so, what syntax do I use for this? I have tried

document.write(‘<input type=“hidden” name=“quote” value=“‘price’” ?>" />’)

and some other stuff too, but I still can’t get it to work!

Or do I pass the value in the url, like confirm.php?Quote=‘price’?
Again, what is the syntax, as I can’t seem to get it to work?

Any help would be great. Thanks.

MetaName

<input type=“hidden” name=“quote” id=“quote”>

Then you can do
document.getElementById(“quote”).value = price;

thank you for the tip - had a similar problem earlier, but the dropdown content comes dynamically from the database backend (AJAX).

You can create the hidden input element dynamically and append it into the form. See createElement() and appendChild(). But, before you go doing that, are you aware of how easy it is for a user to change the price if you do this via javascript? The client shouldn’t be in control of such things, so make sure that there’s no reliance upon the client. It’s ok to do this for display purposes.

Oh yes please, can I have a go at bypassing the javascript and sending a negative price to the web site?