【发布时间】:2016-12-02 17:27:29
【问题描述】:
<img id="image" src="jj.png" onclick="randomizeImage();"/>
<h2 id="Score" name="Score1">0</h2>
<script>
var counter=0;
function randomizeImage() {
counter++;
var test= document.getElementById('Score');
test.innerHTML= counter;
}
</script>
<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$user_Name = $_COOKIE["username"]; //i stored the username as a cookie and retrieved it here
echo $user_Name; //echo it so i can check if it is right
$New= $_POST["Score"]; //**this is the part which is undefined**
$sql = "UPDATE User SET Score = '$New' WHERE ID='$user_Name'";
if (!mysql_query($sql,$con))
{
die('Error please try again ' . mysql_error());
}
mysql_close($con)
?>
我有一个图像,每当我单击它时,它都会调用一个函数,该函数将使计数器增加 1。这个分数将反映在 html 端,并在我单击图像时增加。但是现在我想把这个计数器数据带入 php 中,我可以将它上传到一个数据库中,它与他/她在以前的 phpfile 中输入的用户用户名相匹配。我不能把分数的价值带过来吗?它一直说未定义的索引。
我有两个名为 ID 和 Score 的列。 ID 是我为用户的用户名评分的地方,Score 是计数器的分数。我希望每次按用户名的图像时更新分数。
数据库名称是 :database 表名是:用户
在没有 AJAX 的情况下有没有办法做到这一点?
【问题讨论】:
标签: php html database post cookies