Json结果数据未正确显示

时间:2014-06-13 10:32:17

标签: php html json select

嗨,我有一个小项目,将显示过去4,6周的新闻。我创建了选择框,它会询问过去4,6周的新闻。然后用户从选择框中选择新闻,然后点击提交按钮,他就会得到结果。但我没有得到适当的输出甚至没有任何错误。默认情况下,我不知道为什么输出显示数据库中的数据。我的提交按钮甚至没有显示活动。我正在犯小错误,无法识别。感谢。

js file

$.ajax({
          type: "POST",
          url: "ajax/edit_news.php",
          dataType : 'json',
          cache: false,
          data: {'aktion' : 'edit-news'},
          success: function(data){
          $('#editnews').html(data.html);
          }
        });

这是我的代码:

<?php
chdir('..');
include 'constant/const_system.inc.php';
include 'functions/ad_json.inc';
include 'functions/ad_formulare.inc';
include 'constant/const_system_db.inc.php';  //database file

$return = array();
if(isset($_POST['BtnSubmit']))
    {

        if(($_POST['news'])==4){

            $sql=" SELECT DISTINCT ad_news_texte.headline, ad_news.datum_archiv
        FROM ad_news_texte
        INNER JOIN ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news
        INNER JOIN ad_news ON ad_news_oe.id_ad_news = ad_news.id
        WHERE ad_news.datum_archiv
        BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +28
        DAY AND curdate( )
        ";

            $sql_select=mysql_query($sql);
            while($row = mysql_fetch_array($sql_select)) {
                echo $row['headline'] . " " .$row['datum_archiv'] ;
                echo "<br>";
            }
        }
        if(($_POST['news'])==6){

            $sql=" SELECT DISTINCT ad_news_texte.headline, ad_news.datum_archiv
        FROM ad_news_texte
        INNER JOIN ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news
        INNER JOIN ad_news ON ad_news_oe.id_ad_news = ad_news.id
        WHERE ad_news.datum_archiv
        BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +42
        DAY AND curdate( )
        ";

            $sql_select=mysql_query($sql);
            while($row = mysql_fetch_array($sql_select)) {
                echo $row['headline'] . " " .$row['datum_archiv'] ;
                echo "<br>";
            }
        }
    }
if($param['aktion'] == 'edit-news')
    {
        $html = '                                           
                                                                <form name="UserInformationForm" method="POST" action="#">

                                                <select name="news">
                                                                <option value="4" '. (($_POST['news']=="4")  ?  "selected=selected" : "" ) .'>Show news from last 4 weeks</option>
                                                                <option value="6" '. (($_POST['news']=="6") ?  "selected=selected" : "") .'>Show news from last 6 weeks</option>
                                                </select>
                                                <br/><br/>
                                        <input name="BtnSubmit" type="submit" value="Submit" >
                                                                </form>             


                                ';
        $return = array(
                        'status' => 1,
                        'html'  => $html
                        );

        echo json_encode($return);
        die();
    }
?>

它的显示输出如下: enter image description here

3 个答案:

答案 0 :(得分:0)

您将返回与html混合的无效json,这就是为什么它不起作用,这是您需要更改的代码

//Change this in both places 
...
$news = '';
$sql_select=mysql_query($sql);
        while($row = mysql_fetch_array($sql_select)) {
            $news .= $row['headline'] . " " .$row['datum_archiv'] ;
            $news .= "<br>";
}
...

//and return news like below 

$return = array(
    'status' => 1,
    'news' = $news
 );
//Return the json 
header('Content-Type: application/json');
exit(json_encode($return));

答案 1 :(得分:0)

试试这个

$.ajax({
              type: "POST",
              url: "ajax/edit_news.php",
              dataType : 'html',
              cache: false,
              data: {aktion: 'edit-news'},
              success: function(data){
              $('#editnews').html(data.html);
              }
            });

并做回声html。不要jsonp>

喜欢echo $html;。这将解决您的问题

答案 2 :(得分:0)

在文件顶部添加以下行     标题(&#39;内容类型:application / json&#39;);