Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can anyone solve this problem?

    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

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by mathan123 View Post
    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.”

    Quote Originally Posted by mathan123 View Post
    <html>
    Please use [code][/code] tags in your posts when appropriate.

    Quote Originally Posted by mathan123 View Post
    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:

    Code:
    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.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by mathan123 View Post
    is it possible to use if else statement instead of using switch case.
    Yes:

    Code:
    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";
    	}
    }
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #6
    Regular Coder lokeshshettyk's Avatar
    Join Date
    Aug 2008
    Location
    On the way to the moon!
    Posts
    157
    Thanks
    5
    Thanked 20 Times in 20 Posts

    I suppose it's fixed

    <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

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok....thanks a lot for ur help...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •