Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用onlick事件将外部PHP页面中的内容包含到DIV中?_Javascript_Php_Html - Fatal编程技术网

Javascript 如何使用onlick事件将外部PHP页面中的内容包含到DIV中?

Javascript 如何使用onlick事件将外部PHP页面中的内容包含到DIV中?,javascript,php,html,Javascript,Php,Html,我有一张表格,上面写着: <button type="button" name="viewRecords" id="viewRecords" class="activeButton" onClick= "<?php include ("") ?>" 无法直接使用PHP实现这一点,因为PHP在生成页面时运行,而不是响应用户界面事件(当然,除非加载新页面) 阅读Ajax,为了让事情变得更简单,可以使用jQuery,尽管还有其他库可用 e、 g.使用jquery/Ajax /

我有一张表格,上面写着:

<button type="button" name="viewRecords" id="viewRecords" class="activeButton" onClick= 
  "<?php include ("") ?>"

无法直接使用PHP实现这一点,因为PHP在生成页面时运行,而不是响应用户界面事件(当然,除非加载新页面)

阅读Ajax,为了让事情变得更简单,可以使用jQuery,尽管还有其他库可用

e、 g.使用jquery/Ajax

// Javascript

function success(data) {
    // Assuming you return raw HTML from the ajax call...
    $('#admin-content').html(data); // inserts the html into the div.
}

$.ajax({
  url: url,
  data: data, // URLencoded query string (google encodeURIComponent() if you don't know this)
  success: success,
  dataType: 'html'
});

阅读更多信息:

您可以使用jQuery的.post方法来实现这一点。确保将jQuery包含在网页的标题中

   $("#viewRecords").click(function() {
     $.post( "view-records.php", function( data ) {
        $( "#admin-content" ).html( data );
     });
   });

关于JQuery.load方法的更多信息。

或者,如果您希望避免使用其他人提供的JQuery解决方案(这也很好),那么您可以在纯JavaScript中获得相同的结果(只需将此代码放在
标记中的某个位置):


函数viewRecords(){
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“管理内容”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“view records.php”,true);
xmlhttp.send();
}
然后在按钮的onClick处理程序上调用该函数:

<button type="button" name="viewRecords" id="viewRecords" class="activeButton" onClick="viewRecords();" value="View Records" />


ajax可以做到这一点,或者您可以在页面加载上运行view records php,并添加视图记录将返回的任何内容。`onClick=“myJavascriptAjaxFunction(Arguments)”是的,我已经尝试了所有示例,但没有一个对我有效,我的代码中肯定有其他地方出错了。。有人能看出我哪里出了错吗?[admin page:()view-records.php()好的,我已经尝试了所有的示例,但没有一个对我有效,我一定是在代码的其他地方出错了。。有人能看出我哪里出了错吗?[管理页面:](jsfiddle.net/0d717s5h)view-records.php(jsfiddle.net/ghadzh7o)
<script type="text/javascript">
    function viewRecords() {
        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("admin-content").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "view-records.php", true);
        xmlhttp.send();
    }
</script>
<button type="button" name="viewRecords" id="viewRecords" class="activeButton" onClick="viewRecords();" value="View Records" />