skip to Main Content

I try to hand over a parameter, a String, that is determined by user input via Ajax to a PHP Programm, but the php does not receive the parameter I try to handover. Then Ajax should return a JSON

const category = document.getElementById('genreSelect').value;

            $.ajax({  
            type: 'GET',  
            url: 'button.php', 
            data: { category: category } ,
            success: function(response) {                
            console.log(response);
                

    }
});
<?php

header('Content-Type: application/json');

$kate ="";
if(isset($_POST['category'])) {
        $name = $_POST['category'];
        $kate =$name;
        echo $name;
    } else {
        echo "No name parameter received.";
    }
echo json_encode($kate);
?>

I have tried several other Ajax commands, but they did not work.

2

Answers


  1. you only need change POST for GET

    if(isset($_GET['category'])) {
            $name = $_GET['category'];
    
    Login or Signup to reply.
  2. Your

    type: 'GET'
    

    specifies that you are sending a GET request, whereas on the other side you expect a POST parameter with $_POST['category']. Change your request type to POST, like:

    type: 'POST'
    

    and then test… Open your browser dev tools, navigate to the Network tab and then click on your submit button. See whether you get some errors in the console due to some problems and whether you see a new request appearing in the Network tab.

    If you see a new request, verify the payload of that request, whether it contains the category you wanted to pass and also verify the URL where you wanted to send your request whether it actually executes your server-side code. You can use a debugger tool for that purpose, like xdebug.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search