简体   繁体   English

尝试在单独的 JS 文件中调用 PHP 脚本,如何将输出显示到我的 HTML 页面上

[英]Trying to call a PHP script in separate JS file, how do i display the output onto my HTML page

First time trying this can anyone help me, i get an output on the console but have no idea how to display it in a tag.第一次尝试这个可以帮助我,我在控制台上得到一个输出,但不知道如何在标签中显示它。 This is the problem for both the sections of my code.这是我的代码的两个部分的问题。 Any help would be appreciated.任何帮助,将不胜感激。

const URL_GETOFFERS = 'getOffers.php';
const URL_GETOFFERS_XML = 'getOffers.php?useXML';

fetch(URL_GETOFFERS)
    .then(
        function (response) {
            return response.text();
        })
    .then(
        function (data) {
            console.log(data);
            document.getElementById("getHTMLOffer").innerHTML = "<p>" + this.responseText + "</p>";
        })
    .catch(
        function (err) {
            console.log("Something went wrong!", err);
        });

fetch(URL_GETOFFERS_XML)
    .then(
        function (response) {
            return response.text();
        })
    .then(
        function (data) {
            console.log(data);
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(data,"text/xml");
            xmlDoc.getElementsById("getXMLOffer").innerHTML = this.responseText;
        })
    .catch(
        function (err) {
            console.log("Something went wrong!", err);
        });
 });

If you want to put some content inside HTML elements you have to:如果您想在 HTML 元素中放置一些内容,您必须:

  • create the desired HTML elements创建所需的 HTML 元素
<div id="getHTMLOffer">initial content</div>
<div id="getXMLOffer">initial content</div>
  • get the elements in Javascript, after DOM has loaded在 DOM 加载后get Javascript 中的元素
  • change the contents of the elements改变元素的内容
    // the contents of this function is executed after the HTML structure (DOM) is loaded
    // aka `on ready`
    (function() { 
        document.getElementById("getHTMLOffer").innerHTML = "new content here";
        document.getElementById("getXMLOffer").innerHTML = "new content here";
    })();

Know that there are multiple ways to accomplish this.知道有多种方法可以实现这一点。 Above is shown just one of those ways.以上只是其中一种方式。

Also be aware the function getElementById gets only one element.另请注意,函数getElementById仅获取一个元素。 In your code there is a typo getElementsById (extra s character).在您的代码中有一个拼写错误getElementsById (额外s字符)。

A rudimentary example that sends a basic GET request with a querystring to the same page, processes th servers response and displays on the page将带有查询字符串的基本 GET 请求发送到同一页面、处理服务器响应并显示在页面上的基本示例

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['task'] ) ){
        ob_clean();
        echo 'Some text as respons to the FETCH request task = "'.$_GET['task'].'"';
        exit();
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>FETCH</title>
    </head>
    <body>
        <aside id='getHTMLOffer'></aside>
        <script>
            fetch( '?task=fetch&source=text' )
            .then( r=>{ return r.text() } )
            .then( data=>{
                document.getElementById('getHTMLOffer').innerHTML=data
            })
            .catch(err=>{ alert( err ) } )
        </script>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it? 如何调用单独的header.php? - How do I call onto a separate header.php? 与使用PHP一样,如何在HTML页面中运行Node.js脚本? - How do I run a Node.js script in my HTML page, as I do with PHP? 如何在HTML页面中显示单独的PHP文件的结果? - How to display the results from a separate PHP file in an HTML page? 如何在我的 php/html 输出中显示我的 mysql 表列标题? - How do I display my mysql table column headers in my php/html output? 如何通过php将sql查询的结果显示到php页面上? - How do I display results from a sql query via php onto a php page? 不知道如何使用ajax将html输出到页面上 - Do not know how to output html onto a page with ajax 在使用 AJAX 调用 PHP 脚本后,如何将其显示为 html 页面? - How do I display a PHP script as html page after calling it using AJAX? 如何使我的 PHP 脚本返回到原始 HTML 页面? - How do I make it so that my PHP script goes back to my original HTML page? 如何从php文件获取输出,以显示在调用它的wordpress页面上的div中? - How do I get output from php file to display in a div on the wordpress page that called it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM