...

View Full Version : can anyone solve this problem?



mathan123
08-22-2008, 05:15 AM
hi there...im newbie here...having some problem in javascript and html

and the switch case it's not working....can someone help me to solve this problem...

<html>

<script type="text/javascript">
function setPrice(){

switch(document.memberForm.memberType.value){
case 0:
document.memberForm.price.value="0.00";
break;
case 1:
document.memberForm.price.value="100.00";
break;
case 2:
document.memberForm.price.value="2500.00";
break;
case 3:
document.memberForm.price.value="5000.00";
break;
case 4:
document.memberForm.price.value="10000.00";
break;
case 5:
document.memberForm.price.value="20000.00";
break;
default:
document.memberForm.price.value="0.00";


}
}
</script>


<form name="memberForm" method="post" action="insert.php">

<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="firstName" size="25"></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastName" size="25"></td>
</tr>
<tr>
<td>Member Type: </td>
<td>
<select name="memberType" onChange="setPrice();">
<option value=0 selected>--Please Select--</option>
<option value=1>Normal Member</option>
<option value=2>Torch Bennet (Life Membership)</option>
<option value=3>Horse Thomson (Life Membership)</option>
<option value=4>Cecil Lee (Life Membership)</option>
<option value=5>'G' Gispert (Life Membership) </option>
</select>

</td>
</tr>

<tr>
<td>Price: USD</td>
<td><input type="text" name="price" size="25"></td>
</tr>
<td>
<br>
<br>
<br>
<tr>
<td>Id:</td>
<td><input type="text" name="id" size="25"></td>
</tr>
<tr>
<td>Full Name:</td>
<td><input type="text" name="full_Name" size="25"></td>
<td>Date Of Birth:</td>
<td><input type="text" name="dob" size="25"></td>
</tr>
</td>

<tr>
<td>Identity Card/Passport No:</td>
<td><input type="text" name="identity_Card" size="25"></td>
</tr>
<tr>
<td>Hash Chapter:</td>
<td><input type="text" name="hash_Chapter" size="25"></td>
<td>Hash Name:</td>
<td><input type="text" name="hash_Name" size="25"></td>
</tr>
<tr>
<td>Mailing Adddress</td>
<td><input type="text" name="mailing_Address" size="25"></td>
<tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><input type="text" size="25"></td>
</tr>
<tr>
<td>Postal Code:</td>
<td><input type="text" name="postal_Code" size="25"></td>
<td>Telephone No:</td>
<td><input type="text" name="telephone_No" size="25"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" size="25"></td>
</tr>


</table>
<input type="submit" value="submit">
</form>





</html>



ur help greatly appreciated
warmest regards,
Mathan

Arbitrator
08-22-2008, 05:52 AM
and the switch case it's not working....can someone help me to solve this problem...It helps when you say what the problem is; for example: “When a memberType form option is selected, the price text field is always populated with the value 0.00 when it should be populated with a value based upon the option selected.”


<html>Please use
tags in your posts when appropriate.


function setPrice(){

switch(document.memberForm.memberType.value){
case 0:
document.memberForm.price.value="0.00";
break;
case 1:
document.memberForm.price.value="100.00";
break;
case 2:
document.memberForm.price.value="2500.00";
break;
case 3:
document.memberForm.price.value="5000.00";
break;
case 4:
document.memberForm.price.value="10000.00";
break;
case 5:
document.memberForm.price.value="20000.00";
break;
default:
document.memberForm.price.value="0.00";


}
}The problem seems to stem from the fact that you’re comparing against integers instead of strings; the value property returns a string even when its value is a number. So, you need to compare against strings (e.g., "0") instead of integers (e.g., 0). I’ve provided corrected code with some adjustments reflecting better coding practice:


var d = document;
var price = d.getElementsByName("price").item(0);
function setPrice() {
switch (d.getElementsByName("memberType").item(0).value) {
case "1":
price.value = "100.00";
break;
case "2":
price.value = "2500.00";
break;
case "3":
price.value = "5000.00";
break;
case "4":
price.value = "10000.00";
break;
case "5":
price.value = "20000.00";
break;
default:
price.value = "0.00";
}
}

Additionally, your HTML contains errors and is, otherwise, poorly written, but I won’t go there. If interested, I’d check out http://validator.w3.org/ as a step toward fixing them.

mathan123
08-22-2008, 07:14 AM
to be honest with u, im just a beginner in this programming field. Previously i work with joomla. is it possible to use if else statement instead of using switch case.

Another thing is price USD: should display when i choose the member type.
Hope u will guide me...


regards,
Mathan

Arbitrator
08-23-2008, 08:16 AM
is it possible to use if else statement instead of using switch case.Yes:


var d = document;
var membership_type = d.getElementsByName("memberType").item(0);
var price = d.getElementsByName("price").item(0);
function setPrice() {
if (membership_type.value === "1") {
price.value = "100.00";
}
else if (membership_type.value === "2") {
price.value = "2500.00";
}
else if (membership_type.value === "3") {
price.value = "5000.00";
}
else if (membership_type.value === "4") {
price.value = "10000.00";
}
else if (membership_type.value === "5") {
price.value = "20000.00";
}
else {
price.value = "0.00";
}
}

