Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-06-2011, 05:41 PM   PM User | #1
blug
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
blug is an unknown quantity at this point
Need Help

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 =/

Thankyou!!
blug is offline   Reply With Quote
Old 10-06-2011, 06:37 PM   PM User | #2
ironboy
Regular Coder

 
Join Date: Sep 2011
Location: Sweden
Posts: 154
Thanks: 1
Thanked 22 Times in 22 Posts
ironboy is an unknown quantity at this point
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...
ironboy is offline   Reply With Quote
Old 10-06-2011, 09:33 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.

Example:
Code:
<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:
Code:
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:08 AM.


Advertisement
Log in to turn off these ads.