www.webdeveloper.com
Results 1 to 4 of 4

Thread: Code to check quantity for button, but it will not check for enter key

  1. #1
    Join Date
    Feb 2008
    Posts
    5

    Code to check quantity for button, but it will not check for enter key

    Hello All,

    I am a php programmer and not a Javascript programmer and I am trying to help a friend out with his shopping cart. The original programmer (who wrote this years ago and is no longer around) has a button that looks up the quantities in the database for the submitted items, and then displays a prompt if you selected a quantity that is less than the minimum purchase amount.

    The issue is if the user enters a smaller amount than allowed and hits the enter key instead of the "Buy" button, it allows the order. I would appreciate any help on how to use the current code to also check for an Enter key submission.I can handle the PHP and the db lookup for minimum quantities.

    Here is the page code and the Javascript code.

    Code:
    <a href="javascript:checkQuantity(document.form<?php echo $formCount; ?>,
    <?php echo $row_rsProduct['lotQty']; ?>,
    <?php echo $row_rsProduct['minOrder']; ?>)"
    onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('ID','',
    'images/page-structure/buy_button_dw.jpg',1)">
    <img src="images/page-structure/buy_button_up.jpg"
    alt="Buy Button" name="Buy_Btn_<?php echo $row_rsProduct['ID']; ?>" 
    width="48" height="21" border="0" id="Buy_Btn_<?php echo $row_rsProduct['ID']; ?>"></a>
    Code:
    	function checkQuantity(theForm, numQty, minQty) {
    		if (minQty != -1 && theForm.q.value < minQty) {
    			alert ("You must buy at least " + minQty + " of this item");
    		} else if (numQty != -1 && mod(theForm.q.value, numQty) != 0) {
    			alert ("You must buy in quantities of " + numQty);
    		} else {
    			theForm.submit();
    		}
    	}
    Like I said above. I am hoping to keep the current code and add a hidden field or something like this URL talks about. (sorry for the link to another forum)

    http://stackoverflow.com/questions/2...key-is-pressed

    Thanks,
    Don

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,132
    The problem is the function is being called on the click of the button instead of on the submission of the form.
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Feb 2008
    Posts
    5
    Thanks cbVision,

    I am not sure how to change the button code and have them both work for the form submission. Can you give me some guidance here?

    Thanks,
    Don

  4. #4
    Join Date
    Feb 2008
    Posts
    5
    I did not figure out how to add the button code to the form submission, but I did find some code to stop the Enter button from submitting the form.

    Code:
    <script type="text/javascript">
    function noenter() {
      return !(window.event && window.event.keyCode == 13); }
    </script>
    And on the quantity field, I added:

    Code:
     onkeypress="return noenter()"
    Not a solution, but it fixed the issue. If you have a true solution to get both the button and Enter key to do the same thing, I would still like to see it (so I learn how to do it the correct way).

    Thanks,
    Don

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles