Reputation: 13
I'm very new to scripting and programming and I need some help at this moment.
I have a form with some dropdown boxes for selecting several options. I've written a javascript that calculates a total price based on the selected options from the dropdown boxes. The total value is returned correctly, but i'm having a hard time posting this value with the form. I've tried a hidden input type, but that does not work either. The post goes to a php file, that in the end must insert the value in de database.
HTML
<form id="vmPrice" class="form-horizontal" method="post"
action="create_xml.php">
<div class="control-group">
<label class="control-label" for="formattedTotalPrice">Costs:</label>
<div class="controls">
<input type="hidden" name="postPrice" id="postPrice" value="">
€ <span id="formattedTotalPrice" name="formattedTotalPrice" >...</span>
/month <br/>
<span style="font-size: 10px;"> By clicking on Create VM a subscription with
the above costs will be automaticly added to your account </span>
</form>
JavaScript
function calculateTotal()
{
var unformattedTotalPrice = getVcpuPrice() + getOsPrice() + getHddPrice() +
getMemoryPrice() + getHAPrice();
var formattedTotalPrice = unformattedTotalPrice;
document.getElementById('formattedTotalPrice').innerHTML =
formattedTotalPrice;
document.getElementById('postPrice').innerHTML = formattedTotalPrice;
}
The formattedTotalPrice is displayed correctly on the screen with use of the span id. Now I would like to post this with the form, but is does not work.
PHP code (create_xml.php)
$price = (int)$_POST['formattedTotalPrice'];
// or
$price = (int)$_POST['postPrice'];
print $price;
The printed value at this moment is 0.
Upvotes: 1
Views: 59
Reputation: 1642
You're setting the innerHTML
attribute. Use value
instead to change the value of an input element.
document.getElementById('postPrice').value = formattedTotalPrice;
While innerHTML
can be used to change the page's HTML content, it's the value
of HTML input
elements that gets sent to PHP from the form.
Upvotes: 2