dongnuo3749 2013-02-05 15:44
浏览 64

尝试使用javascript <script>标签来调用php页面并回显信息

I am trying to pass in a tag

<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>

I am trying to pass in the RSS Feed that is parsed through the src:

$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;

When you put that code on the html page it will not display any information, however when you go to the page it self it will return the rss feeds.

I have tried using the document.write function and echo.

Any body have any suggestions?

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

if you use the RSS as echo statements instead of document.write it will output:

<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>

The link to the rss feed using document.write is:

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

link to the rss feed using php echo:

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;num=5&amp;targ=y&amp;utf=y

link to the html page:

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

  • 写回答

1条回答 默认 最新

  • doucuodan0897 2013-02-05 16:24
    关注

    AJAX asks the server for the content of the URL that you provide it. In my example, my_page.php is the URL that I'm asking for a response from.

    Your web file structure should look similar to this:

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

    If you were to type in http://mywebsite.com/my_page.php, this initiates a GET request, and you would receive the same response from your browser as you would from the proceeding ajax call:

    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();
    

    Read the comments in the above code.

    I would definitely suggest using a library for your ajax calls, or at least wrap the above code in it's own function.

    This code is referenced from http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP