First of all it's never a good idea to use <? instead of <?php. But anyway. Your loss. ;)
You will need to do a refresh of the page to be able to run an insert query.
The refresh will need to be made by the JavaScript and since I've refused to learn JavaScript, I can't help you with that. Remember also that the JS needs to send a variable containing the username to the PHP code.
Now, I haven't had enough coffee today, so this is the best I can give you:
<snip>
<?php
mysql_connect($hostname,$mysqluser,$mysqlpassword);
$username = $_POST['user']; // What ever your JS will send the variable in.
mysql_query("insert into score (user,date) values ('" . $username . "','".date("Y-m-d")."'););
?>
</snip>
Also, you might want to parse the username through a few str_replace's before you insert.. removing stuff like '<' and '>'.
Good luck!
Mikael
PS: I apparently sent this the first time using the wrong FROM address.. So here it comes again. ;)
On Feb 20, 2004, at 03:16, JeRRy wrote:
Hi,
I have this code, comments below:
<? include("../pages/setup.php");?> <? login();?> <? include("../pages/header.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <STYLE> .general3 {background-color:#333333;z-index:4} .general2 {background-color:#555555;z-index:3} .general1 {background-color:#777777;z-index:2} .general0 {background-color:#999999;z-index:1} </STYLE> <title></title> </head> <body>
<? include("./config.phtml"); include("./$languagefile");
mysql_connect($hostname,$mysqluser,$mysqlpassword); $query = "select * from kras_params;"; $result = mysql_db_query($database,$query); $row = mysql_fetch_array($result); //fill vars $total = $row[kr_total]; $chance = $row[kr_chance]; $maxwins = $row[kr_max_wins]; $wins = $row[kr_wins]; $timesaday = $row[kr_timesaday]; $host = $REMOTE_ADDR; $check = 1;
$q_checktimes = "select count(*) from kras_users where usr_ip='$host' and usr_date=CURDATE();"; $checktimes_result = mysql_db_query($database,$q_checktimes); $times = mysql_fetch_row($checktimes_result); $error = ""; if ($times[0] < $timesaday) { //poging toevoegen in database $raisetimes = "insert into kras_users values('$host',CURDATE(),'$HTTP_SESSION_VARS[username]');"; $raiseresult = mysql_db_query($database,$raisetimes) or die('error: raise times failed'); } else { $error .= "<LI>$txt_maxplay $timesaday."; $check = 0; } if ($wins == $maxwins) { $error = "<LI>$txt_maxwon"; $check = 0; }
if ($check) { //aantal malen dat spel gespeeld is ophogen met 1 $raisetotal = "update kras_params set kr_total=(kr_total+1);"; $raiseresult = mysql_db_query($database,$raisetotal); if ($total%$chance == 0) { $raisewins = "update kras_params set kr_wins=(kr_wins+1);"; $raisewinsresult = mysql_db_query($database,$raisewins) or die('error: raise wins failed'); $winner = 1; } ?>
<SCRIPT>
sc=new
Array('gfx/crown.jpg','gfx/x.jpg','gfx/seven.jpg','gfx/ diamond.jpg','gfx/ruby.jpg','gfx/magic.jpg');
z=new Array();
function check() {
var oDivs = document.all.tags("DIV");
for (i=0; i<oDivs.length; i++){
var scratched = "true";
if (oDivs(i).style.display != "none" &&
oDivs(i).scratchable == "true") {
scratched = "false";
return false;
}
}
if (scratched == "true") {
<? echo ($winner ? "alert(\"$txt_youwon! $HTTP_SESSION_VARS[username]\")" : "alert(\"$txt_youlost\")");
?>
}
}
function rnd() {
return Math.floor(Math.random()*sc.length);
}
<?
if ($winner) {
echo "function scimgload() {";
echo "document.scratch1.src=sc[rnd()];";
echo "document.scratch2.src=document.scratch1.src;";
echo "document.scratch3.src=document.scratch1.src;";
echo "}";
} else {
echo "function scimgload() {";
echo "document.scratch1.src=sc[rnd()];";
echo "document.scratch2.src=sc[rnd()];";
echo "document.scratch3.src=sc[rnd()];";
echo "while (document.scratch2.src ==
document.scratch3.src) {";
echo "document.scratch3.src=sc[rnd()];";
echo "}";
echo "}";
}
?>
function scmetal() {
for (i=0;i<192;i++)
document.all['M'+i].style.display="";
}
</SCRIPT>
<SCRIPT FOR=window EVENT=onload LANGUAGE="JScript">
scimgload();
</SCRIPT>
<h1><center><font color=blue>TMC Scratch n
Match</font></H1>
<DIV scratchable="false" STYLE="position:
relative;height: 300">
<SCRIPT>
// This script must be within the relatively
positioned DIV.
for (p=0;p<3;p++)
for (i=0;i<4;i++)
for (j=0;j<4;j++)
for (k=0;k<4;k++) {
idn=p*64+i*16+j*4+k;
document.write('<DIV scratchable="true" ID="M'+idn+'"
onmouseover="style.display=\'none\';check();"
CLASS="general'+i+'"
STYLE="position:absolute;width:25;height:25;top:'+eval(0+j*25)+'; left:'+eval(125+p*125+k*25)+'"></DIV>');
}
</SCRIPT>
<IMG SRC="" NAME=scratch1 ID=img1
STYLE="position:absolute;top:0;left:125;width:100;height:100;z-index: 0">
<IMG SRC="" NAME=scratch2 ID=img2
STYLE="position:absolute;top:0;left:250;width:100;height:100;z-index: 0">
<IMG SRC="" NAME=scratch3 ID=img3
STYLE="position:absolute;top:0;left:375;width:100;height:100;z-index: 0">
<BUTTON
STYLE="position:absolute;top:150;left:250;width:auto;height:50"
onclick="location.reload()"><? echo $txt_playagain;
?></BUTTON>
</DIV>
<?
} else {
echo $error;
}
?>
</center>
</BODY>
</HTML>
It's a scartchy game, 3 boxes, run your mouse over them to scratch, if you win it pops up a java box saying you won and your Username.
What I want to do is do a query to a db and update an accounting table to credit users account if they win, and if they lose do nothing.
But I am not sure what to add with this code... Does the page need to refresh to do a insert query or can it do it some other way?
When they win they get a popup, page does not refresh, to play again it restarts. Is there a way to credit the account without refreshing? If worst comes to worst I guess I could redirect the user to another page to credit accounts, but this would take them away from game and is not ideal.
Ideas/code welcome here.
Jerry
Find local movie times and trailers on Yahoo! Movies. http://au.movies.yahoo.com
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php