次のスクリプトを使用して、私のウェブサイトにphp-mysql投票システム(YouTubeのようなもの)を構築しています。また、私はnewtest.phpでphpを実行するためにajaxを使用しています。私はそれを自分でPHPをテストし、それは正常に働いた。今、私はAJAXでマウスをクリックして上下に投票していますが、うまくいきません。私はどこに間違っているのか教えていただけますか?(私はAJAの初心者です)。また、私は戻って、自動的に最新の投票番号を表示したい。php-mysql投票システム?
$contentVar = $_POST['contentVar'];
if ($contentVar == "con1") {
$db_host = "host";
$db_username = "username";
$db_pass = "pass";
$db_name = "phphitscounter";
@mysql_connect ("$db_host","$db_username","$db_pass") or die ("Could Not Connect To The Database");
@mysql_select_db ("$db_name") or die ("No Database");
mysql_select_db("phphitscounter", @mysql_connect ("$db_host","$db_username","$db_pass"));
$ip = $_SERVER['REMOTE_ADDR'];
$Page = $_SERVER['PHP_SELF'];
$pagenum = preg_replace("/[^0-9]/", '', $Page);
$query = mysql_query("SELECT ip FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
if (mysql_num_rows($query) > 0) {
echo "You have already voted";
}
else{
echo "Thank you for voring";
mysql_query("INSERT INTO vote (ip, page, voteup, votedown) VALUES ('$ip', '$pagenum', 0, 0)");
mysql_query("UPDATE vote SET voteup = voteup + 1 WHERE ip = '$ip' AND page = '$pagenum'");
}
$voteup = mysql_query("SELECT voteup FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
$results_voteup = mysql_fetch_array($voteup, MYSQL_NUM);
print ($results_voteup[0]);
echo "1";
}
else if ($contentVar == "con2") {
$db_host = "host";
$db_username = "username";
$db_pass = "pass";
$db_name = "phphitscounter";
@mysql_connect ("$db_host","$db_username","$db_pass") or die ("Could Not Connect To The Database");
@mysql_select_db ("$db_name") or die ("No Database");
mysql_select_db("phphitscounter", @mysql_connect ("$db_host","$db_username","$db_pass"));
$ip = $_SERVER['REMOTE_ADDR'];
$Page = $_SERVER['PHP_SELF'];
$pagenum = preg_replace("/[^0-9]/", '', $Page);
$query = mysql_query("SELECT ip FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
if (mysql_num_rows($query) > 0) {
echo "You have already voted";
}
else {
echo "Thank you for voring";
mysql_query("INSERT INTO vote (ip, page, voteup, votedown) VALUES ('$ip', '$pagenum', 0, 0)");
mysql_query("UPDATE vote SET votedown = votedown + 1 WHERE ip = '$ip' AND page = '$pagenum'");
}
$votedown = mysql_query("SELECT voteup FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
$results_votedown = mysql_fetch_array($votedown, MYSQL_NUM);
print ($results_votedown[0]);
echo "2";
}
test.phpをページ:で
<img src="Assets/like.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con1');"/>
<img src="Assets/dislike.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con2');"/>
:
<html>
<head>
<script type="text/javascript" src="Scripts/jquery.js"></script>
<script language="JavaScript" type="text/javascript">
function swapContent(cv) {
$("#myDiv").html('<img src="Assets/loading.gif" width="50" height="50"/>').show();
var url = "scripts/newtest.php";
$.post(url, {
contentVar: cv
}, function(data) {
$("#myDiv").html(data).show();
});
}
</script>
<style type="text/css">
#myDiv {
width: 200px;
height: 150px;
padding: 12px;
border: #666 1px solid;
background-color: #FAEEC5;
font-size: 18px;
}
</style>
</head>
<body>
<img src="Assets/like.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con1');" />
<img src="Assets/dislike.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con2');" />
<div id="myDiv"></div>
</body>
</html>