Take a PHP variable using AJAX

Asked

Viewed 1,138 times

0

I make this request with AJAX, but when I give echo in the variable $_GET she doesn’t exhibit anything, I gave a print_r and a dump in it and it’s empty, it’s just a array empty. However, in the sucess, the console.log displays the data I want to get, but I want this variable in PHP and I’m not getting it this way.

<script>
$.get("request.php", "dados=dados", function(dados) {
  console.log(dados);
});
</script>
<?
$res = $_GET["dados"];
echo $res;
?>
  • On the console appears dados? If yes, it is not clear what the problem is. If the value arrives in success, PHP is correctly displaying the value. How did you verify that no data arrives in PHP?

  • In the console it displays the value I want, but when I give $_GET["data"] to get the value of the variable it is empty...

  • @rmiranda PHP runs on the server side. $_GET runs "before Ajax". Keep in mind that PHP mounts the full page before sending to the client and it is the client that runs Ajax.

  • And this PHP code is where? In the file request.php? You know this requisition you made is asynchronous, right?

  • Yeah, buddy, stay on request.php. But I want to bring a variable there from the request to the index in the case and doing that get for that purpose, I don’t know if I’m being clear...

  • @rmiranda is being clear, but you need to understand the moments where PHP works and where Ajax works. PHP does not work client-side, if you need something with Ajax on the same page, you need to use JS. If you want to pick up a data via PHP, you need to make Ajax run this PHP and display via JS in the same way. PHP does not have the ability to modify by itself the content of a page already sent to the client, it runs on the server, before the display.

  • Even if you request for the same page, what is being displayed in echo is the first execution. The second run data will be picked up by the next GET, but will not appear on the screen.

  • Rmiranda Just for testing, change <? for <?php, (supposing request.php has js and php at the same time), but it’s like @Bacco said, maybe it helps: https://answall.com/a/177050/3635, https://answall.com/a/168915/3635 and https://answall.com/a/102460/3635 (Bacco if you have any good link, maybe better, please indicate to me, for future use :D)

  • @Guilhermenascimento I’m kind of "passing through" the site, I think your links can help a lot. At the moment, I don’t remember anything specific about the subject (I know we’ve dealt with it before, but I can’t immediately remember how to find it). Take the opportunity to check if I skipped any details in the comments :)

  • @Bacco I get it, but how would I run the GET the way you said? I thought just putting it sequentially after would no longer have this running order problem...

  • @Guilhermenascimento Thanks for the suggestion, but it didn’t work...

  • Within the callback success you already have the value you want in Javascript. PHP there will not work the way you want. To understand this, read all the questions and answers listed by William. At the time you made the request you are working on the client side, no longer on the server, so forget PHP.

  • @Andersoncarloswoss So what I’m trying to do is not possible? Take a variable from a . php with ajax?

  • It is possible and you have already done. So much that in success comes the amount you want. It’s just not clear what you need to do with this value, so there’s no way to help you in more detail than that.

  • @rmiranda read the links I passed in the previous comment, but read calmly, very likely you have not understood the back end and front end and where they are located, the most important thing is you understand the "layers".

  • @Andersoncarloswoss So, friend I need to manipulate this value on the page I’m on, so I gave the $_GET[]. What was not clear to me, is how to take the value of Success and move to my PHP.

  • 1

    It doesn’t pass. Anything you need to do with this value now will be with Javascript, because you are on the client side, no longer on the server.

  • Just answer me one thing, yours $.get(...) is in the same file as request.php? I don’t think so.

  • @Guilhermenascimento is not, William...

Show 14 more comments

3 answers

0


I believe that all this is just one confusion on your part.

If request.php is a file and your jQuery and scripts are on another page, because if they were all in the same file, the return of the variable dados in the function would show on the console something like all this:

assim

Yes, it would return to javascript and html as a string, because the current page would be ordering itself, but if it did not return this then it is because they are different pages and different PHP scripts, if this is the case it is very likely that you are not pointing a file correctly.

Practical test with $.get ($.ajax):

Create the following files:

foo.php

<?php
var_dump($_GET);

Baz.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>

<p>Oi</p>

