Solved

checkbox not working

Posted on 2004-10-19
5
290 Views
Last Modified: 2012-05-05
Below is my code..




<!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">
<head>
<title>Nihon Heaven</title>
<link href="scripts/stylesheet.css" rel="stylesheet" type="text/css" />

<script language="JavaScript" src="./scripts/common.js">
</script>
<script language="JavaScript" src="./scripts/validation.js">
</script>

<script language="JavaScript">
<!--
      function validate_form()
      {
            if ( check_empty(document.frmLogin.txtName.value) || check_empty(document.frmLogin.txtPassword.value) )
            {
                  alert("Please enter both login name and password");
                  return false;
            }
            else
                  return true;
      }
      
      function validate_search(objForm)
      {
            if ( check_empty(objForm.txtSearch.value) )
            {
                  alert("Please enter a keyword search");
                  return false;
            }
            else
            {
                  self.location = "search_results.asp";
                  return false;
            }
      }
      
      function validate_addcart(objForm)
      {
            var i;
            var valid=false;

            alert(objForm.chkItem.length);
                        
            for ( i=0; i<objForm.chkItem.length; i++ )
            {
                  if ( objForm.chkItem[i].checked )
                  {
                        valid = true;
                        break;
                  }
            }

            if ( valid )
                  return true;
            else
            {
                  alert("There are no item selected to be added to shopping cart");
                  return false;
            }
      }
//-->
</script>

<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta name="description" content="First Japanese 24 hours food service in Singapore" />
<meta name="keywords" content="Japanese food Japan 24 hours" />
<meta name="contributor" content="Nihon Heaven" />
<meta name="publisher" content="Nihon Heaven" />
</head>

<BODY>

<table class="fullWidth" align="center">
<tr>
<td>

<table style="width: 100%;" align="center">
<tr>
      <td valign="middle" width="15%">
            <!-- URL's used in the movie-->
            <!-- text used in the movie-->
            <!--First24HoursJapFoodServiceNihonHeaven-->
            <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
             codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
             WIDTH="100" HEIGHT="65" id="logo" ALIGN="">
             <PARAM NAME=movie VALUE="logo.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFCC> <EMBED src="logo.swf" quality=high bgcolor=#FFFFCC  WIDTH="100" HEIGHT="65" NAME="logo" ALIGN=""
             TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
            </OBJECT>
      </td>
      <td>
            <img src="images/banner.jpg" style="width:650px;height:65px;" />
      </td>
</tr>
</table>

<table class="separator" align="center">
<tr>
      <td><div class="topmenubutton">::<a href="./index.asp">HOME</a>&nbsp;&raquo;&nbsp;Alcohol</div></td>
      <td align="right"><div class="topmenubutton">::<a href="./site_map.asp" title="Site Map">SITE MAP</a>&nbsp;&nbsp;::<a href="./feedback.asp" title="Give your feedback">FEEDBACK</a></div></td>
</tr>
</table>

<table class="separator" align="center">
<tr>
      <form action="#" method="get" onSubmit="return validate_search(this);">
      <td valign="middle">
            <div class="searchmenu">
            ::Search
            <select name="selSearch" class="searchdropdown">
            <option selected="true" value="0">All Categories</option>
            <option value="1">Restaurant Menu</option>
            <option value="2">Table Manners</option>
            <option value="3">Help</option>
            </select>
            <input type="type" name="txtSearch" class="searchtextbox" />
            <input type="submit" class="searchboxlook" value="GO" />
            </div>
      </td>
      
                  <td colspan="2" align="right" valign="top"><div class="searchmenu"><font class="searchmenu">New Customer? <a href="./register.asp" title="Start here to make your shopping easier">Start here</a>&nbsp;&nbsp;<a href="#" title="View Shopping Cart"><img src="images/cart.gif" border="0" alt="View Shopping Cart" /></a>&nbsp;<a href="./view_cart.asp" title="View Shopping Cart">View Cart</a></font></div></td>
      
      </form>
</tr>
</table>

<table class="separator" align="center" style="margin-top:10px">
<tr>
      <td width="20%" valign="top">
            <div style="background-color:#FAEBD7;font-weight:bold;font-size:10pt;font-family:Verdana;width:100%;">Restaurant Menu&nbsp;&nbsp;&nbsp;</div>
      <br />
      <span class="restmenu">
