-3

jQuery では、Ajax ライブラリを簡単に使用できます。問題は、コード内で一度しか必要としないため、JavaScript ライブラリ全体を呼び出す必要がないことです。

私が取り組んでいるこのコードがありますが、それを機能させることができないようです。ボタンをクリックして PHP スクリプトを開始しようとしています。それ、どうやったら出来るの?

これが私が今どれくらい離れているかです:

<div onClick="count();">Click!</div>
<script>
    function count() {
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest()
        } else {
            if (window.ActiveXObject) {
                var xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        xhr.open('GET', 'count.php', false);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                while (results.hasChildNodes()) {
                    results.removeChild(results.lastChild);
                }
                results.appendChild(document.createTextNode(xhr.responseText));
            }
        }
        xhr.send();

    }, false);
    }
</script>

PHP ファイル内のコードは次のとおりです。

<?php
mysql_connect("myhost", "username", "password") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());

mysql_query("INSERT INTO `table` (`field`) VALUES(\'+1\'); ") 
or die(mysql_error()); 
?>
4

2 に答える 2

0

非常に短い方法。

HTML :

<input type="button" onclick="count()" value="count">

JS :

function count()  { 
  url="yourphp.php";
  objX=new XMLHttpRequest();
  objX.open("GET",url,false);
  objX.send(null);
}
于 2012-12-13T10:02:31.303 に答える
0
<script>
function count() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest()
    } else {
        if (window.ActiveXObject) {
            var xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    xhr.open('GET', 'count.php', false);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            while (results.hasChildNodes()) {
                results.removeChild(results.lastChild);
            }
            results.appendChild(document.createTextNode(xhr.responseText));
        }
    }
    xhr.send();

}, false); // This Line looks extra. Remove this and try
}
</script>
于 2012-06-28T06:55:25.130 に答える