How to send PHP variables using Ajax

0

I'm loading the page through ajax but as I have to send parameters via url I do not know how to send them with ajax.

I tried it that way

<?php 
$album=$_GET['album'];
$id=$_GET['ntf'];
?>
<script type="text/javascript">
$(function() {
$.ajax({
    type: "GET",
    url: "pegafotos.php?album=<?php echo $album ?>&nft=<?php echo $id  ?>",
    success: function(resp) {
        $("#result").html(resp);
    }
});
});

Div where the result appears

<div id="result"> Carregando <img src="css/images/gif.gif" width="16" height="16" alt="Loading..." /></div>
    
asked by anonymous 22.11.2014 / 20:15

1 answer

1

First. a consideration. of what this:

<?php
$album=$_GET['album'];
$id=$_GET['ntf'];
?>

It should be at least this:

$album = ( isset( $_GET['album'] ) ? $_GET['album'] : 0 );
$id    = ( isset( $_GET['ntf'] )   ? $_GET['ntf']   : 0 );

Now, regarding your problem, AJAX is a technique for making asynchronous requests to a resource located from a given URI.

From what you can tell from your example, what do you expect to happen if the requested page were the same page you are ordering? So:

<html>

    <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <script type='text/javascript' src='http://code.jquery.com/jquery-compat-git.js'></script>

    <?php

    $album = ( isset( $_GET['album'] ) ? $_GET['album'] : 0 );
    $id    = ( isset( $_GET['ntf'] )   ? $_GET['ntf']   : 0 );

    ?>

    <script type="text/javascript">

    $( function() {

        $.ajax({

            type: "GET",
            url: "test.php",

            data: { album: <?php echo $album; ?>, id: <?php echo $id; ?> },

        }).done( function( data ) {

            $( "#result" ).html( data );

        });
    });

    </script>


    </head>

    <body>

        <div id="result"> Carregando <img src="css/images/gif.gif" width="16" height="16" alt="Loading..." /></div>

    </body>
</html>

Whether for extra credit or pure curiosity you will go dozens of requests in the Network / Network tab of your browser or extension to the part if you have nothing native (Firebug).

What you should do is separate PHP into a / URI file and JS that will consume it in another. For example:

index.php

$album = ( isset( $_GET['album'] ) ? $_GET['album'] : 0 );
$id    = ( isset( $_GET['ntf'] )   ? $_GET['ntf']   : 0 );

echo json_encode( array( 'album' => $album, 'id' => $id ) );

index.html

<html>

    <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <script type='text/javascript' src='http://code.jquery.com/jquery-compat-git.js'></script>

    <script type="text/javascript">

    $( function() {

        $.ajax({

            type: "GET",
            url: "index.php",
            dataType: 'json',
            data: { album: 10, ntf: 20 },

        }).done( function( data ) {

            $( "#result" ).html( 'Album: ' + data.album + '. ID: ' + data.id );

        });
    });

    </script>


    </head>

    <body>

        <div id="result"> Carregando <img src="css/images/gif.gif" width="16" height="16" alt="Loading..." /></div>

    </body>
</html>

So, when the Request is successfully completed the contents of the DIV will be updated to, in this example, Album: 10 ID: 20     

22.11.2014 / 21:23