The first script connects the file with js and displays the button and the counter:
<script src="script.js"></script> <input type="button" onclick="doWork" /> <div id="rate">0</div>
js file itself:
function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Ваш браузер не поддерживает AJAX."); return null; } } function setOutput(){ if (httpObject.readyState == 4){ document.getElementById('rate').innerHTML = httpObject.responseText; } } function doWork(){ httpObject = getHTTPObject(); if (httpObject != null) { httpObject.open("GET", "2.php?rate=" +document.getElementById('rate').innerHTML, true); httpObject.send(); httpObject.onreadystatechange = setOutput(); } } var httpObject = null;
and the zoom script itself:
echo ($_GET['rate']+1);
who knows js tell me what's wrong, because nothing works.
ps took from here: http://www.vr-online.ru/content/ajax-php-povyshaem-interaktivnost-1864
another question to improve the script: if I want to transfer parameters in the button, these changes will be:
<input type="button" onclick="doWork('rate','rate','rate')" />
where 1 parameter is the transmitted variable GET, 2nd - where to get the value, 3rd - where to write. And acc. js code:
function doWork(p1,p2,p3){ ... httpObject.open("GET", "2.php?"+p1+"="+document.getElementById(p2).innerHTML, true); ... httpObject.onreadystatechange = setOutput(p3); } function setOutput(p){ if (httpObject.readyState == 4){ document.getElementById(p).innerHTML = httpObject.responseText; }
Will this script be correct?