Get variable from a PHP using AJAX [closed]

0

I do this request with AJAX, but when I give echo to variable $_GET it does not display anything, I gave print_r and dump to it and it's empty, it's simply an array empty. However, in sucess , console.log displays the data I want to get, however I want this variable in PHP and I'm not getting it that way.

<script>
$.get("request.php", "dados=dados", function(dados) {
  console.log(dados);
});
</script>
<?
$res = $_GET["dados"];
echo $res;
?>
    
asked by anonymous 11.07.2017 / 20:56

3 answers

0

I think all this is just a 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 function would show in console something like it all :

Yes,itwouldreturntojavascriptandhtmlasastring,sincethecurrentpagewouldberequestingitself,butifitdidnotreturnthenitisbecausedifferentpagesanddifferentPHPscripts,ifthisisthecaseitisverylikelythatyouarenotpointingafilecorrectly.

Practicaltestwith$.get($.ajax):

Createthefollowingfiles:

foo.php

<?phpvar_dump($_GET);

baz.html

<!DOCTYPEhtml><html><head><title></title><scriptsrc="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 did returned exactly this:

Otherpossibleissues:

VersionofjQuerywithBUG

MaybeinyourpageyouareusingaversionofjQuerythathasabug(ithasalreadyoccurredtome),Irecommendthatyouupgradetothelatestversion(inthiscaseyoucanchoosebetweenmajorversionsjQuery1,jQuery2,orjQuery3),incaseIuse1.12.4:

link

For other versions see: link

Problem of some plugin that is using ajaxSetup

If you happen to be using $.ajaxSetup it might affect the behavior of all jQuery calls, I recommend that you remove exist and test again.

As a test do this:

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

And see if it still crashes

Problem in installing PHP and Apache

To be sure it's a problem installing the server so you can take a real test try to access request.php directly, for example:

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

You did not understand what HTTP is

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

Summarizing a little:

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 rendered in the browser

  • back-end

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

  • HTTP request

    This is what the browser sends to a server, it occurs when you type a URL in the navigation bar, when you upload a

  • HTTP Response

    The HTTP response is generated after an HTTP request and it will respond according to the request of this request

PHP is a language that can be used (and is usually used) for web pages, it runs on the side that we call the back end, the browser communicates with the server through the HTTP protocol 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, every line or entire generated content will be sent as an "HTTP response" to the browser you requested, for example:

Inotherwords,PHPdoesnotrunnexttoHTML,itgeneratesaresponsethatcanbeaHTML"document", such as a TXT, an image, a video, will depend on what you set PHP to send response .

I know that the linked answers do not solve your problem, but your problem is probably your confusion about not understanding the layers.

    
11.07.2017 / 21:58
-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;
?>
    
11.07.2017 / 21:32
-3

Let's see if I understand, you're on a page in PHP, wanting to make an asynchronous request to another page in PHP, that?

Why not use cURL.

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

curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true);

$ response = curl_exec ($ curl);

$ error = curl_error ($ curl);

curl_close ($ curl);

    
11.07.2017 / 21:55