...

View Full Version : User Variable in Onclick



ptmuldoon
11-27-2007, 09: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;
}
}
</script>


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?

Ahri
11-27-2007, 09: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)

ptmuldoon
11-27-2007, 11:16 PM
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;

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

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



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum