I’m not sure if this is possible since javascript is client side and php is server side, but what I have is a series of javascript functions that give a real time total to orders in a form. To clarify that, as the user selects items in the form it gives a total. On submission of the form php is submitting the order to the database. What I need to achieve is a way to submit the total (created by javascript) of the order to the database (via php obv). The javascript function that creates the total is:
function calculateTotal(){
var Price = getDropPrice() + getverifyPrice() + getmiscPrice();
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "$"+Price;
}
The html where this is produced is pretty simple:
<div id="totalPrice"></div>
If you need me to post the other functions(getDropPrice, getverifyPrice, getmiscPrice) let me know, but basically drop is a drop down, verify and misc are radio buttons, I’m just adding their totals together to get the order total in the function above. My searches on SO and google have only shown me how to get php variables into javascript not the other way around so I certainly hope this can be done. Thanks!
Put your total in hidden input field. Then your PHP script will be able to read after the form is submited and do whatever you want.
and then you get it as usual form
$_POST['hiddenInput']; If you got more data to send back that way, you may either add more inputs, or stick to one but pack your all data into JSON and send that way. Once received, you dojson_decode()on that field and you got it all back, no matter how many data was sent.I, however would NOT recommend relying on user side provided data and rather send back which products are selected and calculate totals again, otherwise you may find yourself selling expensive goods for $1 one day 🙂