Only take a PHP variable from another page with Jquery

Asked

Viewed 949 times

3

To get the content of a page I use the date, but I would like to know how to get only one variable on another page. I use this code:

jQuery(document).ready(function(){
        jQuery('.teste').submit( function(){
            var teste = $(this);
            var dados = teste.serialize();
            jQuery.ajax({
                url: "teste.php",
                type: "POST",
                data: dados,

                success: function(data)
                {

                    teste.html(data);


                }

            });

            return false;
        });
    });

But it takes all the content from the page. How would I get only one PHP variable, for example: "$test". I put an example below to illustrate better, I know it is not that way.

jQuery(document).ready(function(){
            jQuery('.teste').submit( function(){
                var teste = $(this);
                var dados = teste.serialize();
                jQuery.ajax({
                    url: "teste.php",
                    type: "POST",
                    data: dados,

                    success: function(data)
                    {

                        teste.html(data.$teste);


                    }

                });

                return false;
            });
        });

Test page.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $teste ="teste";
        $teste2 ="teste2";
        echo $teste;
        echo $teste2;
         ?>
</body>
</html>
  • What is the format of the return?

  • 3

    You can ask the question the file code teste.php?

  • Post the code of the test.php

  • Sending via get url does not work?

  • If you work with feedback on json you can map what you want to display

  • So it only works with JSON?

  • In a certain way yes, besides being the most indica form. But you can use web crawler to do this too

Show 2 more comments

2 answers

2


I don’t know if it’s the best, but one way to do that is to send a variable to the page teste.php and in it isolate only what you want as answer, in case the variable $teste. In return, I use the $.trim(data) to eliminate unnecessary blanks:

Ajax:

jQuery(document).ready(function(){
            jQuery('.teste').submit( function(){
                var teste = $(this);
                var dados = teste.serialize();
                jQuery.ajax({
                    url: "teste.php?variavel=ok",
                    type: "POST",
                    data: dados,

                    success: function(data)
                    {
                        teste.html($.trim(data));
                    }

                });

                return false;
            });
        });

On the page teste.php, I take the variable with $_GET (the other information on the form is $_POST). Anything I don’t want to return in Ajax, I put inside the if:

<?php
if ($variavel != "ok"){
?>
<?php } ?>

Page teste.php:

<?php
$variavel = $_GET['variavel'];

if ($variavel != "ok"){
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
<?php } ?>
        <?php
        $teste ="teste";
        $teste2 ="teste2";
        echo $teste;
         ?>
<?php if ($variavel != "ok"){ ?>
</body>
</html>
<?php } ?>
  • I got to do what I wanted. Thanks for the answer!

2

Making the PHP response with JSON will be the easiest way to handle it with Javascript. First, as the PHP file will be requested through AJAX, it is not necessary to have all this HTML structure. It only makes sense if you’re going to display it somehow in the browser, but it doesn’t seem to be the case. So, you can do it in PHP:

php test.

<?php

$teste = "valor_teste";
$teste2 = "valor_teste2";

// Indica ao navegador que a resposta será um JSON:
header('Content-Type: application/json');

echo json_encode(compact("teste", "teste2"));

The function compact will generate a array in form:

["teste" => "valor_teste", "teste2" => "valor_teste2"]

And the function json_encode will convert it to a string in JSON format. So your Javascript can look like this:

jQuery(document).ready(function(){
    jQuery('.teste').submit( function(){

        var teste = $(this);
        var dados = teste.serialize();

        jQuery.ajax({
            url: "teste.php",
            type: "POST",
            data: dados,
            dataType: "json",
            success: function(data) {
                teste.html(data.teste2);
            }
        });

        return false;
    });
});

Using the property dataType you indicate that the return of PHP will be a JSON, thus the value of data in success will be a JS object created from the JSON returned by PHP and can access the attribute teste2, for the variable $teste2 of PHP.

  • I got to do what I wanted. Thanks for the answer!

Browser other questions tagged

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