PDA

View Full Version : showCost() Not Working



keelerz
05-03-2011, 10:43 PM
Hey guys! My friend has written this javascript code of displaying the costs but it does not seem to work when a button is clicked!


<script type = "text/javascript">
function showCost()
{
var cost = 0;
var membership = document.getElementById("member").value;

if (membership == "full")
{cost = 199.50;}

else if (membership == "student")
{cost = 82.25;}

else if (membership == "no")
{cost = 235;}


var startdate = document.forms["registerform"]["reg_start_date"].value;
var enddate = document.forms["registerform"]["reg_end_date"].value;


if(startdate == "2011-09-05" && enddate == "2011-09-06")
{cost = cost * 2;}

else if(startdate == "2011-09-06" && enddate == "2011-09-07")
{cost = cost * 2;}


else if(startdate == "2011-09-05" && enddate == "2011-09-07")
{cost = cost * 3; }



var dinner = document.getElementById("conf_dinner").value;
if (dinner == "Y")
{cost += 54.40;}



var payment = document.getElementById("pay_method").value;
if (payment == "credit")
{cost = cost * 1.0161;}


document.getElementById("pay_total").value = cost.toFixed(2);

}


</script>



<form name ="registerform" id="registerform" action="confirmationpage.php" method="post" onsubmit="return validateForm(this);">

<p>
Title <select id = "title" name="title">
<option value="0" selected="selected">Please select</option>
<option value="Prof">Prof</option>

<option value="Dr">Dr</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select></p>

<p> First name <input type="text" name="first_name" size="20" maxlength="50"> </p>

<p> Last name <input type="text" name="last_name" size="20" maxlength="50"></p>

<p> Billing address details <input type="text" name="address" size="20" maxlength="255"> </p>

<p> Postcode <input type="text" name="postcode" size="20" maxlength="8" onchange="validatePostcode();"> </p>

<p> Email Address<input type="text" name="email" size="20" maxlength="75"></p>

<p> Membership status <input type="radio" name="member" value="no" id="no"/> <label for="no">No</label>

<input type="radio" name="member" value="student" id="student" /> <label for="student">Student</label>

<input type="radio" name="member" value="full" id="full"/> <label for="full">Full</label></p>

<p> First Day to Attend:

<select name="reg_start_date">

<option value="0" selected="selected">Please select</option>
<option value="2011-09-05">2011/09/05</option>
<option value="2011-09-06">2011/09/06</option>
<option value="2011-09-07">2011/09/07</option>
</select></p>


<p> Last Day to Attend:

<select name="reg_end_date" onChange="checkDates();">

<option value="0" selected="selected">Please select</option>
<option value="2011-09-05">2011/09/05</option>
<option value="2011-09-06">2011/09/06</option>
<option value="2011-09-07">2011/09/07</option>
</select></p>

<p> Have you registered for conference dinner ?
<input type="radio" name="conf_dinner" value="Y" id="Y" /> <label for="Y">Yes</label>
<input type="radio" name="conf_dinner" value="N" id="N" /> <label for="N">No</label></p>

<p> Are you a Vegeterian?
<input type="radio" name="vegetarian" value="Y" id="Y" /> <label for="Y">Yes</label>
<input type="radio" name="vegetarian" value="N" id="N" /> <label for="N">No</label></p>


<p> Special dietary or physical requirement: <br>
<textarea name="special_req" rows="4" cols="20"></textarea></br></p>

<p> Payment Method: <select id ="pay_method" name="pay_method">
<option value="0" selected="selected">Please select</option>
<option value="cheque">Cheque</option>
<option value="credit">Credit Card</option>

<option value="debit">Debit Card</option> </select></p>

<p><input type="button" id="totalcost" value="Total Cost" onClick="showCost( );">
&pound; <input name="pay_total" type="text" id="pay_total" size="20" readonly="readonly" />




Thank you for your help!!!!

vwphillips
05-04-2011, 03:55 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type = "text/javascript">
function showCost(frm){
var cost = 0,membership = frm["member"],startdate = frm["reg_start_date"].value,enddate = frm["reg_end_date"].value;

for (var z0=0;z0<membership.length;z0++){
if (membership[z0].checked){
cost+=membership[z0].value*1;
}
}

if (startdate == "2011-09-05" && enddate == "2011-09-06"){
cost = cost * 2;
}
else if (startdate == "2011-09-06" && enddate == "2011-09-07"){
cost = cost * 2;
}
else if (startdate == "2011-09-05" && enddate == "2011-09-07"){
cost = cost * 3;
}

if (frm["conf_dinner"][0].checked){
cost += 54.40;
}

if (frm["pay_method"].value == "credit"){
cost = cost * 1.0161;
}

document.getElementById("pay_total").value = cost.toFixed(2);

}

function checkDates(){

}
</script></head>

<body>

<form name ="registerform" id="registerform" action="confirmationpage.php" method="post" onsubmit="return validateForm(this);">

<p>
Title <select id = "title" name="title">
<option value="0" selected="selected">Please select</option>
<option value="Prof">Prof</option>

<option value="Dr">Dr</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
</select></p>

<p> First name <input type="text" name="first_name" size="20" maxlength="50"> </p>

<p> Last name <input type="text" name="last_name" size="20" maxlength="50"></p>

<p> Billing address details <input type="text" name="address" size="20" maxlength="255"> </p>

<p> Postcode <input type="text" name="postcode" size="20" maxlength="8" onchange="validatePostcode();"> </p>

<p> Email Address<input type="text" name="email" size="20" maxlength="75"></p>

<p> Membership status <input type="radio" name="member" value="235" id="no"/> <label for="no">No</label>

<input type="radio" name="member" value="82.25" id="student" /> <label for="student">Student</label>

<input type="radio" name="member" value="199.50" id="full"/> <label for="full">Full</label></p>

<p> First Day to Attend:

<select name="reg_start_date">

<option value="0" selected="selected">Please select</option>
<option value="2011-09-05">2011/09/05</option>
<option value="2011-09-06">2011/09/06</option>
<option value="2011-09-07">2011/09/07</option>
</select></p>


<p> Last Day to Attend:

<select name="reg_end_date" onChange="checkDates();">

<option value="0" selected="selected">Please select</option>
<option value="2011-09-05">2011/09/05</option>
<option value="2011-09-06">2011/09/06</option>
<option value="2011-09-07">2011/09/07</option>
</select></p>

<p> Have you registered for conference dinner ?
<input type="radio" name="conf_dinner" value="Y" id="Y" /> <label for="Y">Yes</label>
<input type="radio" name="conf_dinner" value="N" id="N" /> <label for="N">No</label></p>

<p> Are you a Vegeterian?
<input type="radio" name="vegetarian" value="Y" id="Y" /> <label for="Y">Yes</label>
<input type="radio" name="vegetarian" value="N" id="N" /> <label for="N">No</label></p>


<p> Special dietary or physical requirement: <br>
<textarea name="special_req" rows="4" cols="20"></textarea></br></p>

<p> Payment Method: <select id ="pay_method" name="pay_method">
<option value="0" selected="selected">Please select</option>
<option value="cheque">Cheque</option>
<option value="credit">Credit Card</option>

<option value="debit">Debit Card</option> </select></p>

<p><input type="button" id="totalcost" value="Total Cost" onClick="showCost(this.form);">
&pound; <input name="pay_total" type="text" id="pay_total" size="20" readonly="readonly" />

</form>

</body>

</html>