Home Ask Login Register

Developers Planet

Your answer is one click away!

MWasser February 2016

How to increase a php var without refreshing the page?

can somebody help me to solve this prob : I've a page width some div ID (Section of page #1 #2 ...) and a link (NEXT), how i can do to increase this link each time the user click on without refreshing the page ? sample if link was 1 .. link become 2 etc ..

Any ideas ?


tutankhamun February 2016

In the browser script: Listen onClick event on links and send AJAX-request to php-script when links is clicked.

In the server script: Update field on database or other storage.

arnuga3 February 2016

I would use an ajax for that just to send a request to php script what would generate a whole <a> tag and insert it in html. So onload of the page you set counter on 1 for example and everytime you click the 'next' you trigger onclick event (call a function on this event) increase your counter by 1 and send this value to server where script is generating for you <a href="value.php"></a> tag or something similar... add it to the html, so you can update your link in html without refreshing the page. On parallel you can delete, hide or show other elements if you need.

I hope that will give you some idea. I would tell you more if i would be able to see your code, so far it is as i see your situation. Sorry if i didn't get it right; )

div {
  display: block;
  width: 100px;
  height: 800px;
  background-color: red;
a {
  font-size: 3em;
  top: 0;
  <div id="section1">one</div>
  <div id="section2">two</div>
  <div id="section3">three</div>
  <a href="#section1" id="linkId"         onclick="increaseLink();">next</a>
var i = 1; 
function increaseLink() {
   var link = document.getElementById("linkId");
   link.innerHTML = "click";
   link.setAttribute('href', "#section"+ i);

   return false;

Is that what you need? You do not need php or ajax to achieve this.

Paul Dunlop February 2016

AJAX if you need to pull data from the server again on the click.

Standard JavaScript if you just want to do the number increment locally in the browser without anything fancy.

Post Status

Asked in February 2016
Viewed 1,756 times
Voted 11
Answered 3 times


Leave an answer

Quote of the day: live life