Fetch PHP data from Ajax

Asked

Viewed 91 times

1

I have an Ajax script here that his function is as follows.. I have a news system, which by clicking on the title will open a modal and show me the news data (title, text, author).. but I don’t know what to put in PHP..

This is the js code

var News = {
Init: function(id) {
    $.ajax({
        url: 'noticia.php',
        type: 'POST',
        dataType: 'json',
        data: {'id': id},
        beforeSend: function(){
            $('body').animate({'opacity':'0.5'});
        },success: function(data) {
            $('body').animate({'opacity':'1'});
            var Html = '';
                Html+='<div id="modal" class="animated bounceInDown">';
                Html+='     <div class="ler_content animated bounceInDown"> <div id="close_new" onclick="Alerta.Close()"></div>';
                Html+='         <div class="titulo">';
                Html+='            <div class="img" style="background: url('+data['imagem']+');">';
                Html+='            </div>';
                Html+='           <div class="autor">'+data['autor']+'</div>';
                Html+='           <span>'+data['titulo']+'</span>';
                Html+='        </div>';
                Html+='        <div class="texto">'+data['texto']+'</div>';
                Html+='     </div>';
                Html+=' </div>';

            $('body').prepend(Html);

        }

    })



}}

what I’m trying to put in the news.php is this, but I know it’s wrong..

$id = mysql_real_escape_string( $_GET['id'] );
$data = array();
$sql = "SELECT id, titulo, imagem, autor, texto
    FROM noticias
    WHERE id=$id
    ORDER BY id";
$res = mysql_query( $sql );
while ( $row = mysql_fetch_assoc( $res ) ) {
$data[] = array(
    'id'    => $row['id'],
    'titulo'            => $row['titulo'],
    'imagem'            => $row['imagem'],
    'autor'         => $row['autor'],
    'texto'         => $row['texto'],
);}
echo( json_encode( $data ) );

Help me!!

  • Where’s the problem? Some error or something that doesn’t work?

  • when I open the news she doesn’t show what she should

  • Some mistake? shows something? what gives console.log(data); within that success ajax?

  • beforeSend: Function(){ $('body'). Animate({'opacity':'0.5'}); },Success: Function(data) { $('body'). Animate({'opacity':'1'}); If it does not return the "sucess" page it is 0.5 of transparence..

  • You know what it is console.log? you already tested that I asked?

  • I don’t understand much of it, in the same already in another function has a "console.log('Init');"

  • Read this http://answall.com/q/38057/129 and then answer the question here in the comments.

  • Man, I don’t quite get it, but it sums me up, whatever I do?

  • The best way to help you is to get you to read that question and answers. I’ve asked you what I need to know to help you. I’m waiting for the answer if you want more help. Believe me it’s worth reading :)

  • Sergio, I don’t understand this language, I read what you told me and I got lost in the subject..

  • https://www.youtube.com/watch?v=eSPMRDcq9Co

  • ok, I think I understand now, I added to the end of the function News console.log('News'); and I checked that the sucess modified my body by adding a style="opacity: 0.5;", but it did not show the rest that it should

  • It executes "beforeSend: Function(){ $('body'). Animate({'opacity':'0.5'}); }" but does not display the successor afterwards

  • Sergio, now yes it is working, but a detail, the values are returning as "Undefinied"

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.