View Full Version : Need Help

10-06-2011, 06:41 PM
Hey guys, I am creating a website to sell some stuff on but I have hit a bit of a problem, it's not huge it's just annoying.

I have created a bunch of methods for a shopping cart I am implementing on the website, and at the same time I take information from my database to get the quantity of how much stock I have left.

Basically I want to do a bit of error handling, so that if you select 5 things to go into the shopping cart but the amount of items that are in stock is only 3 then an error message will come up saying "Sorry! Don't have that many items in stock!". I have a setCookie method which I created, and it is triggered by an onclick event.

I guess what I am trying to say is... Is it possible to run conditional statements on events such as "onclick" or "onload"? As it currently stands, I am going to have to do the error handling within my external js file and that really seems sloppy to me since I really want that external file to stick to cookie creation. I would really prefer to do my conditionals within my view/html file =/


10-06-2011, 07:37 PM
Actually it is not sloppy to handle javascript in a script file ;)

What might seem sloppy to many seasoned programmers is to mix presentation (your html) with functions/scripting (assigning event handlers).

I would suggest that you move all scripting/event assigning etc to a script file. If you feel the need to only have code handling your cookies in one script file, then create another one for events etc... :)

Old Pedant
10-06-2011, 10:33 PM
I'm curious what the cookies have to do with the onclick handling, at all.

And if it really is onclick, what is the user clicking on to select a quantity??? If radio buttons, then why even display buttons for quantities not in stock? Or even if it's a <select> for quantity, same question. If it's simply an <input> box for quantity, then don't you mean onchange will trigger the quantity check?

If it's an <input>, then I'd probably have the PHP/JSP/ASP code create the <form> with a hidden field that matches the <input> and holds the current inventory amount.


<input type="hidden" name="prod_7731_inv" value="3" />
<input type="text" name="prod_7731_qty" />

Now you can attach onchange handlers to all the "prod_xxx_qty" <input>s and the code is simple:

function attachQuantityChecks()
var form = document.forms[0];
for ( var e = 0; e < form.elements.length; ++e )
var elem = form.elements[e];
if ( elem.name != null && elem.name.match(/^prod_\d+_qty$/) != null )
elem.onchange = function(){ checkQuantity(this); }
window.onload = attachQuantityChecks;

function checkQuantity( inp )
var invname = inp.name.replace(/_qty/,"_inv");
var inv = Number(inp.form[invname].value);
var qty = parseInt( inp.value );
if ( isNaN(qty) || qty < 0 )
alert("Invalid quantity");
return false;
if ( qty > inv )
alert("Sorry, we only have " + inv + " in stock");
return false;
return true;

And I know I got carried away. Sorry. Kind of thinking out loud on the easiest way to do this. But note that the above code can be quite nicely put in a separate external JS file, as many JS purists would urge.