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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    User Variable in Onclick

    I'm currently using a small piece of javascript to provide a confirmation box when a user clicks on a link/button. The javascript is as follows:

    Code:
    <script type="text/javascript">
    function goConfirm(title, link){
    if(confirm(title) == true){
    window.location.href = link;
    return true;
    } else {
    return false;
    }
    }
    </script>
    And then I have the following input link button being created

    Code:
    <input type=\"button\" value=\"Stand\" onclick=\"goConfirm('Do you want to stand up?','index.php?modul=game&method=stand_player&stand=white');\" />
    I would like possible be able to use a variable for the confirmation statement of "Do you want to stand up?" Than I should be able to use either an if or switch/case to alter the confirm message (I think anyway).

    Can someone give an example of setting the onclick=$variable in the input link?

  • #2
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    There's this cool forum I go to sometimes that might just be able to help..... what was it called again?
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    lol, well I wasn't sure to post this in the javascripting forums or not.

    The reason being I'm trying to use a php variable in the onClick javascript function. Something like.

    From:
    onclick=\"goConfirm('Do you want to stand up?',

    To:
    onclick=$my_variable;

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You would have to pass the variable to a PHP page via GET or use AJAX to GET/POST it to the page.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Actually, this does belong in this forum.

    PHP Code:
    <?php
        
    switch ($action) {
            case 
    'stand'$message 'Do you want to stand up?'$method 'stand_player';
            case 
    'sit'$message 'Do you want to sit down?'$method 'sit_player';
            
    // etc.
        
    }
    ?>
    <input type="button" value="Stand" onclick="goConfirm('<?php echo $message?>','index.php?modul=game&method=<?php echo $method?>&stand=white');" />


  •  

    Posting Permissions

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