Jquery,Ajax没有触发正确的php功能

时间:2014-06-19 08:07:08

标签: javascript php jquery html ajax

嗨,我有一个小项目,其中有2个选项卡,即新闻和编辑 - 新闻。当您打开页面时,默认选项卡将默认显示一些新闻。当用户切换到编辑 - 新闻标签时,他可以从选择框中选择他是否需要过去4周或6周的新闻。在选择和提交按钮上单击,他可以再次切换到包含已编辑新闻的新闻选项卡。现在我可以正确管理我的选项卡。我面临的唯一问题是当用户从选择框中选择新闻并提交按钮时,我无法触发save-news-widget功能,这将调用我的数据库查询部分来更新我的新闻。所以提交按钮后我也是触发get-news-widget功能,我默认新闻而不是编辑的新闻。谢谢。 这是我的代码:

dashboard.php

if($param['aktion'] == 'save-widget-news')
{
    //$param['news'] 
    //UPDATE SQL...
        $page['register-news'] = array(
        1   => array( 'News','aktiv',$page['script'],'',''),
        0   => array( 'Edit News','enabled',$page['script'],''),    
    );
    //echo $selectValue=$_POST['news'];
    if(isset($_POST['Save']))
    {
        if(($selectValue)==4){

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

}
 elseif(($selectValue)==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);

}

$html = '<table width="538" cellspacing="0" cellpadding="0" border="0">
        <tr>
            <td>
                <div>'.CreateRegister($page['register-news']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register"> ';
                while($row = mysql_fetch_array($sql_select)){       
                $news_result = $row['headline'] . " " .$row['datum_archiv'] ; 

                $html .= '<table id="news"> 
                        <div class="welcome-rahmen krz toggleNews">  

                       <p class="welcome-subheadline"><input type="hidden" name="type" value="'. $news_result .'" ></p> ';
                      }

                $html .= '</div></table>            
            </div>
        </td>
    </tr>
</table>';


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

    echo json_encode($return);
    die();
    }
    else{
    $param['aktion'] = 'get-widget-news';
}
}



if($param['aktion'] == 'get-widget-news')
{
    $newsId = 1;
    $page['register-news'] = array(
        1   => array( 'News','aktiv',$page['script'],''),
        0   => array( 'Edit-News','enabled',$page['script'],'',''), 
    );
        $param['aktion'] = 'save-widget-news';
        $html = '<table width="538" cellspacing="0" cellpadding="0" border="0" >
            <tr>
                <td>
                <div>'.CreateRegister($page['register-news']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register">               
                <table id="news">
<div class="welcome-rahmen lng toggleNews" id="news_269_kurz">
<a href="news.php?id=269" class="TrackNews" id="01" target="_blank">
<p class="welcome-breadcrump">Montag, 19.05.2014</p>
<p class="welcome-subheadline">Teilnahme von MAN Top Used an der Samoter 2014</p>
</a>
</div>

<div class="welcome-rahmen lng toggleNews" id="news_264_kurz">
<a href="news.php?id=264" class="TrackNews" id="02" target="_blank">
<p class="welcome-breadcrump">Freitag, 24.01.2014</p>
<p class="welcome-subheadline">Kaufvertrag: neue Porsche-Vorlage zum Drucken!</p>
</a>
</div>


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

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

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

if($param['aktion'] == 'get-widget-news-edit')
{


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

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

                            <form name="UserInformationForm" method="POST" >
                            <table id="news">

                        <select name="news" id="news">
                                <option value="4">Show news from last 4 weeks</option>
                    <option value="6">Show news from last 6 weeks</option>                              
                        </select>
                        <br/><br/>

                            </table>
                            '.CreateButton($page['button']).'
                            </form> 

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

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

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

dashboard.js

$(document).ready(function(){

    function saveNewsWidget()
    {
        var selectBoxValue = $('select[name="news"]').val();
        $.ajax({
        type: "POST",
        url: "ajax/dashboard.php",
        dataType : 'json',
        cache: false,
        data: {'aktion' : 'save-widget-news', 'news' : selectBoxValue},
        success: function(data)
        {
            //getNewsWidget();
            $('#news').html(data['html']);

        }
      });
  }

    function getNewsWidget()
    {
        $.ajax({
        type: "POST",
        url: "ajax/dashboard.php",
        dataType : 'json',
        cache: false,
        data: {'aktion' : 'get-widget-news'},
        success: function(data){
            //alert(data);
            $('#news').html(data['html']);
        },
        error: function(data){
            alert('error');
            //$('#news').html(data.html);
        }
      });
  }

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

        }
      });
  }

    $('#news .butt-rahmen').live('click', function(){

            if($(this).attr('id') == 'saveId')
            {
                saveNewsWidget();

            } 
                return true;


    });

    getFahrzeugeWidget();
    getNewsWidget();

});

3 个答案:

答案 0 :(得分:1)

在我看来,您正在提交页面以及运行ajax调用。你需要像这样停止提交页面......

$('#news .butt-rahmen').live('click', function(e) {

    e.preventDefault(); // this will stop the form being submitted and reloading the page

    if($(this).attr('id') == 'saveId') {
        saveNewsWidget();
    } 
});

注意e也作为函数参数传递。

此外,我假设您使用的是旧版本的jQuery,因为live()已被弃用并替换为on()。如果您使用的是较新版本,那么您也需要更改它。对于你所拥有的,只需将live更改为on

答案 1 :(得分:1)

如果我没错 请检查你的mysql连接是否正确。如果合适,你可以检查 你是否进入if循环之一,即     如果(($ selectValue)== 4) 要么     如果(($ selectValue)== 6)

如果你的代码没有进入任何一个,如果循环查询可能没有执行,因此结果没有生成。

答案 2 :(得分:1)

您可以尝试将连接变量()传递给mysql_query()函数     $ sql_select中=的mysql_query($ SQL,$连接);

您也可以尝试在代码中的一行中进行查询。