How to send PHP variables using Ajax

Asked

Viewed 2,937 times

0

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

I tried so

<?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>
  • Apart from missing ; after both of them echo no mistakes. Must be <?php echo $album; ?>. I have doubts: This code is on the same page? Can you explain better what the role of AJAX is? how does Ajax run? If AJAX runs when the page loads it would be better to have only PHP without AJAX...

  • Thank you for noticing that ; was missing. Yes the code is on the same page ,what the ajax is doing there and when the page takes photos.php has loaded the delete what is in the div and displays the results that were loaded from the sticky photos.php ,so I have to pass parameters and I can’t send them

1 answer

1

First. a consideration. of that:

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

It should be at least that:

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

Now, regarding your problem, AJAX is a technique to perform asynchronous requests to a resource located from a particular URI.

From what you can understand from your example, what do you expect would 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 it’s extra credit or just out of curiosity you’ll go dozens of requests in your browser’s Network/Network tab or a separate extension if you don’t have anything native (Firebug).

What you should do is separate PHP into one/URI file and JS into 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 made the content of the DIV will be current to, in this example, Album: 10 ID: 20

  • Thank you for your reply could only make me doubt ? Because at the very least so $album = ( isset( $_GET['album'] ) ? $_GET['album'] : 0 );&#xA;$id = ( isset( $_GET['ntf'] ) ? $_GET['ntf'] : 0 );

  • 1

    It’s the least security when using a value from the user that may not come. If you made the request without passing these values and by chance your error alerts were too low or disabled, the resulting JSON, which jQuery would work, would be invalid.

Browser other questions tagged

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