click counter in php using jquery

泄露秘密 提交于 2020-01-03 04:45:06

问题


I am new to JQuery and trying to create a counter which counts the number of clicks and updates the counter in the database by one. I have created a button, on click of that i am sending the counter's value to the database. and trying to get the updated count at my first page.

my code is -

<!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=utf-8" />
<title>Implementing counter</title>
</head>

<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<? session_start()?>
<? ob_start()?>
<? 
if($_SERVER['HTTP_HOST']=='localhost'){
$host="localhost";

$user="root";

$pass="";

$dbas="db_name";

mysql_connect($host,$user,$pass,$dbas);
mysql_select_db($dbas);
}
?> 


<script>
$(document).ready(function(){
$("#count").click(function(){
saveData();
$("#counter").load();
});

function saveData(){  
$.ajax({
       type: "POST",
   url: "counter.php",
   data: { count: "1"}
    })
    .done(function( count ) {
    alert( "Counter Plus: " + count );
    })
    .fail(function() {
    alert( "error" );
    });
}
});
</script>
<br />
<button id="count">Add Counter</button><br>
<div id="counter">
<?
$fetch1=mysql_query("select `count` from user");
$fetch2=mysql_fetch_array($fetch1);
$counter=$fetch2['count'];
echo "You have ".$counter." Clicks";
?>
</div><br><br>

</body>
</html>

My counter.php looks like -

<!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=utf-8" />
<title>Counter</title>
</head>

<body>

<? session_start()?>
<? ob_start()?>
<? 
if($_SERVER['HTTP_HOST']=='localhost'){
$host="localhost";

$user="root";

$pass="";

$dbas="db_name";

mysql_connect($host,$user,$pass,$dbas);
mysql_select_db($dbas);
}
?> 


<?
if (isset($_POST['count'])) { // Form has been submitted.
    $count = $_POST['count'];
$n=0;
$fetch1=mysql_query("select `count` from user");
$fetch2=mysql_fetch_array($fetch1);
$n1=$fetch2['count'];
$n=$n1+$count;
// INSERT THE DATA 
$query = "UPDATE user SET `count`='{$n}'";
// Confirm if the query is successful.
$result = mysql_query($query);
echo "Counter Updated by 1";
}
?>


</body>
</html>

when i click the button, the counter.php is called, counter is updated, but my < div > is not updated. the counter.php page is shown as a dialog over my screen.

My first page & when i click the button, it looks like this -

Tell me what is going wrong??


回答1:


Remove HTML Tag's FROM counter.php

<? session_start()?>
<? ob_start()?>
<? 
if($_SERVER['HTTP_HOST']=='localhost'){
$host="localhost";

$user="root";

$pass="";

$dbas="db_name";

mysql_connect($host,$user,$pass,$dbas);
mysql_select_db($dbas);
}
?> 


<?
if (isset($_POST['count'])) { // Form has been submitted.
    $count = $_POST['count'];
$n=0;
$fetch1=mysql_query("select `count` from user");
$fetch2=mysql_fetch_array($fetch1);
$n1=$fetch2['count'];
$n=$n1+$count;
// INSERT THE DATA 
$query = "UPDATE user SET `count`='{$n}'";
// Confirm if the query is successful.
$result = mysql_query($query);
echo $n;
}
?>

AND replace ajax code with below

function saveData(){  
$.ajax({
       type: "POST",
   url: "counter.php",
   data: { count: "1"}
    })
    .done(function( count ) {
   $("#counter").html( "Counter Plus: " + count );
    })
    .fail(function() {
   // alert( "error" );
    });
}
});



回答2:


you are using alert !! use .html() find and try this!!

   //alert( "Counter Plus: " + count );
    $("#counter").html( "Counter Plus: " + count );



回答3:


You can change div content at success

function saveData(){  
  $.ajax({
    type: "POST",
    url: "counter.php",
    data: { count: "1"},
    success: function(data) {
      $('#div').html(data);
    },
    error: function(error) {
      alert(error);
    }
  });
}



回答4:


You need to return total count from your counter.php not the whole html markup

Your counter.php should output

Total count = 25

then in your jquery .done callback put this instead of alert

$("#counter").html(count);



回答5:


from your counter.php you are returning some string... do like this..

counter.php

    $query = "UPDATE user SET `count`='{$n}'";
    $result = mysql_query($query);
    echo $n; //here $n has updated count value

in jquery

   success : function (count)
   { 
     $("#counter").html("you have "+count+" clicks");
   }

hope will help you




回答6:


What you're doing is you are using javascript function 'alert', hence the dialog box appears whenever you press the Add Counter button. Change your code to following:

function saveData(){  
   $.ajax({
      type: "POST",
      url: "counter.php",
      data: "count=1",
      success: function(data)
      {
          $('#counter').html(data);
      },
      error: function()
      {
          //alert("error");
      }
  });

});

Also, exclude all the HTML tags from the counter.php page and viola!



来源:https://stackoverflow.com/questions/20421159/click-counter-in-php-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!