#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12

    Javascript Alert and Value comparison


    Here is some code I am trying to get working on this issue. I keep getting browser JS errors.

    PHP Code:
    <script>
    function 
    Check(f

    if (
    f.minor) {
        if (
    f.total_cash f.minor_cost) {
            
    alert('Sorry, you do not have enough gold.');
            return 
    false;
        } else {
            return 
    true;
        }
    }
    if (
    f.full) {
        if (
    f.total_cash f.full_cost) {
            
    alert('Sorry, you do not have enough gold.');
            return 
    false;
        } else {
            return 
    true;
        }
    }
    }

    </script> 
    And the form itself.
    PHP Code:
    <form name="healer" method="post" action="<?=$PHP_SELF?>" onSubmit="return Check(this);">
    <input name="total_cash" type="hidden" value="<?=$user_gold?>">
    <input name="minor_cost" type="hidden" value="<?=$minor_heal?>">
    <input name="minor_amt" type="hidden" value="<?=$heal_amt?>">
    <input name="full_cost" type="hidden" value="<?=$full_heal?>">
    <input name="full_amt" type="hidden" value="<?=$needed_hp?>">
    <input name="minor" type="submit" value="Heal <?=$heal_amt?> hps: <?=$minor_heal?>">
    <input name="full" type="submit" value="Full heal: <?=$full_heal?>">
    </form>
    Any ideas what I am messing up?
    -Sotonin
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    A couple of things; these:

    f.total_cash / f.minor_cost / f.full_cost

    ...are references to form elements (objects); you probably meant to compare their .value properties .
    Also, the values are strings and should be cast as numbers (converted). This:

    if (f.minor) / if (f.full)

    ...won't check which submit button was pressed, just if either exist - which, of course, they do.

    function Check(oSubmit)
    {
    var oForm = oSubmit.form;
    if (oSubmit == oForm.minor) {
    if (Number(oForm.total_cash.value) < Number(oForm.minor_cost.value)) {
    alert('Sorry, you do not have enough gold.');
    return false;
    } else {
    return true;
    }
    }
    else if (oSubmit == oForm.full) {
    if (Number(oForm.total_cash.value) < Number(oForm.full_cost.value)) {
    alert('Sorry, you do not have enough gold.');
    return false;
    } else {
    return true;
    }
    }
    }

    </script>
    </head>
    <body>

    <form name="healer" method="post" action="">
    ............
    ............
    <input name="minor" type="submit" value="Heal <?=$heal_amt?> hps: <?=$minor_heal?>" onclick="return Check(this)">
    <input name="full" type="submit" value="Full heal: <?=$full_heal?>" onclick="return Check(this)">
    </form>
    </body>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    awesome thanks. i knew i wasn't doing that portion correctly, i have not done much anything with javascript. I will try that code out and let you know.


    *** it works like a charm, you rock. Maybe you can help with another predicament i was needing to implement soon.

    First of all, think game when you read this. this portion is going to be a shop, but dont think ecommerce think diablo or another rpg shop that sells items.

    I suppose it would just be an adaptation of what you showed above, but it would need to be placed on OnChange event instead of OnClick?

    There would be a listing of items that are for sale, each one has the name price, and a checkbox next to it. If you check a box it should compare the value of it, and any other currently checked items versus your total available gold. if you don't have enough to purchase it, display a alert box telling you so, and unselect the check box.

    I suppose it might be good to go another step, when the form loads each time it checks the value of each item vs your gold and makes it .disabled ? and uncheckable by default. but this would not be enough to handle the incurring items the user may click. it should be dynamic as the user click on items to purchase.

    It seems possible, but yet my JS knowledge is far too lacking to complete something like this on my own.

    (I'm using php by the way. so the items and their prices would be pulled from a database table, along with their ID number.)

    many thanks in advance,
    Last edited by Sotonin; May 11th, 2003 at 05:12 PM.
    -Sotonin
  6. #4
  7. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Sotonin...

    Almost anything is possible these days. Your best bet is to post a typical sample of your form, complete with evaluated php (a delivered version, i.e.) and a description similar to the above, so we can recommend a specific approach.

IMN logo majestic logo threadwatch logo seochat tools logo