...

View Full Version : OnClick to run function



ptmuldoon
11-15-2007, 05:21 PM
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


<span id=\"white_player\">
<input type=\"button\" value=\"Sit\" onclick=\".$this->sit_white_player().\" />
</span>


the function
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
}

}

mlseim
11-15-2007, 05:42 PM
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.

Inigoesdr
11-15-2007, 05:56 PM
You could use a Javascript onclick call to retrieve the page via AJAX without reloading it.

ptmuldoon
11-15-2007, 07:32 PM
I'm currently ok with a page refresh. But can you call a php script from an input button?

Inigoesdr
11-15-2007, 07:34 PM
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.

ptmuldoon
11-15-2007, 08:32 PM
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.

Inigoesdr
11-15-2007, 08:47 PM
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.

ptmuldoon
11-15-2007, 09:57 PM
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.


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

Inigoesdr
11-15-2007, 10:16 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum