View Full Version : User Variable in Onclick

11-27-2007, 10:39 PM
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:

<script type="text/javascript">
function goConfirm(title, link){
if(confirm(title) == true){
window.location.href = link;
return true;
} else {
return false;

And then I have the following input link button being created

<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?

11-27-2007, 10:40 PM
There's this cool forum I go to sometimes that might just be able to help..... what was it called again? (http://www.codingforums.com/forumdisplay.php?f=2)

11-28-2007, 12:16 AM
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.

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


11-28-2007, 12:21 AM
You would have to pass the variable to a PHP page via GET or use AJAX to GET/POST it to the page.

11-28-2007, 05:47 PM
Actually, this does belong in this forum.

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');" />