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

    OnClick to run function

    I'm trying to create a 'Sit' and 'Stand' button to use in a chess game, and trying to run the function when the user click's on a button. I have the following for the button code, and for the function, yet it didn't seem to work:

    button code
    Code:
    <span id=\"white_player\">
    	<input type=\"button\" value=\"Sit\" onclick=\".$this->sit_white_player().\" />
    </span>
    the function
    PHP Code:
    function sit_white_player()  // Add the sit player functions here
        
    {
            
    $game_query mysql_query("SELECT * FROM  ".$this->settings->mysql_prefix."game WHERE id='".$this->gid."'");
            
    $game_row mysql_fetch_object($game_query);
            
            
    mysql_free_result($game_query);
            
            
    // First confirm the player id is 0 for a waiting player
            
            
    if($game_row->white_player 0)
            {    
    // Player ID is 0, so let user join game
                             
    mysql_query"INSERT INTO" .$this->settings->mysql_prefix"game SET white_player = ".$_SESSION['user']['id']." WHERE id='".$this->gid."' ");
            }else
            {
                
    // Seat if full, so can not join
            
    }

        } 
    Last edited by ptmuldoon; 11-15-2007 at 05:32 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I think that "onclick" is Javascripting and you can't call a PHP script
    from there. The button will need to be a link to a PHP script. You'll
    end up with a page refresh though, because PHP is server-side.

    We'll need to know more about your layout and whether or not a
    page refresh is OK.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You could use a Javascript onclick call to retrieve the page via AJAX without reloading it.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    I'm currently ok with a page refresh. But can you call a php script from an input button?

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yes, with AJAX. If you're OK with a page refresh then just pass a variable in the link and have code that checks for it/executes your function.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by Inigoesdr View Post
    Yes, with AJAX. If you're OK with a page refresh then just pass a variable in the link and have code that checks for it/executes your function.
    I hate to ask this. But can someone give a small example on how that is done to help me learn it? Just an example though, that i may be able to run with and modify to get working with what i have above. I'd like to try and learn more on how its all done.

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Here is one: http://www.webpasties.com/xmlHttpRequest/
    You can find many more by just searching for any combination of "AJAX tutorial example PHP XMLHTTPRequest" etc.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks

    I also did forget to mention that I am modifying scripts to be used with php5 and am adding functions to existing classes. I'm trying to avoid ajax for now till I get more comfortable with the php side of things. But as I keep studying the code, I've modified a link as follows, but its still not working yet, although I think I am closer now. The page refreshes like it should be running the function, but nothing happens.

    PHP Code:
    <input type="button\" value=\"Sit\" href=\"index.php?modul=game&method=sit_white_player&id=$id\" onClick=\"return confirm('Sit down as white player?')\"> 

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Is "modul" supposed to be "module"? print_r()/var_dump() the variable you expect to be set that is not and find out why it isn't.


  •  

    Posting Permissions

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