Javascript 协助完成以下五星评级脚本
在这里,JavaScript和CSS运行平稳。但每当我尝试运行PHP文件时,它都会显示一个错误,说明未定义的索引,如下所示:Javascript 协助完成以下五星评级脚本,javascript,php,html,mysql,css,Javascript,Php,Html,Mysql,Css,在这里,JavaScript和CSS运行平稳。但每当我尝试运行PHP文件时,它都会显示一个错误,说明未定义的索引,如下所示: Notice: Undefined index: do in C:\wamp\www\PROJECT\jquery_tutorial_starrating\update.php on line 8 Call Stack # Time Memory Function Location 1 0.0016 375888 {main}( ) ..\u
Notice: Undefined index: do in C:\wamp\www\PROJECT\jquery_tutorial_starrating\update.php on line 8
Call Stack
# Time Memory Function Location
1 0.0016 375888 {main}( ) ..\update.php:0
Notice: Undefined index: do in C:\wamp\www\PROJECT\jquery_tutorial_starrating\update.php on line 14
Call Stack
# Time Memory Function Location
1 0.0016 375888 {main}( ) ..\update.php:0
我的html代码是:
<html>
<head>
<script src="rating/jquery.min.js" type="text/javascript"></script>
<script src="rating/starrating.js" type="text/javascript"></script>
<link href="rating/starrating.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<h2>Star Rater</h2>
<ul class='star-rating'>
<li class="current-rating" id="current-rating"><!-- will show current rating --></li>
<span id="ratelinks">
<li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="javascript:void(0)" title="3 stars out of 5" class="three- stars">3</a></li>
<li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li>
</span>
</ul>
</body>
</html>
我的starrating.js文件-
// JavaScript Document
$(document).ready(function() {
// get current rating
getRating();
// get rating function
function getRating(){
$.ajax({
type: "GET",
url: "update.php",
data: "do=getrate",
cache: false,
async: false,
success: function(result) {
// apply star rating to element
$("#current-rating").css({ width: "" + result + "%" });
},
error: function(result) {
alert("some error occured, please try again later");
}
});
}
// link handler
$('#ratelinks li a').click(function(){
$.ajax({
type: "GET",
url: "update.php",
data: "rating="+$(this).text()+"&do=rate",
cache: false,
async: false,
success: function(result) {
// remove #ratelinks element to prevent another rate
$("#ratelinks").remove();
// get rating after click
getRating();
},
error: function(result) {
alert("some error occured, please try again later");
}
});
});
});
我的PHP文件:
<?php
// connect to database
$dbh=mysql_connect ("localhost", "abc", "") or die ('Cannot connect to the database');
mysql_select_db ("star",$dbh);
if($_GET['do']=='rate')
{
// do rate
rate();
}
else if($_GET['do']=='getrate')
{
// get rating
getRating();
}
// function to retrieve
function getRating(){
$sql= "select * from vote";
$result=@mysql_query($sql);
$rs=@mysql_fetch_array($result);
// set width of star
$rating = (@round($rs[value] / $rs[counter],1)) * 20;
echo $rating;
}
// function to insert rating
function rate(){
$text = strip_tags($_GET['rating']);
$update = "update vote set counter = counter + 1, value = value + ".$_GET['rating']."";
$result = @mysql_query($update);
if(@mysql_affected_rows() == 0){
$insert = "insert into vote (counter,value) values ('1','".$_GET['rating']."')";
$result = @mysql_query($insert);
}
}
?>
如果您试图访问不存在的数组索引,PHP会发出警告。与其使用
$\u GET['do']=='rate'
,不如尝试isset($\u GET['do'])&&$\u GET['do']=='rate'
,对于其他数组访问也是如此。此外,带标签
也不安全,无法防止SQL注入;改用mysql\u real\u escape\u string
。更好的是,切换到PDO或mysqli并使用参数化。您能告诉我@in@$_GET['do']的用法吗?请看这里,它将抑制错误消息:
<?php
// connect to database
$dbh=mysql_connect ("localhost", "abc", "") or die ('Cannot connect to the database');
mysql_select_db ("star",$dbh);
if($_GET['do']=='rate')
{
// do rate
rate();
}
else if($_GET['do']=='getrate')
{
// get rating
getRating();
}
// function to retrieve
function getRating(){
$sql= "select * from vote";
$result=@mysql_query($sql);
$rs=@mysql_fetch_array($result);
// set width of star
$rating = (@round($rs[value] / $rs[counter],1)) * 20;
echo $rating;
}
// function to insert rating
function rate(){
$text = strip_tags($_GET['rating']);
$update = "update vote set counter = counter + 1, value = value + ".$_GET['rating']."";
$result = @mysql_query($update);
if(@mysql_affected_rows() == 0){
$insert = "insert into vote (counter,value) values ('1','".$_GET['rating']."')";
$result = @mysql_query($insert);
}
}
?>