0

i want to make a clickable element. every time the user is clicking on that element the server is running the following php function without reloading the page with a $_GET value:

function addOne() {
$num = file_get_contents("number.txt");
$fp = fopen('number.txt', 'w');
fwrite($fp, $num + 1);
fclose($fp);
echo "<script>document.getElementById('number').innerHTML = " . $num ."</script>";
}
shani dahan
  • 37
  • 1
  • 9

1 Answers1

1

If you create file called addOne.php which calls the addOne function and give your button an id of add-one, then you can use jQuery to post to it on every click.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $('#add-one).on('click', function() {
    $.post('/addOne.php');
  });
});
</script>
wogsland
  • 9,106
  • 19
  • 57
  • 93