PHP cannot read $_GET parameter when sending AJAX request


Ed91gg

I've been struggling with this since a few months and finally got to the point where I need to fix it in my project.

The problem is, I am sending an ajax request to a php file where I need to read the current $_GET['user_id'] value to insert the query. But php doesn't read the $_GET['user_id'] value from the current URL address like it doesn't exist.

This is driving me crazy because I don't know of any other way to follow user accounts to achieve the desired result.

Here is a piece of code, just for illustration purposes:

// PHP

<?php
require_once "db.php";

$query = "INSERT INTO followers(follower, following) ";
$query .= "VALUE('{$_SESSION['id_user']}','{$_GET['user_id']}' )";
$result = mysqli_query($connect_db, $query);

?>

// AJAX

<script>
    $('.js-follow_user_btn').click(function(){
            follow_unfollow_action();
        });


        function follow_unfollow_action(){

            $.ajax({
                type: 'POST',
                url: 'follow_user.php',
                success: function(data) {
                    alert('done');
                }
            });

        }
</script>
Quentin

PHP $_GETsuperglobals are populated with data from the query string of the requested URL (regardless of the HTTP method used to make the request).

Here is the URL you use:

url: 'follow_user.php',

There is no query string at all.

You might expect it to copy the query string from the page hosting the JavaScript. It doesn't work that way, you need to provide the data explicitly.


a little:

You are inserting data into database. You should use a POST request for this (but it does), but you should pass the data in the request body (otherwise you're likely to accidentally create something that responds incorrectly to a GET request.

So first thing to do: change PHP to $_POSTreplace $_GET.

While editing, your PHP is vulnerable to SQL injection attacks . You need to learn how to protect their code.


OK, so next you need to get the data from the query string of the current page.

You can read it using JavaScript. This answer explains several ways.

Once you have the data, you can put it into the request:

var user_id = (new URL(url_string)).searchParams.get("user_id");
// See the answer linked above to see how this works and for compatibility with old browsers

$.ajax({
    type: 'POST',
    url: 'follow_user.php',
    data: { user_id: user_id },
    success: function(data) {
        alert('done');
    }
});

...remember, this puts it in the request body, so you need to use $_POSTinstead of reading $_GET.

Related


PHP cannot read $_GET parameter when sending AJAX request

Ed91gg I've been struggling with this since a few months and finally got to the point where I need to fix it in my project. The problem is, I am sending an ajax request to a php file where I need to read the current $_GET['user_id'] value to insert the query.

PHP returns no data when sending ajax request

beans, lentils When I send an ajax post request to my getMessages.php file it returns nothing. I've tried manually setting the array values and printing them in the console and that seems to work. get message.php <?php require_once "mysqli.php"; $data = arra

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

PHP returns no data when sending ajax request

beans, lentils When I send an ajax post request to my getMessages.php file it returns nothing. I've tried manually setting the array values and printing them in the console and that seems to work. get message.php <?php require_once "mysqli.php"; $data = arra

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I've enabled rewrite rules in the .htaccess file in the root directory to keep pointing everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Read stream while sending GET request

jameson Here is the scene: I'm using OkHttp to send get requests asynchronously and it works great. code show as below: private void doGetRequest(String url){ Request request = new Request.Builder() .url(url) .build();

Read stream while sending GET request

jameson Here is the scene: I'm using OkHttp to send get requests asynchronously and it works great. code show as below: private void doGetRequest(String url){ Request request = new Request.Builder() .url(url) .build();

Read stream while sending GET request

jameson Here is the scene: I'm using OkHttp to send get requests asynchronously and it works great. code show as below: private void doGetRequest(String url){ Request request = new Request.Builder() .url(url) .build();

Read stream while sending GET request

jameson Here is the scene: I'm using OkHttp to send get requests asynchronously and it works great. code show as below: private void doGetRequest(String url){ Request request = new Request.Builder() .url(url) .build();

Read stream while sending GET request

jameson Here is the scene: I'm using OkHttp to send get requests asynchronously and it works great. code show as below: private void doGetRequest(String url){ Request request = new Request.Builder() .url(url) .build();

"ajaxFunction is not defined" when sending AJAX request

Chirag Senjaliya I want to update another page userreg.phpfrom index.php. Here is my code: <html> <head> <title> User List </title> <link rel="stylesheet" href="css/datacss.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs

500 error when sending ajax request

Siraj I have read every article here and have not found an answer to my problem. I have an action in this controller: public class AccountController : Controller { [HttpPost] public JsonResult CheckUsername(string i_username) { var Membersh

500 error when sending ajax request

Siraj I have read every article here and have not found an answer to my problem. I have an action in this controller: public class AccountController : Controller { [HttpPost] public JsonResult CheckUsername(string i_username) { var Membersh

"ajaxFunction is not defined" when sending AJAX request

Chirag Senjaliya I want to update another page userreg.phpfrom index.php. Here is my code: <html> <head> <title> User List </title> <link rel="stylesheet" href="css/datacss.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs

500 error when sending ajax request

Siraj I have read every article here and have not found an answer to my problem. I have an action in this controller: public class AccountController : Controller { [HttpPost] public JsonResult CheckUsername(string i_username) { var Membersh

why jquery ajax is not sending data in get request

cdm014 I'm setting up a page where our staff can view a list of items that should be on the shelf and then confirm if those items are there, or raise an alert if something goes wrong. I'm trying to create a function for each case (item exists, item doesn't exi

Sending ajax get request to node, response is undefined

Randy I have a node server running a single web page. The page has a contact form with a send button. If I click send, the following code runs: $('.contactsend').click(function(){ $.ajax({ method: "GET", url: "http://127.0.0.1:3000/contact"