#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Alert messages: Help?


    Hi there,

    I am trying to make alert messages for when neither a flight nor seating preference has been made, and it's a struggle. Any help would be amazing.


    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=utf-8">
    <title>gotham airlines</title>
    <script>
    
    function calculateCost() {
    
    for (var i = 1; i < 7; i++) {
    var radiobutton = document.getElementById("flight" + i);
    if (radiobutton.checked == true) {
    var base=radiobutton.value;
    }
    }
    var rtn=document.getElementById("returnfare").checked?2:1;
    for (var a = 1; a < 4; a++) {
    var radiobutton = document.getElementById("seating" + a);
    if (radiobutton.checked == true) {
    var seatclass=radiobutton.value;
    }
    }
    var cost=base*rtn*seatclass;
    alert("The selected flight will cost $" + cost);
    }
    
    </script>
    </head>
    <body>
    <h1>Gotham Airlines Fare Calculator</h1>
    <form>
    <p>Complete the form below to calculate the cost of your flight.</p>
    Route:<br>
    <p><input type="radio" id="flight1" name="flights"
    value="230"> <label for="flight1">Gotham - Hill Valley</label><br>
    <input type="radio" id="flight2" name="flights"
    value="250"> <label for="flight2">Gotham - Las Venturas</label><br>
    <input type="radio" id="flight3" name="flights"
    value="190"> <label for="flight3">Gotham - Storybrooke</label><br>
    <input type="radio" id="flight4" name="flights"
    value="160"> <label for="flight4">Gotham - Marlinshire</label><br>
    <input type="radio" id="flight5" name="flights"
    value="270"> <label for="flight5">Gotham - Lillian</label><br>
    <input type="radio" id="flight6" name="flights"
    value="220"> <label for="flight6">Gotham - Seahaven</label></p>
    
    <p><input type="checkbox" id="returnfare" name="return fare"
    value="2"> <label for="returnfare">Click here if you will be purchasing a return fare</label></p>
    
    Seating:<br>
    <p><input type="radio" id="seating1" name="seating"
    value="2"> <label for="seating1">First class</label><br>
    <input type="radio" id="seating2" name="seating"
    value="1.5"> <label for="seating2">Business class</label><br>
    <input type="radio" id="seating3" name="seating"
    value="1"> <label for="seating3">Economy class</label></p>
    <p><input type="submit" value="Calculate"
    onClick="calculateCost();"> <input type="reset"></p>
    </form>
    </body>
    </html>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
    <title>gotham airlines</title>
    <script>
    
    function calculateCost() {
    
    var route = "unsel";
    var seats = "unsel";
    
    for (var i = 1; i < 7; i++) {
    var radiobutton = document.getElementById("flight" + i);
    if (radiobutton.checked == true) {
    var base=radiobutton.value;
    route="sel";
    }
    }
    var rtn=document.getElementById("returnfare").checked?2:1;
    for (var a = 1; a < 4; a++) {
    var radiobutton = document.getElementById("seating" + a);
    if (radiobutton.checked == true) {
    var seatclass = radiobutton.value;
    seats="sel";
    }
    }
    var cost=base*rtn*seatclass;
     if (route != "sel") {
     alert("Please Select Your Route");
     }
     else if (seats != "sel") {
     alert("Please Select Your Seating");
     }
     else if (route != "unsel" && seats != "unsel") {
     alert("The selected flight will cost $" + cost);
     document.getElementById("flyWithUs").onsubmit = function() {
       return true;
      }
     }
    }
    
    </script>
    </head>
    <body>
    <h1>Gotham Airlines Fare Calculator</h1>
    <form id="flyWithUs" onsubmit="return false">
    <p>Complete the form below to calculate the cost of your flight.</p>
    Route:<br>
    <p><input type="radio" id="flight1" name="flights"
    value="230"> <label for="flight1">Gotham - Hill Valley</label><br>
    <input type="radio" id="flight2" name="flights"
    value="250"> <label for="flight2">Gotham - Las Venturas</label><br>
    <input type="radio" id="flight3" name="flights"
    value="190"> <label for="flight3">Gotham - Storybrooke</label><br>
    <input type="radio" id="flight4" name="flights"
    value="160"> <label for="flight4">Gotham - Marlinshire</label><br>
    <input type="radio" id="flight5" name="flights"
    value="270"> <label for="flight5">Gotham - Lillian</label><br>
    <input type="radio" id="flight6" name="flights"
    value="220"> <label for="flight6">Gotham - Seahaven</label></p>
    
    <p><input type="checkbox" id="returnfare" name="return fare"
    value="2"> <label for="returnfare">Click here if you will be purchasing a return fare</label></p>
    
    Seating:<br>
    <p><input type="radio" id="seating1" name="seating"
    value="2"> <label for="seating1">First class</label><br>
    <input type="radio" id="seating2" name="seating"
    value="1.5"> <label for="seating2">Business class</label><br>
    <input type="radio" id="seating3" name="seating"
    value="1"> <label for="seating3">Economy class</label></p>
    <p><input type="submit" value="Calculate"
    onClick="calculateCost()"> <input type="reset"></p>
    </form>
    </body>
    </html>

IMN logo majestic logo threadwatch logo seochat tools logo