&nbsp;&#149; <a href="./category.asp?cat=1">Alcohol</a><br />&nbsp;&#149; <a href="./category.asp?cat=2">Bento Set</a><br />&nbsp;&#149; <a href="./category.asp?cat=3">Donburi</a><br />&nbsp;&#149; <a href="./category.asp?cat=4">Ramen</a><br />&nbsp;&#149; <a href="./category.asp?cat=5">Salad</a><br />&nbsp;&#149; <a href="./category.asp?cat=6">Sashimi</a><br />&nbsp;&#149; <a href="./category.asp?cat=7">Side Orders</a><br />&nbsp;&#149; <a href="./category.asp?cat=8">Sushi</a><br />&nbsp;&#149; <a href="./category.asp?cat=9">Udon/Soba</a>
      </span>
      <br /><br />      <div style="background-color:#FAEBD7;font-weight:bold;font-size:10pt;font-family:Verdana;width:100%;">Table Manners&nbsp;&nbsp;&nbsp;</div>
      <br />
      <span class="restmenu">
      &nbsp;&#149; <a href="./sitting.asp">Sitting Techniques</a>
      <br />
      &nbsp;&#149; <a href="./greetings.asp">Greetings</a>
      <br />
      &nbsp;&#149; <a href="./dishes.asp">Dishes</a>
      <br />
      &nbsp;&#149; <a href="./chopsticks.asp">Use of Chopsticks</a>
      <br />
      &nbsp;&#149; <a href="./table_rules.asp">Table Rules</a>
      <br />
      &nbsp;&#149; <a href="./drinking_rules.asp">Drinking Rules</a>
      <br />
      &nbsp;&#149; <a href="./how_to_eat.asp">How to Eat</a>
      </span>
      
      <br /><br /><br /><br />
      
      </td>
      
      <!-- STARTSTSRT -->      
      <td valign="top">

      <form name="frmCart" action="./confirm_addcart.asp" method="post" onsubmit="return validate_addcart(this);">
      <table cellpadding="2" cellspacing="0" width="95%" border="0" align="center">
      <tr>
            <td colspan="3"><div align="justify" class="subhead">Alcohol</div></td>
      </tr>
      <tr>
            <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
            <td colspan="3" align="justify">
                              <table border="0" cellpadding="1" cellspacing="2" align="center" width="100%">
                              <tr>
                                    <td colspan="2"><div style="background-color:#F2F2F2;width:350px;border:1px solid;font-weight:bold;font-size:13px;color:blue">Biru</div></td>
                              </tr>
                              <tr>
                                    <td width="26%" rowspan="2">
                                          <img src="./images/1/1.gif" width="100" height="100" border="1" alt="Biru" />
                                    </td>
                                    <td valign="top">
                                          Biru (beer) is the most popular alcoholic drink in Japan. We serve Tiger's Beer.
                                          <br /><br />
                                          Restaurant Price: SS$7.90
                                          <br />
                                          <b>Online Price: SS$7.90</b>
                                    </td>
                              </tr>
                              <tr>
                                    <td valign="bottom" align="right">
                                          <span style="color:brown;font-weight:bold;">Item already in shopping cart.</span>
                                    </td>
                              </tr>
                              <tr>
                                    <td colspan="2">&nbsp;</td>
                              </tr>
                  
                              <table border="0" cellpadding="1" cellspacing="2" align="center" width="100%">
                              <tr>
                                    <td colspan="2"><div style="background-color:#F2F2F2;width:350px;border:1px solid;font-weight:bold;font-size:13px;color:blue">Happoshu</div></td>
                              </tr>
                              <tr>
                                    <td width="26%" rowspan="2">
                                          <img src="./images/1/2.gif" width="100" height="100" border="1" alt="Happoshu" />
                                    </td>
                                    <td valign="top">
                                          Happoshu, a recent invention by Japanese brewing companies, literally means "sparkling alcohol". It looks like beer, contains the same amount of alcohol as beer, but is made with less malt than beer, giving it a different, lighter taste.
                                          <br /><br />
                                          Restaurant Price: SS$5.90
                                          <br />
                                          <b>Online Price: SS$4.90</b>
                                    </td>
                              </tr>
                              <tr>
                                    <td valign="bottom" align="right">
                                          <span style="color:brown;">Add to Cart</span>&nbsp;<input type="checkbox" name="chkItem" value="2" />
                                    </td>
                              </tr>
                              <tr>
                                    <td colspan="2">&nbsp;</td>
                              </tr>
                  
                              <tr>
                                    <td colspan="2" align="right">
                                    <span style="font-size:9px;color:red;text-decoration:underline;">Add selected items to shopping cart &raquo;</span>
                                    <input type="image" src="images/add_to_cart.gif" alt="Add Selected Items to Shopping Cart" />
                                    </td>
                              </tr>
                              <tr>
                                    <td colspan="2">
                                    <span style="font-size:11px;color:blue;font-weight:bold;">
                                    <br />
                                    For more varieties, visit any of our restaurants.
                                    </span>
                                    </td>
                              </tr>
                              </table>
                              
                              <br />
                              <div align="right">
                              
                                          <a href="category.asp?cat=1&amp;page=1">1</a>
                                          
                                          <a href="category.asp?cat=1&amp;page=2">2</a>
                                          
                                    <a href="category.asp?cat=1&amp;page=2">[Next &gt;&gt;]</a>
                                    
                              
                              </div>
                              
                        </td>
            
      </tr>
      </table>
      </form>

      <br /><br /><br /><br />
      </td>
      <!-- ENDEND -->
      
      <td width="20%" valign="top">
      
                  <form name="frmLogin" action="./check_login.asp" method="post" onSubmit="return validate_form();">
                  <div style="background-color:#FFCC66;font-weight:bold;font-size:10pt;font-family:Verdana;width:100%;">Welcome!&nbsp;&nbsp;&nbsp;</div>
                  <br />
                  <div align="right">
                                    
                  Login: <input type="text" name="txtName" class="logintextbox" maxlength="10">
                  <br />
                  Password: <input type="password" name="txtPassword" class="logintextbox" maxlength="8">
                  <br />
                  <input type="checkbox" name="chkRemember" style="width:13px;height:18px;margin-right:3px;" />Remember Me
                  <input type="submit" class="searchboxlook" value="Login" />
                  <a href="forget_password.asp"><span style="font-size:12px;font-family:Comic Sans Ms;">Forget Your Password?</span></a>
                  </div>
                  </form>
      
      <div style="background-color:#FAEBD7;font-weight:bold;font-size:10pt;font-family:Verdana;width:100%;">Best Seller&nbsp;&nbsp;&nbsp;</div>      
      <br />
      <span class="restmenu">
      <ol type="1">
            <li>Chawanmushi</li>
            <li>Chef's Choice Bento</li>
            <li>Sushi Roll</li>
            <li>Katsudon</li>
            <li>Yakisoba</li>
      </ol>
      </span>
      <div style="background-color:#FAEBD7;font-weight:bold;font-size:10pt;font-family:Verdana;width:100%;">Best Price&nbsp;&nbsp;&nbsp;</div>
      <br />
      <span class="restmenu">
      <ol type="1">
            <li>Chawanmushi</li>
            <li>Amaguri</li>
            <li>Sushi Combo</li>
            <li>Salmon & Teriyaki Bento</li>
            <li>Katsudon</li>
      </ol>
      </span>
      <br /><br /><br /><br />
      </td>
