2017-03-08 90 views
0

我想制作一个按钮点击计数器。当你点击按钮时,它的文本会发生变化,当点击按钮时,我想写入数据库,但我无法管理它。PHP AJAX按钮点击计数器

<button type="button" class="btn btn-info" style="width:90%;" id="textChanger" onclick="counter;"><h4><i class="glyphicon glyphicon-earphone" aria-hidden="true"> </i> XXXX XXX XXX <h6>Show Number</h6></h4></button> 

document.getElementById("textChanger").onclick= function(){ 
     document.getElementById("textChanger").innerHTML="<h4><i class=\"glyphicon glyphicon-earphone\" aria-hidden=\"true\"> </i> <?php echo $nrTelefonAnunt ?> </h4>"; 
     } 

这是我管理的文本变化的代码,现在我可以做什么写入数据库,我尝试了一些方法,但没有工作

+0

假设DB是不是你的本地计算机上,你需要做一个远程调用,所以你可以做一些服务器端脚本。 – Ilion

回答

2

试试这个

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<button type="button" class="btn btn-info" style="width:90%;" id="textChanger" onclick="counter;">1</button> 
 
<script> 
 
document.getElementById("textChanger").onclick= function(){ 
 
     var count = document.getElementById("textChanger").innerHTML; 
 
     count = parseInt(count) + 1; 
 
     document.getElementById("textChanger").innerHTML=count; 
 
     } 
 
</script>

0

试试这个

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script> 
<button type="button" class="btn btn-info" style="width:90%;" id="textChanger" onclick="counter;"><h4><i class="glyphicon glyphicon-earphone" aria-hidden="true"> </i> <span>XXXXX</span> <h6>Show Number</h6></h4></button> 

<script> 
count=0; 
$(document).on("click","button",function(){ 
    count++; 
    $('span').text(count); 



    }); 
</script> 
2

谢谢大家对我manged加入这个答案:

$('#textChanger').click(function(){ 
    $.ajax({ 
     url : 'rate.php?idanunt="<?php echo $idAnunt ?>"', 
     type : 'post', 
     success : function(data){ 
     } 
    }); 
}); 
+0

写,你ajax需要这个问题。好 –