Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Need Help

  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!!

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    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...

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.


  •  

    Posting Permissions

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