0

This is the code of a simply button.

<input value="Download" type="button" onclick=" inccount();" /> 

And here this is what inccount(); does:

function inccount() {
 var a = parseInt(document.getElementById("num").value);
 document.getElementById("num").value = a + 1;
}

As you can see, this function increments by 1 the number I have on the input field num. I'd like to save this number on my server, but if I call a php script with the <form> tag, the page changes.

I want save the content of count on my server without leaving the current page. Who could I do it?

I wrote this simply PHP script for save the content of the input field (that is called num):

<?php
$file = '/dat/count.txt';
$current = $_GET["num"];
file_put_contents($file, $current);
?>
1
  • If you need not refresh the page and submit a value to the backend script, you can use ajax call. Commented Aug 26, 2013 at 1:11

2 Answers 2

1

To update a page without leaving it you need to investigate Ajax. From the linked page:

in the AJAX model, web applications are able to make quick, incremental updates to the user interface without reloading the entire browser page.

The Ajax call can be to a PHP script that writes to a text file.

Sign up to request clarification or add additional context in comments.

Comments

0

You are Posting the Page to the Server. So, the Page will have changes..

You need to use AJAX request from javascript to the Server.

simply with JQuery

$.ajax(
{
    url:'/?num=' + $('#num').val(),
    type: 'GET',
    success: function(response){
        alert('Success');
    },
    error: function(e1,e2,e3){
        alert('Error occured, Could not able to make request.');
    }

});

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.