Web Analytics Made Easy -
StatCounter Javascript Calculator from PHP Inputs - CodingForum

Announcement

Collapse
No announcement yet.

Javascript Calculator from PHP Inputs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript Calculator from PHP Inputs

    Hi guys!

    I'm making a small online booking system which i would like to release to the public, As a learning experience

    I have done most of it in PHP but i would like to make a small Javascript Calculator, Basically on a .php page i have the following values

    Code:
    <select id = "roomtype" name = "roomtype">
          <option value = "200">Double</option>
          <option value = "100">Single</option>
    </select>
    Code:
    <select id = "nightsstaying" name = "nightsstaying">
          <option value = "1">1</option>
          <option value = "2">2</option>
    And then a calculate button, Which would then take the roomtype * nightsstaying and then using the alert() function to popup how much it will cost

    I have a rough idea on how to structure it but im not sure how i could retrieve it from the inputs

    Ill show you how i think it would work

    Code:
    function calculatePrices(){
                    var total = 0;
    		var nightsstaying = ??;
    		var roomtype = ??;
    		if (roomtype == "Double") {
    			total = nightsstaying * 200
    		} else if (roomtype == Single ) {
    			total = nightsstaying * 100)
    		}
    		alert(total);
    		total='0';
    
    	}
    I program in PHP/Java i havent used too much JS so sorry if it looks alot like Java and nothing like Javascript

    Thanks in advance!

  • #2
    Try this:-

    Code:
    <select id = "roomtype" name = "roomtype">
    <option value = "0">Select a room type</option>
    <option value = "200">Double</option>
    <option value = "100">Single</option>
    </select>
    <br><br>
    <select id = "nightsstaying" name = "nightsstaying">
    <option value = "0">For how many nights?</option>
    <option value = "1">1</option>
    <option value = "2">2</option>
    </select>
    <br><br>
    <input type = "button" value = "Calculate Price" onclick = "calcPrice()">
    
    <script type = "text/javascript">
    
    function calcPrice() {
    var rt = document.getElementById("roomtype").value;
    var ns = document.getElementById("nightsstaying").value;
    if ((rt == 0) || (ns == 0)) {
    alert ("Please select Room Type and the Number of Nights");
    return false;
    }
    var totalPrice = ns * rt;
    alert ("The total price is $" + totalPrice);
    }
    
    </script>

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment

    Working...
    X