更改输入背景图像onclick

时间:2017-01-27 16:18:47

标签: javascript php jquery html

我有一个页面(resto演示文稿)和一个蓝色img(如图标)。

当我点击like_icon时,如果用户从未投票支持此resto,我想要: - 增加投票(以整数形式保存到mysql数据库中); - 在页面中显示新值(无刷新页面); - 更改类似图标(红色图标,始终可点击);

如果用户已投票,则点击图标: - 减少投票(更新恢复表); - 在页面中显示新值(无刷新页面); - 将like图标更改为蓝色img(始终可点击)。

这是我尝试过的,但不能正常工作。怎么了?我怎么能这样做?

谢谢。

HTML:

...
...
 <?php include 'test.php'; ?>

<div id="idlike"><?php echo $idlike[0]; ?></div>

<label for="like">
    <img id="likeimg" src="/img/like.png" style="cursor: pointer;" />
<script>
 var x = document.getElementById("idlike").value;

 if (x > 0) {
  $("#likeimg").attr('src', '/img/dislike.png');
  //document.getElementById("likeimg").innerHTML = <img scr="/img/dislike.png">;
 }
 else {
  $("#likeimg").attr('src', '/img/like.png');
  //document.getElementById("likeimg").innerHTML = <img scr="/img/like.png">;
 }
</script>
</label>

<div id="likeimg"></div>

<form method="POST" action="resto-test.php?id=1">
    <input type="submit" id="vote" name="vote" value="" style="display: none;" />
</form>

<script>
$("#likeimg").click(function(){
    $("#vote").click();
});
</script>
...
...

test.php的:

<?php
 //date_default_timezone_set("Europe/Bucharest");
 require('login/dbconfig.php');
 session_start();

 $login_session = $_SESSION['login_user'];
 $idresto = 1;

 $query = "SELECT id FROM users WHERE username = '$login_session'";
 $result = mysqli_query($dbconfig, $query);
 $iduser = mysqli_fetch_array($result);

 //$query = "SELECT id FROM likes WHERE id_user = (SELECT id FROM users WHERE username = '$login_session')";
 $query = "SELECT id FROM likes WHERE id_user = $iduser[0]";
 $result = mysqli_query($dbconfig, $query);
 $idlike = mysqli_fetch_array($result);

 //echo "<div id='idlike'>";
 //if ($idlike[0]) {
  //echo $idlike[0];
 //}
 //else {
  //echo "0";
 //}
 //echo "</div>";

 $query = "SELECT COUNT(id_resto) FROM likes WHERE id_resto = $idresto";
 $result = mysqli_query($dbconfig, $query);
 $nrl = mysqli_fetch_array($result);

 if (isset($_POST['vote'])) {
   if (!$idlike[0]) {
     //echo "<div id='likes'><img id='likeimg' src='/img/like.png'>";
     //echo "<div id='likes'><img id='likeimg' src='/img/like.png'>";

     $query = "INSERT INTO likes (id, id_user, id_resto, id_fel, id_gr) VALUES (default, $iduser[0], $idresto, 0, 0)";
     $result = mysqli_query($dbconfig, $query);

     $query = "UPDATE restos SET likes = ($nrl[0]+1) WHERE id = $idresto";
     $result = mysqli_query($dbconfig, $query);

     //echo "<span id='likenr'>&nbsp;&nbsp;" . ($nrl[0] + 1). "</span></div>";
     echo "<span id='likenr'>&nbsp;&nbsp;" . ($nrl[0] + 1). "</span>";
     $img = '/img/like.png';
   }

   else {
     //echo "<div id='likes'><img id='likeimg' src='/img/dislike.png'>";

     $query = "DELETE FROM likes WHERE id = $idlike[0]";
     $result = mysqli_query($dbconfig, $query);

     $query = "UPDATE restos SET likes = ($nrl[0]-1) WHERE id = $idresto";
     $result = mysqli_query($dbconfig, $query);

     //echo "<span id='likenr'>&nbsp;&nbsp;" . ($nrl[0] - 1). "</span></div>";
     echo "<span id='likenr'>&nbsp;&nbsp;" . ($nrl[0] - 1). "</span>";
     $img = '/img/dislike.png';
   }
 }
?>

0 个答案:

没有答案