使用 Javascript 插入 php 包含

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11234215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:29:34  来源:igfitidea点击:

Insert php include with Javascript

phpjavascript

提问by DigitalKrony

I haven't found a clear answer for this at all. I've been looking for an hour or two.

我根本没有找到明确的答案。我已经找了一两个小时了。

I have a PHP page doing SQL call and building out my page. I have a list of items with onClick JavaScript functions that trigger the page's additional events. Among other things, I need that onClick function to append the HTML page with additional PHP code. I've been hopping to append in a PHP file via an include but that seems unavailable to me using this JS function.

我有一个 PHP 页面执行 SQL 调用并构建我的页面。我有一个包含触发页面附加事件的 onClick JavaScript 函数的项目列表。除其他外,我需要使用 onClick 函数为 HTML 页面添加额外的 PHP 代码。我一直希望通过 include 附加到 PHP 文件中,但使用此 JS 函数似乎无法使用。

And ideas would be most helpful. If you can support your answer with code references or existing tutorials that make since that would be fantastic.

想法将是最有帮助的。如果你能用代码参考或现有的教程来支持你的答案,那将是太棒了。

回答by Greg

JavaScript is code that executes in the client's browser, PHP is code that executes on the server. Attempting to output PHP from JavaScript will neverwork.

JavaScript 是在客户端浏览器中执行的代码,PHP 是在服务器上执行的代码。尝试从 JavaScript 输出 PHP永远不会成功

If you wish to trigger PHP code via JavaScript, you could make an XML Http Requestto a PHP file on your server.

如果您希望通过 JavaScript 触发 PHP 代码,您可以向服务器上的 PHP 文件发出XML Http 请求

回答by Jordizle

You can use JQuery+Ajax to load an external file and then just append the element, for example:

您可以使用 JQuery+Ajax 加载外部文件,然后只附加元素,例如:

// when the page loads
$(document).ready(function () {
    $.ajax({
      url: "test.html",
      cache: false
    }).done(function( html ) {
      $("#results").append(html);
    });
});

or

或者

// when the an element is clicked
$(document).ready(function () {
  $.fn.appendElement = function (){
    $.ajax({
      url: "test.html",
      cache: false
    }).done(function( html ) {
      $("#results").append(html);
    });
  }
});

$('#buttonId').click(function(){ $(this).appendElement(); });

HTH

HTH

回答by transparent

Using ajax is the solution. :) What ajax (Asynchronous JavaScript and XML) does is run an external php page and returns its results dynamically without reloading the page. :)

使用ajax是解决方案。:) ajax(异步 JavaScript 和 XML)所做的是运行外部 php 页面并动态返回其结果,而无需重新加载页面。:)

Of course another solution would be to have your html file in .php and just echo the php you want added.

当然,另一种解决方案是将您的 html 文件放在 .php 中,然后回显您想要添加的 php。

回答by Pedro L.

Use AJAX, with jQuery you could do this:

使用 AJAX,使用 jQuery 你可以这样做:

$.get('test.php', function(data) {
  $("#MyDiv").html(data);
});

This would insert "test.php" generated content inside <div id="MyDiv">.

这会在 .php 文件中插入“test.php”生成的内容<div id="MyDiv">

Ok. You don't want jQuery... The same above using plain javascript:

行。你不想要 jQuery ... 上面同样使用普通的 javascript:

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;  
}

function $(id) { return document.getElementById(id); }

function AJAX_GetAsinc(ajax, id, url) {
  ajax.abort();
  function statechange() {
    if ((ajax.readyState==4) && (ajax.status==200)) $(id).innerHTML=ajax.responseText;
  }
  ajax.open('GET', url, true);
  ajax.onreadystatechange = statechange;
  ajax.send(null);
}

var ajax = getHTTPObject();

AJAX_GetAsinc(ajax, 'MyDiv', 'test.php'); // This loads the content

Ohhh yes... this is less confusing :S

哦,是的......这不那么令人困惑:S