使用按钮切换mysql数据库显示,更新的图标仅在页面刷新时更改

时间:2015-10-26 13:54:17

标签: php ajax

我已经设法在我的mysql数据库中编辑特定值,只需单击当前显示的图标即可。如果再次单击,这会将值从0更改为1,反之亦然。问题是只有刷新页面时图标才会更新新图标。事实上,当我点击当前图标时,我会得到一种无效的图标。见图。我附上了我的代码,任何人都可以提出错误的建议。 displayed table

这是代码

<script type='text/javascript'>
function getXMLHTTPRequest() {
   var req =  false;
   try {
      /* for Firefox */
      req = new XMLHttpRequest(); 
   } catch (err) {
      try {
         /* for some versions of IE */
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err) {
         try {
            /* for some other versions of IE */
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (err) {
            req = false;
         }
     }
   }
   return req;
}
function updateToggle(obj,id){
    obj.style.backgroundColor = '#cc0000';
    var updReq = getXMLHTTPRequest();
    var url = 'updateToggle.php';
    var vars = 'id='+id+'&value='+obj.value;
    updReq.open('POST', url, true);
    updReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    updReq.onreadystatechange = function() {//Call a function when the state changes.
        if(updReq.readyState == 4 && updReq.status == 200) {
            if(updReq.responseText == 'N'){
                obj.style.backgroundColor = '#cc8800';
            }else{
                if(obj.value == '1'){
                    obj.value = '0';
                }else{
                    obj.value = '1';
                }
                obj.src = updReq.responseText;
                obj.style.backgroundColor = '#0000cc';
            }
        }
    }
    updReq.send(vars);
}
</script>



<?php include("e:\\wamp\\www\\IR_Timecapture\\protector\\password_protect.php"); ?>
<?php
include "db.inc.php";

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 15; 
$sql = "SELECT * FROM ircb ORDER BY id DESC LIMIT $start_from, 15";

$rs_result = mysql_query ($sql);
$num_rows = mysql_num_rows($rs_result);
$query = mysql_query("SELECT * FROM ircb"); 
$number=mysql_num_rows($query); 

print "<img src=\"backgrounds/untitled2.png\" border=\"0\"> </P>";


print "<table class=\"table1\"    STYLE=\"word-wrap:break-word;\"   width=1210 border=\"1\"  bordercolor=\"#000000\" bgcolor=\"E6E6E6\" style=\"border-collapse: collapse\"   cellpadding=\"2\" cellspacing=\"1\"> 


<tr>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Entry Date</b></font></td> 
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Staff</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Start Time</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>End Time</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Total Time</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Service</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Entry</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Comp</b></font></td>
<td  height=\"44px\"  background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Travel</b></font></td>
<td  height=\"44px\"   background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Info</b></font></td>
<td  height=\"44px\"   background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Edit</b></font></td>
<td  height=\"44px\"   background=backgrounds/marblish.jpg><font size=5 color=\"#FFFF00\"><b>Entered</b></font></td>

\n";


// Define $color=1
$color="1";

while($rows=mysql_fetch_array($rs_result))
{
    $id = $rows["id"];
    $enter = $rows ["enter"];


// If $color==1 table row color = #9B9D9F
    if($color==1){ ?>
       <tr bgcolor='#9B9D9F'>
           <td><?= $rows['cdate'] ?></td>
           <td><?= $rows['staff'] ?></td>
           <td><?= $rows['start'] ?></td>
           <td><?= $rows['end'] ?></td>
           <td><?= $rows['totalt'] ?></td>
           <td><?= $rows['service'] ?></td>
           <td><?= $rows['item'] ?></td>
           <td align="center">
               <img src="backgrounds/<?= $rows['mort'] ?>.png">
           </td>
           <td><?= $rows['travel'] ?></td>
           <td align="center">
               <a href="#" data-tooltip="<?= $rows['id'] ?>">
                   <img src="backgrounds/<?= $rows['cmtad'] ?>.png" border="0">
               </a>
           </td>
           <td align="center">
               <a href="edit_form.php?id=<?= $rows['id'] ?>">
                   <img src="backgrounds/edit1.png" border="0" align="center">
               </a>
           </td>
           <td align="center">
               <?php 
        if($enter == '1'){
            ?>
            <input type="image" onclick="updateToggle(this,'<?php echo $id;?>'); " src="backgrounds/bg/1.png" border="0" name="enter" value="0"/>
            <?php 
        }else{
            ?>
            <input type="image" onclick="updateToggle(this,'<?php echo $id;?>'); " src="backgrounds/bg/0.png" border="0" name="enter" value="1"/>
            <?php
        }
    ?>
           </td>
       </tr>

updateToggle php

<?php 

include "db.inc.php";

$id = $_POST['id'];
$enter=$_POST['enter'];
$value = $_POST['value'];
if(ctype_digit($id) && ctype_digit($value)){
    //mysql_connect();
    $Q = "UPDATE `xacall`.`ircb` SET enter='$value' WHERE `id` = '$id'";
    if(mysql_query($Q)){
        if($value == '1'){
            echo "status-toggle_enabled.png";
        }else{
            echo "status-toggle_disabled.png";
        }
    }else{
        echo 'N';
    }
}else{
    echo 'N';
}
?>

我尝试修改onClick以包含 location.reload();

<input type="image" onclick="updateToggle(this,'<?php echo $id;?>'); location.reload();" src="backgrounds/bg/1.png" border="0" name="enter" value="0"/>
                <?php 
            }else{
                ?>

这确实有效,但我想知道是否可以在没有刷新的情况下完成,这有点令人讨厌。 感谢

0 个答案:

没有答案