代码如下:
  index.php
<?php
ob_start();
session_start();
require_once("config.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>简单投票系统</title>
<style type="text/css">
/*全局样式*/
body { font-family: "宋体"; font-size: 12pt; color: #333333; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;background-image: url(images/Zlppy_Bg.jpg);} 
table { font-family: "宋体"; font-size: 9pt; line-height: 20px; color: #333333}
a:link { font-size: 9pt; color: #333333; text-decoration: none} 
a:visited { font-size: 9pt; color: #333333; text-decoration: none} 
a:hover { font-size: 9pt; color: #E7005C; text-decoration: underline} 
a:active { font-size: 9pt; color: #333333; text-decoration: none} 
/*全局样式结束*/
</style>
<script language="javascript">
function check()
{
node=frm.itm;
flag=false;
for(i=0;i<node.length;i++)
{
if(node[i].checked)
{
flag=true;
}
}
if(!flag)
{
alert("您没有选择")
return false;
}
return true;
}
</script><?php    if($_POST["submit"]){

if($_SESSION["vote"]==session_id())
{
?>
<script language="javascript">
alert("您已经投票了");
location.href="index.php";
</script>
<?php
exit();
}
$id=$_POST["itm"];
$sql="update vote set count=count+1 where id=$id";
if(mysql_query($sql))
{
$_SESSION["vote"]=session_id();
?>
<script language="javascript">alert("投票成功,点确定查看结果");location.href="index.php?id=ck";</script>
<?php
}
else
{
?>
<script language="javascript">alert("投票失败");location.href="index.php";</script>
<?php
}
}
?></head>
<body>
<form name="frm" action="" method="post" onsubmit=return(check()) style="margin-bottom:5px;">
<table width="365" border="0" align="center" cellpadding="12" cellspacing="1" bgcolor="pink">
<tr>
<th bgcolor="#FFFFCC">
<?php
$sql="select * from votetitle";
$rs=mysql_query($sql);
$row=mysql_fetch_assoc($rs);
echo $row["votetitle"];
?> </th>
</tr>
<?php
$sql="select * from vote";
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs))
{
?>
<tr>
  <td bgcolor="#FFFFFF"><input type="radio" name="itm" value="<?php echo $rows["id"]?>" />&nbsp;&nbsp;<?php echo $rows["item"]?></td>
</tr>
<?php
}
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="submit" value="投票"/>
<input type="button" value="查看结果" onClick="location.href='index.php?id=ck'" /></td>
</tr>
</table>
</form>
<?php if($_GET["id"]=="ck"){?><?php
$sql="select sum(count) as 'total' from vote";
$rs=mysql_query($sql);
$rows=mysql_fetch_assoc($rs);
$sum=$rows["total"];  //得出总票数
$sql="select * from vote";
$rs=mysql_query($sql);
?>
<table width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2">
<tr>
<th bgcolor="#FFFFFF">项目</th>
<th bgcolor="#FFFFFF">票数</th>
<th bgcolor="#FFFFFF">百分比</th>
</tr>
<?php
while($rows=mysql_fetch_assoc($rs))
{
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $rows["item"]?></td>
<td bgcolor="#FFFFFF"><?php echo $rows["count"]?></td>
<td bgcolor="#FFFFFF">
<?php
$per=$rows["count"]/$sum;
$per=number_format($per,4);
?>
<img src="100.jpg" height="4" width="<?php echo $per*100?>" />
<?php echo $per*100?>% </td>
</tr>
<?php
}
  ?>
  </table>
  <div align="center">
  <a href="index.php">隐藏结果</a>
  </div>
 <?php } ?>
 </body>
  </html>admin.php<?php
require_once("config.php");
?>
<!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=gb2312" />
<link href="../style.css" rel="stylesheet" type="text/css" />
<title>简单投票系统</title>
<style type="text/css">
/*全局样式*/
body { font-family: "宋体"; font-size: 12pt; color: #333333; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;background-image: url(images/Zlppy_Bg.jpg);} 
table { font-family: "宋体"; font-size: 9pt; line-height: 20px; color: #333333}
/*全局样式结束*/
</style>
<script language="javascript">
function selectAll()
{
node=window.document.frm.itm;
for(i=0;i<node.length;i++)
{
node[i].checked=true;
}
}
function cancelAll()
{
node=frm.itm;
for(i=0;i<node.length;i++)
{
node[i].checked=false;
}
}
function del()
{
node=frm.itm;
id="";
for(i=0;i<node.length;i++)
{
if(node[i].checked)
{
if(id=="")
{
id=node[i].value
}
else
{
id=id+","+node[i].value
}
}
}
if(id=="")
{
alert("您没有选择删除项");
}
else
{
location.href="?type=del&id="+id
}
}
</script>
</head>
<body>
<?php
if($_POST["Submit"])
{
$title=$_POST["title"];
$sql="update votetitle set votetitle='$title'";
mysql_query($sql);
?>
<script language="javascript">
alert("修改成功");
</script>
<?php
}
if($_POST["Submit2"])
{
$newitem=$_POST["newitem"];
$sql="insert into vote (titleid,item,count) values (1,'$newitem',1)";
mysql_query($sql);

}
?>
<form id="frm" name="frm" method="post" action="" style="margin-bottom:3px;">
  <table width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2">
    <tr>
      <td colspan="4" bgcolor="#FFFFFF"><label>
  <?php
   $sql="select * from votetitle";
$rs=mysql_query($sql);
$rows=mysql_fetch_assoc($rs);
  ?>
        <input name="title" type="text" id="title" size="35" value="<?php echo $rows["votetitle"]?>" />
      </label></td>
      <td width="68" align="center" bgcolor="#FFFFFF"><label>
        <input type="submit" name="Submit" value="修改标题" />
      </label></td>
    </tr>
    <tr>
      <th width="30" bgcolor="#FFFFFF">编号</th>
      <th width="45" bgcolor="#FFFFFF">项目</th>
      <th width="52" bgcolor="#FFFFFF">票数</th>
      <th width="50" align="center" bgcolor="#FFFFFF">修改</th>
      <th align="center" bgcolor="#FFFFFF">删除</th>
    </tr>
    <?php
$sql="select * from vote order by count desc";
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs))
{
?>
<tr>
      <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name="itm" value="<?php echo $rows["id"]?>" /><?php echo $rows["id"]?></td>
      <td align="center" bgcolor="#FFFFFF"><?php echo $rows["item"]?></td>
      <td align="center" bgcolor="#FFFFFF"><?php echo $rows["count"]?></td>
      <td align="center" bgcolor="#FFFFFF"><input type="button" value="修改" onclick="location.href='?type=modify&id=<?php echo $rows["id"]?>'" /></td>
      <td align="center" bgcolor="#FFFFFF"><input type="button" value="删除" onclick="location.href='?type=del&id=<?php echo $rows["id"]?>'"  /></td>
    </tr>
<?php
}
?>
    <tr>
      <td colspan="5" align="center" bgcolor="#FFFFFF">
   <input type="button" value="选择全部" onclick="selectAll()" />
<input type="button" value="取消全部" onclick="cancelAll()" />
  <input type="button" value="删除所选" onclick="del()" />   </td>
    </tr>
    <tr>
      <td colspan="3" bgcolor="#FFFFFF"><label>
        <input name="newitem" type="text" id="newitem" />
      </label></td>
      <td colspan="2" bgcolor="#FFFFFF"><label>
        <input type="submit" name="Submit2" value="添加新项" />
      </label></td>
    </tr>
  </table>
</form><?php    if($_GET["type"]=="modify"){

$id=$_GET["id"];
if($_POST["Submit3"])
{
$item=$_POST["itm"];
$count=$_POST["count"];
$sql="update vote set item='$item',count=$count where id=$id";
mysql_query($sql);
echo "<script language=javascript>alert('修改成功!');window.location='admin.php'</script>";
}
$sql="select * from vote where id=$id";
$rs=mysql_query($sql);
$rows=mysql_fetch_assoc($rs);
?>
<form id="form1" name="form1" method="post" action="" style="margin-top:2px;">
  <table width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2">
    <tr>
      <th colspan="2" bgcolor="#FFFFFF">修改投票项目</th>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">名称:</td>
      <td bgcolor="#FFFFFF"><label>
        <input name="itm" type="text" id="itm" value="<?php echo $rows["item"]?>" />
      </label></td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">票数:</td>
      <td bgcolor="#FFFFFF"><label>
        <input name="count" type="text" id="count" value="<?php echo $rows["count"]?>" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#FFFFFF"><label>
        <input type="submit" name="Submit3" value="修改" />
        <input type="reset" name="Submit" value="重置" />
      </label></td>
    </tr>
  </table>
</form><?php 
}
?>
<?php
if($_GET["type"]=="del"){
$id=$_GET["id"];
$sql="delete from vote where id in ($id)";
mysql_query($sql);
echo "<script language=javascript>alert('删除成功!');window.location='admin.php'</script>";
}
?>
</body>
</html>
config.php<?php
$conn=@mysql_connect("localhost","root","");
if($conn==null)
die("数据库连接失败");
mysql_query("set names 'gb2312'");
if(!mysql_select_db("vote"))
{
die("数据库连接失败");
}
?>

解决方案 »

  1.   

    代码太乱,大致扫了一眼没看明白...我看没人回复 楼主我给你说个思路你自己研究下吧: 使用firebug观察当你点击时是否正确触发ajax.(看看是否js出错)如果正确触发ajax,看看ajax都传了什么参数 看看参数是否出错看看返回什么值,是否正确返回值. 如果正确返回 那就检查返回后的js如果没有正确返回,逐行查看提交到的php页面,用ajax传过去的参数一行行试.
      

  2.   

    代码我就不看了,太乱了,获取不到值的话,你检查一下变量名有没写错?或者get和post方法不一致?
      

  3.   

    太长了,你应该把css,js 都拆分开写,link 引入,最基本的规范啊~
      

  4.   

    line 58
    $id=$_POST["itm"]; 这里拿不到$id吗?
      

  5.   

    你是如何知道没有数据的?
    程序中并没有相关的代码从贴图顶部的错误信息片段上看,php 的错误检查是全开的
    因此你需要对于每个做取值操作变量做有效性检查,除非你能确认其真实存在

    if($_POST["submit"]){
    应写作
    if(isset($_POST["submit"])){if($_SESSION["vote"]==session_id())
    应写作
    if(isset($_SESSION["vote"]) && $_SESSION["vote"]==session_id())
      

  6.   

    代码太长了,没看完。没取得,看参数名对了没,post的路径对不对吧
      

  7.   

    你还真是女的哟,$id=$_POST["itm"];是这里取值不到?