How to load a PHP page into a div with jQuery and AJAX?

后端 未结 6 606
名媛妹妹
名媛妹妹 2021-02-03 13:58

I am trying to write a function that will call getproduct.php?id=xxx when clicked. I can get the innerHTML portion to appear, but how do I also call t

6条回答
  •  无人共我
    2021-02-03 14:53

    Edit: the original question didn't reference jQuery. Leaving this answer here as others may find it useful.

    Here's how you would do this using the XHR object for an ajax request without jQuery or Prototype or other JS library.

    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById('digital_download').innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", 'getproduct.php?id=' + id,true);
        xmlhttp.send();
    }
    

提交回复
热议问题