0

Hi i have small project in which i have 2 tabs i.e News and Edit-News. News tab will show some news by default when you open a page. When user switch to Edit-News tab he can select from selectbox whether he wants news from last 4 weeks or 6 weeks. And on select and on submit button click he can again switch to News tab with the edited news.Till now i am able to manage my tabs properly. The only problem i am facing here is when user select news from select-box and submit button i am not able to trigger save-news-widget function which will call my database query part to upadte my news.So after submit button also i am triggering to get-news-widget function and i am getting by default news not the edited news.Thanks. Here is my code:

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();

});
0

3 Answers 3

1

It looks to me like you're submitting the page as well as running the ajax call. You need to stop the page submitting like this...

$('#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();
    } 
});

Notice the e being passed as a function parameter as well.

Also, I'm assuming you're using an older version of jQuery since live() has been deprecated and replaced with on(). If you are using a newer version then you'll need to change that too. For what you have there, literally just change live to on.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi its not problem acutally.Yes its depriciated but its working fine for me here and now i am able to call edit-news on submit button but i am getting mysql_fetch_array(): supplied argument is not a valid MySQL result resource in while loop ...But i ran my query in mysql and it was running properly. Can you please tell me what i am doing wrong in my query part ?
That's a separate question for php people - I can't help with that, I'm afraid. Just create another question for that and you'll get relevant help. You've also removed the code from the question that shows what this answer fixed. I'll roll that back - please don't do that as this site is for everyone who visits, not just the question asker. It's not like a forum :)
1

If I am not wrong Please check your mysql connection is proper. If it is proper you can check whether you are going in one of if loop i.e if(($selectValue)==4) or if(($selectValue)==6)

If your code is not going inside any of this if loop query might not executing hence result not generated.

1 Comment

yes my connection is proper i have just checked it and its going inside loop also i guess that's why its giving error ? And i am geeting multiple rows from dataabse so is it the problem of array ?
1

You can try by passing connection variable() to mysql_query() function $sql_select=mysql_query($sql,$connection);

also you can try making your query in one line in the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.