/    Sign up×
Community /Pin to ProfileBookmark

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]

[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();
}
}[/CODE]

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)

[URL=”http://stackoverflow.com/questions/29943/how-to-submit-a-form-when-the-return-key-is-pressed”]http://stackoverflow.com/questions/29943/how-to-submit-a-form-when-the-return-key-is-pressed[/URL]

Thanks,
Don

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionNov 09.2011 — The problem is the function is being called on the click of the button instead of on the submission of the form.
Copy linkTweet thisAlerts:
@dmacmanauthorNov 09.2011 — 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
Copy linkTweet thisAlerts:
@dmacmanauthorNov 09.2011 — 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>[/CODE]


And on the quantity field, I added:

[CODE] onkeypress="return noenter()"[/CODE]

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
×

Success!

Help @dmacman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...