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?

    2 answers 2

    for nothing works.

    : D is a very informative description of the error ....

    1. Look in the javfascript console to check for errors.
    2. you need to write like this: httpObject.open ("GET", "2.php", true); httpObject.send ("rate =" + document.getElementById ('rate'). innerHTML);

    3

    httpObject.onreadystatechange = setOutput ();

    replaced by:

    httpObject.onreadystatechange = setOutput;

    1. in the setOutput function, httpobject must come in parameters.
    • 1) Opera errors console: nothing (: 2) corrected 3) removed brackets And what parameters should come? more? already under the implementation of paragraph 3 earned. - Antiless

    1) The purpose of the handler function "setOutput", must be specified before sending. Those. like this: httpObject.onreadystatechange = setOutput;
    httpObject.send ();

    2) httpObject.open ("GET", "2.php", true); 3rd parameter "true" sets the execution of an asynchronous AJAX request. Ie, after sending "httpObject.send ();" the script will continue its execution and will not wait for the AJAX responses.

    3) httpObject.onreadystatechange = setOutput; indicated without brackets, since here, the hash of the function is passed to the "onreadystatechange" property of the "httpObject" object to be called later. If you specify "setOutput ();" then this function will be immediately called and its result will be recorded in the "onreadystatechange" property.

    • But in general, how can I pass a parameter to setOutput? - Antiless
    • can. There are different approaches for this. But this is a topic for another question. - Alex Kapustin
    • I just created a whole function to perform all the actions, and setOutput is already going on as a subfunction - and pass the parameters. - Antiless
    • one
      If you need to pass parameters instead of: = setOutput; // necessary = function () {setOutput (p1, p2)} - Zowie
    • this is a bad option associated with global variables and references to them, you can easily get confused. It is better to use the so-called bandage of arguments. - Alex Kapustin