mathan123
08-25-2008, 03:37 AM
still having problem...suppose the price USD should be change according to the membership type i choose.....hope someone will help me...
Thank you

<html>

<script type="text/javascript">

var d = document;
var membership_type = d.getElementsByName("memberType").item(0);
var price = d.getElementsByName("price").item(0);
function setPrice() {
if (membership_type.value === "1") {
price.value = "100.00";
}
else if (membership_type.value === "2") {
price.value = "2500.00";
}
else if (membership_type.value === "3") {
price.value = "5000.00";
}
else if (membership_type.value === "4") {
price.value = "10000.00";
}
else if (membership_type.value === "5") {
price.value = "20000.00";
}
else {
price.value = "0.00";
}
}
</script>


<form name="memberForm" method="post" action="insert.php">

<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="firstName" size="25"></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastName" size="25"></td>
</tr>
<tr>
<td>Member Type: </td>
<td>
<select name="memberType" onChange="setPrice();">
<option value=0>--Please Select--</option>
<option value=1>Normal Member</option>
<option value=2>Torch Bennet (Life Membership)</option>
<option value=3>Horse Thomson (Life Membership)</option>
<option value=4>Cecil Lee (Life Membership)</option>
<option value=5>'G' Gispert (Life Membership) </option>
</select>

</td>
</tr>

<tr>
<td>Price: USD</td>
<td><input type="text" name="price" size="25"></td>
</tr>
<td>
<br>
<br>
<br>
<tr>
<td>Id:</td>
<td><input type="text" name="id" size="25"></td>
</tr>
<tr>
<td>Full Name:</td>
<td><input type="text" name="full_Name" size="25"></td>
<td>Date Of Birth:</td>
<td><input type="text" name="dob" size="25"></td>
</tr>
</td>

<tr>
<td>Identity Card/Passport No:</td>
<td><input type="text" name="identity_Card" size="25"></td>
</tr>
<tr>
<td>Hash Chapter:</td>
<td><input type="text" name="hash_Chapter" size="25"></td>
<td>Hash Name:</td>
<td><input type="text" name="hash_Name" size="25"></td>
</tr>
<tr>
<td>Mailing Adddress</td>
<td><input type="text" name="mailing_Address" size="25"></td>
<tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><input type="text" size="25"></td>
</tr>
<tr>
<td>Postal Code:</td>
<td><input type="text" name="postal_Code" size="25"></td>
<td>Telephone No:</td>
<td><input type="text" name="telephone_No" size="25"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" size="25"></td>
</tr>


</table>
<input type="submit" value="submit">
</form>





</html>

lokeshshettyk
08-25-2008, 01:13 PM
<html>
<script language="javascript" type="text/javascript">
var mtype='';
var price='';
function setPrice() {
price = document.getElementById("price");
mtype = document.getElementById('memberType');
if (mtype.value == "1") {
price.value = "100.00";
}
else if (mtype.value == "2") {
price.value = "2500.00";
}
else if (mtype.value == "3") {
price.value = "5000.00";
}
else if (mtype.value == "4") {
price.value = "10000.00";
}
else if (mtype.value == "5") {
price.value = "20000.00";
}
else {
price.value = "0.00";
}
}
</script>

<form name="memberForm" method="post" action="insert.php">
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="firstName" size="25"></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastName" size="25"></td>
</tr>
<tr>
<td>Member Type: </td>
<td>
<select id="memberType" name="memberType" onChange="setPrice();">
<option value="0">--Please Select--</option>
<option value="1">Normal Member</option>
<option value="2">Torch Bennet (Life Membership)</option>
<option value="3">Horse Thomson (Life Membership)</option>
<option value="4">Cecil Lee (Life Membership)</option>
<option value="5">'G' Gispert (Life Membership) </option>
</select>

</td>
</tr>

<tr>
<td>Price: USD</td>
<td><input id="price" type="text" name="price" size="25"></td>
</tr>
<td>
<br>
<br>
<br>
<tr>
<td>Id:</td>
<td><input type="text" name="id" size="25"></td>
</tr>
<tr>
<td>Full Name:</td>
<td><input type="text" name="full_Name" size="25"></td>
<td>Date Of Birth:</td>
<td><input type="text" name="dob" size="25"></td>
</tr>
</td>

<tr>
<td>Identity Card/Passport No:</td>
<td><input type="text" name="identity_Card" size="25"></td>
</tr>
<tr>
<td>Hash Chapter:</td>
<td><input type="text" name="hash_Chapter" size="25"></td>
<td>Hash Name:</td>
<td><input type="text" name="hash_Name" size="25"></td>
</tr>
<tr>
<td>Mailing Adddress</td>
<td><input type="text" name="mailing_Address" size="25"></td>
<tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><input type="text" size="25"></td>
</tr>
<tr>
<td>Postal Code:</td>
<td><input type="text" name="postal_Code" size="25"></td>
<td>Telephone No:</td>
<td><input type="text" name="telephone_No" size="25"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" size="25"></td>
</tr>
</table>
<input type="submit" value="submit">
</form>
</html>

Hope this helps:)
Compare your code with this and trace your mistakes, that way you improve your knowledge:)

mathan123
08-26-2008, 03:52 AM
ok....thanks a lot for ur help...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum