0

画像をクリックしてカウントが増加している場合にのみ、画像を更新するために使用できる方法はありますか? ページを更新すると、クリック カウンターを 0 から再度カウントする必要があるため、ページ全体を更新したくありません。

<?php

include("connection.php");

$table1=mysql_query("select * from pic_a");
$table2=mysql_query("select * from pic_b");
$table3=mysql_query("select * from pic_c");

$tab1[0]=0;
$tab2[0]=0;
$tab3[0]=0;

$i=0;
while($row=mysql_fetch_assoc($table1))
{
    $tab1[$i]= $row['pic_link']; 
    $i++;
}

$i=0;
while($row=mysql_fetch_assoc($table2))
{
    $tab2[$i]= $row['pic_link']; 
    $i++;
}

$i=0;
while($row=mysql_fetch_assoc($table3))
{
    $tab3[$i]= $row['pic_link']; 
    $i++;
}

    $rand1=rand(0,9);
$rand2=rand(0,9);
$rand3=rand(0,9);
?>

<script type="text/javascript">

        var count = 0;
function countClicks() {
         count = count + 1;
            document.getElementById("p2").innerHTML = count;
        }

<table  border="1">
          <tr id="bird" onclick="countClicks();">
            <td><img src="password\<?php echo $tab1[$rand1]; ?>" alt="" width="76" height="67"></td>
            <td>Bird</td>
          </tr>
          <tr id="car" onclick="countClicks();">
            <td><img src="password\<?php echo $tab2[$rand2]; ?>" alt="" width="76" height="67"></td>
            <td>Car</td>
          </tr>
          <tr id="computer" onclick="countClicks();">
            <td><img src="password\<?php echo $tab3[$rand3]; ?>" alt="" width="76" height="67"></td>
            <td>Computer</td>
          </tr>

</table>
4

2 に答える 2

0

onclickのイベントはありません<img>

あなたはそれを中に包む必要があります<a>

次に関数を呼び出しonclick、js変数を使用してクリック数を追跡できます

于 2013-01-07T11:55:45.030 に答える
0

画像をクリックすると、いつでもAJAXを使用してカウンターを更新できます。(データベースに保存したい場合)

jQuery AJAXは、開始するのに最も簡単で迅速です。

そうしないと

id="cnt"でページに1つの要素を作成します

function countClicks()
{
   document.getElementById("cnt").value = Number(document.getElementById("cnt").value) + 1;
}
于 2013-01-07T11:55:57.500 に答える