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>";
}
3
  • 1
    Use an AJAX call to the PHP script Commented Dec 13, 2015 at 9:38
  • 2
    Possible duplicate of Call php function from javascript Commented Dec 13, 2015 at 9:48
  • please give me the code. i know i can use AJAX i just dont know how can i use it to call a PHP function. Commented Dec 13, 2015 at 10:02

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.