【问题标题】:trying to use javascript <script> tag to call a php page and echo information尝试使用 javascript <script> 标签调用 php 页面并回显信息
【发布时间】:2013-02-05 15:44:40
【问题描述】:

我正在尝试传递标签

<script src="http://news.agc.org/agccorner/rss_feed.php?src=http%3A%2F%2Fnews.agc.org%2Fagccorner%2Frss_feed.php%3Ffdurls%255B%255D%3D4%26fdurls%255B%255D%3D5&amp;num=5&amp;targ=y&amp;utf=y" type="text/javascript"></script>

我正在尝试传入通过 src 解析的 RSS Feed:

$str ='<script type="text/javascript">';
   $str .= "document.write('<ul>');";
   //each rss feed is gone through
   foreach($rss_array as $each_rss) {

       foreach($each_rss as $item){
            //$num=1; 
            // if you want to channel title shown uncomment this
            /*
                if(@$item['title'])
                echo "Channel Title: " . $item['title'];
            */
            foreach($item as $record) {

                //this statement is to not display the array that contains the title
                if($record['title'] == "A") {

                }else {
                    if($num <= $num_limit) {
                    $num++;
                         $str .= "document.write('<li> <a href='".$record['link']. "' target='".$new_target."'>" . $record['title']. "</a></li>');";
                    }
                }
            }
       }
   } 
   $str .= "document.write('</ul>');";
   $str .= '</script>';

header("Content-type: application/x-javascript"); 

echo $str;

当您将该代码放在 html 页面上时,它不会显示任何信息,但是当您转到它自己的页面时,它将返回 rss 提要。

我尝试过使用 document.write 函数和 echo。

任何机构有什么建议吗?

