doupafu6980 2014-06-05 10:48
浏览 77
已采纳

使用Ajax调用表数据中的php文件时出错

I have small project in which i have different news.I have to include this news_all.php file in table data in the dashboard.php file. I have predefined root structure in which i cannot use include('news.php) and i dont want to use it. So in this case how i can be able to call news.php file in this table data in dashboard.php file.I have used Ajax method.I am almost done but having small mistake in my code.Any help.Thanks. Here is my code:

dashboard.php

   <?php
// -- REGISTER ERSTELLEN -------------------------------------------------------

$page['register-news'] = array(
    1   => array( 'News','aktiv',$page['script'],'',''),
);

$page['edit-register-news'] = array(
    1   => array( 'Edit-News','aktiv',$page['script'],'',''),
);


$page['content'] .= '

<table width="538" cellspacing="0" cellpadding="0" border="0" >
    <tr>
        <td id="News">
            <div>'.CreateRegister($page['register-news']).'</div>
            '.CreateMessage().'
            <div class="cont-liste-verlauf register">           


            </div>
        </td>
    </tr>
</table>';


?>

news_all.php

        <?php
include 'constant/const_system.inc.php';
include 'functions/ad_json.inc';
include 'functions/ad_formulare.inc';
$html = 
'<table width="538" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>

<a href="news.php?id=" class="TrackNews" id="01">
<div class="welcome-rahmen lng toggleNews" id="news_269_kurz">
<p class="welcome-breadcrump">Montag, 19.05.2014</p>
<p class="welcome-subheadline">Teilnahme von MAN Top Used an der Samoter 2014</p>
<div class="newsText">
<p class="welcome-text"><img src="http://intern.autodo.de/admin/news/man-it.jpg" width="165" class="text_fixed" border="0"></p>
<p class="welcome-text">Die 29. Internationale Erd- und Bautechnik-Ausstellung Samoter fand zwischen dem 8. und 11. Mai in Verona statt und zog rund 100.000 Besucher an. Samoter ist die wichtigste italienische Messe ihrer Art, die den Themen Erdbewegung, Hochbau und Baumaschinen gewidmet ist. Zugleich ist diese Veranstaltung damit auch f? europ?chen Markt bedeutsam.</p>
</div>
</div>
</a>

<a href="news.php?id=" class="TrackNews" id="02">
<div class="welcome-rahmen lng toggleNews" id="news_264_kurz">
<p class="welcome-breadcrump">Freitag, 24.01.2014</p>
<p class="welcome-subheadline">Kaufvertrag: neue Porsche-Vorlage zum Drucken!</p>
<div class="newsText">
<img src="http://intern.autodo.de/admin/news/porsche-kaufvertrag.jpg" border="0" align="right" class="img_fixed" width=60><p class="welcome-text">Ihr AMO Druckcenter bietet Ihnen ab sofort die M?chkeit, Kaufvertr? im Porsche-Design zu nutzen.</p>
<p class="page-breadcrump">AutoDo!-Team</p>
</div>
</div>
</a>
</td>
</tr>
</table>';
$return = array(
        'status' => 1,
        'html'  => $html
    );

    echo(json_encode($return)) ;

?>

ajax.js

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

    <script>
    $.ajax({
              type: "POST",
              url: "news_all.php",
              dataType : 'json',
              cache: false,
              data: {},
              success: function(data){
                 $('#news').html(data.html);
              }
            });

    </script>
  • 写回答

1条回答 默认 最新

  • duanchi19820419 2014-06-05 13:20
    关注

    You were almost there, 2 main things since you are using data.html

    • dataType : 'json' (in the AJAX call)
      • This tell your AJAX function to treat the data received as a JSON object allowing you to use data.html (see more jQuery.ajax() documentation)
    • echo(json_encode($return)) (in the news_all.php output)
      • This gives the output in JSON compatible format

    My Sample Codes below

    dashboard.php

    <table width="538" cellspacing="0" cellpadding="0" border="0" >
        <tr>
            <td id="news">
                <div>'.CreateRegister($page['register-news']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register">           
    
    
                </div>
            </td>
        </tr>
    </table>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    
    <script>
    $.ajax({
              type: "POST",
              url: "news_all.php",
              dataType : 'json',
              cache: false,
              data: {},
              success: function(data){
                 $('#news').html(data.html);
              }
            });
    
    </script>
    

    news_all.php

    <?php
    $html =' <table width="538" cellpadding="0" cellspacing="0" border="0">
        <tr>
        <td>    
        <a href="news.php?id=" class="TrackNews" id="01">
            <div class="welcome-rahmen lng toggleNews" id="news_269_kurz">
            <p class="welcome-breadcrump">Montag, 19.05.2014</p>
            <p class="welcome-subheadline">Teilnahme von MAN Top Used an der Samoter 2014</p>
            <div class="newsText">
            <p class="welcome-text"><img src="http://intern.autodo.de/admin/news/man-it.jpg" width="165" class="text_fixed" border="0"></p>
            <p class="welcome-text">Die 29. Internationale Erd- und Bautechnik-Ausstellung Samoter fand zwischen dem 8. und 11. Mai in Verona statt und zog rund 100.000 Besucher an. Samoter ist die wichtigste italienische Messe ihrer Art, die den Themen Erdbewegung, Hochbau und Baumaschinen gewidmet ist. Zugleich ist diese Veranstaltung damit auch f? europ?chen Markt bedeutsam.</p>
            </div>
            </div>
            </a>
    
            <a href="news.php?id=" class="TrackNews" id="02">
            <div class="welcome-rahmen lng toggleNews" id="news_264_kurz">
            <p class="welcome-breadcrump">Freitag, 24.01.2014</p>
            <p class="welcome-subheadline">Kaufvertrag: neue Porsche-Vorlage zum Drucken!</p>
            <div class="newsText">
            <img src="http://intern.autodo.de/admin/news/porsche-kaufvertrag.jpg" border="0" align="right" class="img_fixed" width=60><p class="welcome-text">Ihr AMO Druckcenter bietet Ihnen ab sofort die M?chkeit, Kaufvertr? im Porsche-Design zu nutzen.</p>
            <p class="page-breadcrump">AutoDo!-Team</p>
            </div>
            </div>
            </a>
    
            <a href="news.php?id=" class="TrackNews" id="03">
            <div class="welcome-rahmen lng toggleNews" id="news_265_kurz">
            <p class="welcome-breadcrump">Mittwoch, 15.01.2014</p>
            <p class="welcome-subheadline">AutoDo! Update: Die Eigenschaft -Unfallfahrzeug- wurde im AMO erweitert!</p>
            <div class="newsText">
            <p class="welcome-text">Ab sofort k?n Sie Fahrzeuge nach folgenden Kriterien kennzeichnen:</p>
            <p class="highlight"><b>? Unfallfahrzeug<br>
            ? Unfallfrei<br>
            ? Keine Angabe</b></p>
            <p class="page-breadcrump">AutoDo!-Team</p>
            </div>
            </div>
            </a>
        </td>
        </tr>
    </table>
    ';
    $return = array(
            'status' => 1,
            'html'  => $html
        );
    
        echo(json_encode($return)) ;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同