View Full Version : OnClick to run function

11-15-2007, 06: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().\" />

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);


// 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."' ");
// Seat if full, so can not join


11-15-2007, 06: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.

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

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

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

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

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

11-15-2007, 10:57 PM

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?')\">

11-15-2007, 11: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.