&lt;script type="text/javascript"&gt;document.write('&lt;ul&gt;');document.write('&lt;li&gt; &lt;a href='http://news.agc.org/2013/01/28/house-ti-committee-finalizes-chair-assignments-chairman-shuster-meets-chairman-boxer-to-discuss-highway-trust-fund-concerns/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=house-ti-committee-finalizes-chair-assignments-chairman-shuster-meets-chairman-boxer-to-discuss-highway-trust-fund-concerns' target='_blank'&gt;House T&amp;I Committee Finalizes Chair Assignments, Chairman Shuster Meets Chairman Boxer to Discuss Highway Trust Fund Concerns&lt;/a&gt;&lt;/li&gt;');document.write('&lt;li&gt; &lt;a href='http://news.agc.org/2013/01/25/states-look-to-increase-transportation-funding/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=states-look-to-increase-transportation-funding' target='_blank'&gt;States Look to Increase Transportation Funding&lt;/a&gt;&lt;/li&gt;');document.write('&lt;li&gt; &lt;a href='http://news.agc.org/2013/01/25/nebraska-approves-keystone-xl-pipeline-route/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=nebraska-approves-keystone-xl-pipeline-route' target='_blank'&gt;Nebraska Approves Keystone XL Pipeline Route&lt;/a&gt;&lt;/li&gt;');document.write('&lt;li&gt; &lt;a href='http://news.agc.org/2013/01/24/house-ti-committee-finalizes-chair-assignments/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=house-ti-committee-finalizes-chair-assignments' target='_blank'&gt;House Transportation and Infrastructure Committee Finalizes Chair Assignments&lt;/a&gt;&lt;/li&gt;');document.write('&lt;li&gt; &lt;a href='http://news.agc.org/2013/01/24/states-look-to-increase-transportation-funding-2/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=states-look-to-increase-transportation-funding-2' target='_blank'&gt;States Look to Increase Transportation Funding&lt;/a&gt;&lt;/li&gt;');document.write('&lt;/ul&gt;');&lt;/script&gt;在php页面上

如果您使用 RSS 作为 echo 语句而不是 document.write,它将输出:

<ul><li> <a href='http://news.agc.org/2013/01/02/worker-shortage-in-construction-industry/?utm_source=rss&utm_medium=rss&utm_campaign=worker-shortage-in-construction-industry' target='_blank'>Worker Shortage in Construction Industry</a></li><li> <a href='http://news.agc.org/2012/12/18/15125/?utm_source=rss&utm_medium=rss&utm_campaign=15125' target='_blank'>Contractors Collaborate, Develop Guide to Streamline Development of Environmental Management Program</a></li><li> <a href='http://news.agc.org/2012/12/18/billing-index-continues-to-see-improvement/?utm_source=rss&utm_medium=rss&utm_campaign=billing-index-continues-to-see-improvement' target='_blank'>Billing Index Continues to See Improvement</a></li><li> <a href='http://news.agc.org/2012/12/12/new-changes-in-epa-policy-governing-disposal-of-pcb-contaminated-building-materials/?utm_source=rss&utm_medium=rss&utm_campaign=new-changes-in-epa-policy-governing-disposal-of-pcb-contaminated-building-materials' target='_blank'>New Changes in EPA Policy Governing Disposal of PCB-Contaminated Building Materials</a></li><li> <a href='http://news.agc.org/2012/12/04/consensusdocs-releases-updated-federal-subcontract-to-help-construction-firms-perform-federal-work/?utm_source=rss&utm_medium=rss&utm_campaign=consensusdocs-releases-updated-federal-subcontract-to-help-construction-firms-perform-federal-work' target='_blank'>ConsensusDocs Releases Updated Federal Subcontract to Help Construction Firms Perform Federal Work</a></li></ul>

使用 document.write 的 RSS 提要的链接是:

http://news.agc.org/agccorner/rss_feed.php?src=http%3A%2F%2Fnews.agc.org%2Fagccorner%2Frss_feed.php%3Ffdurls%255B%255D%3D4%26fdurls%255B%255D%3D5&amp;amp;num=5&amp;amp;targ=y&amp;amp;utf=y

使用 php echo 链接到 rss 提要:

http://news.agc.org/agccorner/rss_feed_2.php?src=http%3A%2F%2Fnews.agc.org%2Fagccorner%2Frss_feed.php%3Ffdurls%255B%255D%3D4%26fdurls%255B%255D%3D5&amp;amp;num=5&amp;amp;targ=y&amp;amp;utf=y

链接到html页面:

http://news.agc.org/agccorner/rss.htm

【问题讨论】:

  • 好像你想用 AJAX 加载 PHP 文件。
  • 我建议使用更好的解决方案,例如 AJAX,但无论哪种方式,它的输出是什么?即来源?
  • 如果您发送的内容类型为x-javascript,则不要包含&lt;script&gt; 标签。
  • &lt;script&gt; 标签是多余的。文件必须只包含 javascript。 &lt;script&gt; - 是 html。如果 php 创建的 js-code 是正确的,那么它必须能够正常工作。

标签: php javascript html rss


【解决方案1】:

AJAX 向服务器询问您提供的 URL 的内容。在我的示例中,my_page.php 是我请求响应的 URL。

您的网络文件结构应该类似于:

/var/www/public/index.php
/var/www/public/my_page.php

如果您要输入http://mywebsite.com/my_page.php,这将启动一个 GET 请求,并且您会从浏览器收到与正在进行的 ajax 调用相同的响应:

var xmlhttp;
if(window.XMLHttpRequest){
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
} else {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function readyStateChange(){
  if(xmlhttp.readyState==4 && xmlhttp.status==200){
    // This is the important part.
    // The xmlhttp.responseText is the text that is responded
    // from the web server.

    // This assumes that the document (DOM) is loaded.
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","my_page.php",true);
xmlhttp.send();

阅读上面代码中的cmets。

我肯定会建议为您的 ajax 调用使用一个库,或者至少将上述代码包装在它自己的函数中。

此代码引用自http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2012-01-08
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    相关资源
    最近更新 更多