</tr>
</table>
</td>
</tr>

<tr>
<td>
<div align="center" class="footer">
<a href="./privacy_policy.asp">Privacy Policy</a> | <a href="./rest_policy.asp">Restaurant Policy</a> | <a href="./terms_conditions.asp">Terms and Conditions</a> | <a href="faq.asp">FAQ</a> | <a href="./business_hours.asp">Business Hours</a> | <a href="./locations.asp">Locations</a> | <a href="./site_map.asp">Site Map</a> | <a href="./about.asp">About Us</a> | <a href="./contact.asp">Contact Us</a>
<br />
This E-Commerce site is intended solely for my academic purposes only.
</div>
</td>
</tr>

</table>

</body>

</html>



When I click on "Add Selected Items to Shopping Cart" image, how come the javascript function does not give me the correct checkbox length?

0
Comment
Question by:ansi_c
  • 2
  • 2
5 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 50 total points
Comment Utility
try this


      function validate_addcart(objForm)
      {
            var i;
            var valid=false;

            if ( objForm.chkItem[0] )
            {
                  for ( i=0; i<objForm.chkItem.length; i++ )
                  {
                        if ( objForm.chkItem[i].checked )
                        {
                              valid = true;
                              break;
                        }
                  }
            }
            else
            {
                  if ( objForm.chkItem.checked )
                        valid = true;
                  else
                        valid = false;
            }

            if ( valid )
                  return true;
            else
            {
                  alert("There are no item selected to be added to shopping cart");
                  return false;
            }
      }



hongjun
0
 

Expert Comment

by:sreekanth_k77
Comment Utility
Hi anci,

To use document.frmCart.names.length property you should have more than one item with the same name or id. In your case if you have atleast one more checkbox with name 'names' then your code will work perfectly. This is the main proble you are facing in this situation. So you have to use document.frmCart.names.checked property to solve your problem.

Thanks and regards,
Srikanth
0
 

Author Comment

by:ansi_c
Comment Utility
thanks for the explanation srikanth
0
 

Expert Comment

by:sreekanth_k77
Comment Utility
After looking into the hongjun's code I think if single checkbox present in the form then the code may not work properly.
Thanks
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
Hi,

I tried with 1 checkbox and it works.
Tried with many checkboxes and it work too.

hongjun
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now