0

我对 php 和 javascript 很陌生,但仍在尝试让我的网页正常工作。我试图在页面中添加一个 5 星投票系统,该系统将联系我的数据库并进行相应更新。您可能已经注意到我的代码语法也不完美。

<div class="first"  onmouseover="starmove('-32px')" onmouseout="starnormal()" onclick="rate('harrys','1')">

这是“联系”javascript的div

function rate(id,ratings){
var i=id;
var r=ratings;
var xmlhttp;
if (window.XMLHttpRequest)
  { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
else
   { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   }
 xmlhttp.onreadystatechange=function()
   {
xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
xmlhttp.send();
   }

这是javascript

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Namnlös 1</title>
</head>
<body>
<?php
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
mysql_set_charset('utf8');
$itemid = ($_GET["id"]);
$rating = ($_GET["rating"]);
$query = "INSERT INTO ratings(id,rating) VALUES ('$itemid','$rating')";
mysql_query($query);
?>
</body>
</html>

这是我的 rating.php 文件(注意:mysql_connect 中的值在实际页面上,即使此处未定义。)

如果我只是打开我的 php 文件,则会在 mysql 中添加一行,所以我知道它已正确连接。

4

4 回答 4

5

您将值作为 GET 传递给 rating.php,但您将其作为 POST 获取

xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);

在这里,即使您将方法指定为 POST,?id="+i+"&rating="+r也意味着 id 和 rating 将作为 GET 传递。

所以你不能访问它们

$itemid = ($_POST["id"]);
$rating = ($_POST["rating"]);

您需要将其更改为:

$itemid = ($_GET["id"]);
$rating = ($_GET["rating"]);

还将您的查询更改为:

$query = "INSERT INTO ratings(id,rating) VALUES ('".$itemid."','".$rating."')";

编辑:

我在这里更新了 Javascript 代码。尝试这个:

<script>
function rate(id,ratings)
{
    var i=id;
    var r=ratings;
    var xmlhttp;
    if (window.XMLHttpRequest)
    { 
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
    xmlhttp.send();
}
</script>

变化: 1. 后面有两个大括号:

    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   }

第二个大括号有效地关闭了rate()函数,因此之后的代码没有按照预期进行。所以我把那个大括号移到了代码的末尾,以包装函数。

  1. onreadystatechange函数在数据被发送到服务器后调用,以跟踪请求发生了什么。在您的代码中,您在 onreadystatechange 内部发送请求,该请求永远不会执行,因为请求永远不会被发送..

我变了

  xmlhttp.onreadystatechange=function()
       {
         xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
         xmlhttp.send();
       }

进入一个简单的 POST 调用:

 xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
 xmlhttp.send();

我希望这能解释一切:)

于 2012-08-17T19:33:46.407 回答
1

您应该尝试使用 jQuery 进行 XHTTP 调用。这样,您可以确定您的代码支持所有浏览器。

将此添加到您的头部:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

然后使用此代码调用 PHP 页面:

var i = id;
var r = ratings;
$.get("http://------/rating.php?id="+i+"&rating="+r);

我还注意到您确实在查询的“id”字段中输入了“sds”。“id”字段应该是一个整数。如果它已经是一个整数,那是你的问题。还将 $_POST 更改为 $_GET。固定版本:

$itemid = ($_GET["id"]);
$rating = ($_GET["rating"]);
$query = "INSERT INTO ratings(id,rating) VALUES ($itemid,'$rating')";
于 2012-08-17T19:34:51.033 回答
0

请使用 jQuery 处理 ajax 请求。jQuery 可以跨浏览器工作,你可以确定它可以工作。

还:

$itemid = ($_POST["id"]);
$rating = ($_POST["rating"]);
$query = "INSERT INTO ratings(id,rating) VALUES ('sds','$rating')";

不安全

$itemid = intval($_POST["id"]);
$rating = intval($_POST["rating"]);
$query = "INSERT INTO ratings(id,rating) VALUES ('sds','$rating')"; 

好多了

于 2012-08-17T19:34:14.527 回答
0

First, use relative instead of absolute links if the php file you're trying to request is in the same site:

// Absolute link
xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);

// Relative link
xmlhttp.open("POST","rating.php?id="+i+"&rating="+r,true);

Then, you're trying to send and retrieve the info via POST, but you're sending the info in a query string (GET):

JS:

xmlhttp.open("GET","rating.php?id="+i+"&rating="+r,true);

PHP:

$itemid = $_GET["id"];
$rating = $_GET["rating"];

Finally, before you insert, use mysql_real_escape_string() or mysqli_real_escape_string(), depending on the MySQL library you're using, to prevent SQL injection in the database and compromise your site:

$itemid = mysql_real_escape_string($_GET["id"]);
$rating = mysql_real_escape_string($_GET["rating"]);
$query = 'INSERT INTO ratings(id,rating) VALUES ('.$itemid.','.$rating.')';
mysql_query($query);
于 2012-08-17T19:56:55.323 回答