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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题