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

标签 javascript php html

我有一个表格,里面有这个:

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

我希望在单击按钮时使用外部 PHP 文件中的内容填充 div。

<div id="admin-content">
<?php include ("view-records.php");?>
</div>

有办法做到这一点吗?

最佳答案

无法直接使用 PHP 执行此操作,因为 PHP 在生成页面时运行,而不是响应用户界面事件(当然,除非您加载新页面)。

阅读 Ajax 并让事情变得更容易一点,使用 jQuery,尽管还有其他库可用。

例如使用 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'
});

了解更多:http://api.jquery.com/jquery.get/

关于javascript - 如何使用 onclick 事件将外部 PHP 页面的内容包含到 DIV 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28087259/

相关文章:

php - 如何更改登录路径名称

php - 交响乐 5 : How to make Login redirect to different pages deppending on role

html - IOS Safari <select> 元素

javascript - Gulp Rev 不在我的 list 文件中包含路径

javascript - Action 必须是普通的对象,尽管看起来我的 Action 已经很普通了

php - 从已过期的行中删除图像

html - CSS 边距 : Why does this layout behave this way?

html - 如何使用 HtmlUnit 在另一个 div 中找到 div?

javascript - this.href 不返回 href

javascript - Angularjs 中的温度中位数