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

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12

    forms & javascript


    I know how to do the fancy little popup yes or no confirmation on a form, that's not what I am referring too. I think what I am wanting requires a javascript function, but then again I am not quite sure.

    How do you do some mathematical comparison or action on some hidden form fields and if certain conditions meet, display a pop-up window that says, nope sorry you cant do that. (On submit)

    An example... a shop in an online game, it lists all the items you can purchase and their prices. you check the ones you want to buy then when you click purchase it checks amount spent vs total amount of money your char has (sent as hidden form field), if you don't have the appropriate cash it says nope, sorry you cant buy that.

    And to take it one step further, I have once done a dropdown box that uses an OnChange event to submit the form when you change whats in the dropdown box. Could you take this and use it to make it when you click a checkbox next to an item, it then does the check versus your total money and tells you you can't buy that item (And even better, also deselect the checkbox you just checked)

    I have never done any javascript aside from the onchange events and such, so any code examples would be greatly appreciated.

    many thanks,
    -Sotonin
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Mexico
    Posts
    116
    Rep Power
    12
    2 things...

    You posted in the wrong forum to ask for Javascript support, this is PHP forum.

    What you need is also posible (and more reliable) with PHP, but you need much more resources (knowledge, server) to achieve this than you will if you solve it with javascript. it is up to you...

    Greetings!
    The best way to learn is:
    read + try + ask (in that order)

    Manolo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    Aye, i posted it here because i code in PHP and would be trying to interact with php variables. How would you be able to accomplish this in php? what would be needed. Either way would suffice, whichever i am able to do.

    thanks,
    -Sotonin
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Mexico
    Posts
    116
    Rep Power
    12
    Well, in PHP you could let the user to click or change anything, and then in the next step, yo can compare the value of the total buy against the user's balance, if its greater than the balance, you display the form again, else you process the purchase...

    Got it?

    If you want to pre-check that before sending the form, you couls do something like this:

    In your onchange, set the action to... onChange="mycheck()"


    Then make a little script like this:
    Code:
    <script language="javascript">
    function mycheck(){
    thisbuy=document.yourform.item1.value + document.yourform.item2.value;
    if (thisbuy>document.yourform.max.value) {
    alert "You can't afford that!";
    } else {
    document.yourform.submit;
    }
    </script>
    Or something like that... you can check it in the JS forum...

    Greetings!
    The best way to learn is:
    read + try + ask (in that order)

    Manolo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    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?
    Last edited by Sotonin; May 11th, 2003 at 02:29 PM.
    -Sotonin
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Mexico
    Posts
    116
    Rep Power
    12
    As you can see with the color coding...

    You are using single quotes on a phrase that also have single quotes...

    alert("Sorry, you don't have enough gold.");
    The best way to learn is:
    read + try + ask (in that order)

    Manolo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    hehe. yep just noticed that and edited it. not sure if that's all thats wrong though

    I don't think this portion is working correctly.

    if (f.minor) {
    and
    if (f.full) {

    I am trying to discern which submit button was clicked. is there a different syntax for this? The script simply does nothing it doesnt pop up a box.
    Last edited by Sotonin; May 11th, 2003 at 02:18 PM.
    -Sotonin
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Mexico
    Posts
    116
    Rep Power
    12
    I'm not totally into JS, but you might try to call the funtion like this...

    onSubmit="return Check(this.form);"

    That could help...
    The best way to learn is:
    read + try + ask (in that order)

    Manolo
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Mexico
    Posts
    116
    Rep Power
    12
    I insist... you could have already found the solution if you posted in the JS forum...

    As you can see, here we are guessing... there you could find someone who really knows what's this about...

    The best way to learn is:
    read + try + ask (in that order)

    Manolo
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    448
    Rep Power
    12
    ok thanks. =)
    -Sotonin

IMN logo majestic logo threadwatch logo seochat tools logo