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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler