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:
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:
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:
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.
On the console appears
dados
? If yes, it is not clear what the problem is. If the value arrives insuccess
, PHP is correctly displaying the value. How did you verify that no data arrives in PHP?– Woss
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
@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.
– Bacco
And this PHP code is where? In the file
request.php
? You know this requisition you made is asynchronous, right?– Woss
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
@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.
– Bacco
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.
– Bacco
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)– Guilherme Nascimento
@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
@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...
– rmiranda
@Guilhermenascimento Thanks for the suggestion, but it didn’t work...
– rmiranda
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.– Woss
@Andersoncarloswoss So what I’m trying to do is not possible? Take a variable from a . php with ajax?
– rmiranda
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.– Woss
@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".
– Guilherme Nascimento
@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.
– rmiranda
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.
– Woss
Just answer me one thing, yours
$.get(...)
is in the same file as request.php? I don’t think so.– Guilherme Nascimento
@Guilhermenascimento is not, William...
– rmiranda