<script>
$.get("foo.php", "dados=dados", function(dados) {
    console.log(dados);
});
</script>
</body>
</html>

Both should be in the same folder and should not change anything, the result I made returned exactly this:

teste Ajax no Opera

Other possible problems:

Version of jQuery with BUG

Maybe on your page you are using a version of jQuery that has some bug (already occurred to me), I recommend you upgrade to the latest version (in case you can choose between the major jQuery1, jQuery2, or jQuery3 versions), in which case I use 1.12.4:

https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js

For other versions see: http://jquery.com/download/

Problem of some plugin using ajaxSetup

If you happen to be using $.ajaxSetup it can affect the behavior of all jQuery calls, recommend removing if it exists and testing again.

How test do this:

$.get("request.php?dados=dados", function(dados) {
    console.log(dados);
});

And see if it still fails

Problem installing PHP and Apache

To be sure if it is a problem in the installation of the server so you can take a real proof try to access request.php directly, for example:

If it works hard the problem will be in the installation of the server.

You don’t understand what HTTP is

As I asked, you could read the links that I tried to explain the best I could about how Requests and Answers works, I think you should be making confusion in this and if you read the links calmly you will understand well where it is failing, this because HTML and Javascript do not get "communicate directly" with PHP:

To sum up:

you have to understand some things first:

  • front-end

    The front-end is a relative term, but in practice it is usually used to refer to what will be processed in the browser

  • back-end

    The back-end is also relative, but in practice it is usually used to refer to general server-side technologies such as database, HTTP-processing program (such as Apache and IIS) and dynamic language and frameworks

  • HTTP request

    It is what the browser sends to a server, occurs at the moment you type a URL in the navigation bar, when it sends an upload

  • HTTP response

    The HTTP response is generated after an HTTP request and it will respond as requested by this request

PHP is a language that can be used (and is usually used) for web pages, it runs on the side we call "back-end", the browser communicates with the server through the HTTP protocol by making a request, then PHP processes a script and generates a response, this all occurs on the server and not on the user’s machine, each line or entire content generated will be sent as an "HTTP response" to the browser you requested, for example:

http

In other words, PHP does not run next to HTML, it generates a response that can be an HTML "document", as it can be a TXT, an image, a video, it will depend on what you have defined that PHP should send in response.

I know that linked answers don’t solve your problem, only your problem is probably your confusion because you don’t understand the layers.

  • 1

    The last sentence answered everything. I think I understood what he was trying to do and, in fact, it was a mess. The files are different and the request was made correctly - understanding what was happening, but I believe the confusion was in the HTTP response. The echo $_GET["dados"] of him in PHP was in index.php and it was so because he believed the value dados sent by the request would be returned by the reply to the file index.php and thus display again dados on screen. As commented, he did not understand HTTP and [client/server]-side.

  • 1

    @Andersoncarloswoss is such a common mistake, so I insist on the links I pointed out in that comment, I had a better link that was just about the subject, but I can’t remember where else this (maybe it was in Soen)

  • 1

    Valeuzão, Guilherme Nascimento! Now I get it, I made a mess right here, rs I just asked here why I couldn’t get help elsewhere, but thanks!

  • @rmiranda when can mark the answer as correct, if she solved your problem.

-2

Try sending it like this to see what happens

  <script>
    $.get( "request.php",
           { dados: "dados" }, 
           function( data ) {
            console.log(data);
       }  );
    </script>

And in php:

<?php
  $dados = $_GET['dados'];
  echo $dados;
?>
  • 1

    In the question it is explicit that the console.log correctly displays the values. That is, the problem is not in this code. Please pay a little more attention before replying and see the comments what has already been discussed.

  • 1

    Just for the record, I’m not the one who said no, so much so that I understood your suggestion, however $.get accepts both string and Object, the problem must be a user confusion.

-3

Let’s see if I understand, you are on a page in PHP, wanting to make an asynchronous request to another page in PHP, this?

Why not use Curl.

$Curl = curl_init( "request.php? data=data" );

curl_setopt( $Curl, CURLOPT_RETURNTRANSFER , true );

$Answer = curl_exec( $Curl );

$error = curl_error( $Curl );

curl_close( $Curl );

Browser other questions tagged

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