按钮计数未定义PHP AJAX JAVASCRIPT

时间:2014-05-03 14:06:14

标签: javascript php ajax button count

我有以下问题:

1 /我有这段代码:

的index.html:

<script type="text/javascript">
            function doOnOrientationChange()
            {
                switch(window.orientation) 
                {  
                    case -90:                 
                        document.getElementById("landscape").style.display="block";
                    break;
                    case 90:              
                        document.getElementById("landscape").style.display="block";                 
                    break;
                default:                   
                    document.getElementById("landscape").style.display="none";
                break;                            
                }
            }

            //Listen to orientation change
            window.addEventListener('orientationchange', doOnOrientationChange);  

            function getXMLHttp()
            {
                var xmlHttp
                try
                {
                    xmlHttp = new XMLHttpRequest();
                }
                catch(e)
                {
                try
                    {
                            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                catch(e)
                    {
                    try
                        {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                    catch(e)
                        {
                        alert("AJAX not supported.")
                        return false;
                        }
                    }
                }
            return xmlHttp;
            }

function MakeRequest()
{
   var xmlHttp = getXMLHttp();
   xmlHttp.onreadystatechange = function()
   {
     if(xmlHttp.readyState == 4)
     {
      HandleResponse(xmlHttp.countText);
     }
   }
   xmlHttp.open("GET", "compteur.php", true); 
   xmlHttp.send(null);
 }


 function HandleResponse(response)
 {
   document.getElementById('text').innerHTML = response;
  }
        function changement() {

        document.getElementById('bouton').style.display = 'none';
        var count= '<?php echo file_get_contents("compteur.txt");?>';
        var last =  count%10;
        var beforelast =  count%100;

        if ( count>9998 &&  count<99999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '160px';
            document.getElementById('text1').style.fontSize = '160px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if ( count>99998 &&  count<999999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '135px';
            document.getElementById('text1').style.fontSize = '135px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if ( count>999998 &&  count<9999999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '120px';
            document.getElementById('text1').style.fontSize = '120px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if ( count>9999998 &&  count<99999999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '110px';
            document.getElementById('text1').style.fontSize = '110px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if ( count>99999998 &&  count<999999999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '95px';
            document.getElementById('text1').style.fontSize = '95px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if ( count>999999998 &&  count<9999999999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '89px';
            document.getElementById('text1').style.fontSize = '89px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if ( count>9999999998 &&  count<99999999999){
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '85px';
            document.getElementById('text1').style.fontSize = '85px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if (last===0 && beforelast===10){
            document.getElementById("text1").innerHTML = "th";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if (last===0){
            document.getElementById("text1").innerHTML = "st";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if (last===1 && beforelast===11){
            document.getElementById("text1").innerHTML = "th";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if (last===1){
            document.getElementById("text1").innerHTML = "nd";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if (last===2 && beforelast===12){
            document.getElementById("text1").innerHTML = "th";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else if (last===2){
            document.getElementById("text1").innerHTML = "rd";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        else {
            document.getElementById("text1").innerHTML = "th";
            document.getElementById('text').style.display = 'block';
            document.getElementById('text1').style.display = 'block';
            document.getElementById('text').style.fontSize = '195px';
            document.getElementById('text1').style.fontSize = '195px';
            document.getElementById('textg').style.display = 'block';
            document.getElementById('textg1').style.display = 'block';
            document.getElementById('texth').style.display = 'block';
            document.getElementById('texth1').style.display = 'block';
            document.getElementById('v').style.display = 'none';
            document.getElementById('buttons').style.display = 'block';
            }
        }
</script>
<body>
 <span id="space" ><input onclick='MakeRequest();' onmouseup="changement()"       type="button" value="PUSH" id="bouton"/></span>
 </body>

compteur.php:

<?php

$clicks = file_get_contents("compteur.txt");
$clicks++;

$fp = fopen("compteur.txt", "w+");

while ( !flock($fp, LOCK_EX) ) {    
    usleep(500000); // Delay half a second
}

fwrite($fp, $clicks);
fclose($fp);
flock($fp, LOCK_UN);

 ?>

当我点击按钮时,一切似乎都有效。控制台中没有错误。 但不是向我显示点击次数,而是显示“未定义”。

感谢您的帮助。

TheMightyKnight

1 个答案:

答案 0 :(得分:1)

XMLHttpRequest个对象没有名为countText的属性。在MakeRequest中,您需要responseText代替:

HandleResponse(xmlHttp.responseText);
// Not `countText` ----^