0

When I try to access the form data I send with a jQuery AJAX PUT request, I get this response:

Array ( [------WebKitFormBoundary4TPPQZu7B7WFrUsv Content-Disposition:_form-data;_name] => "id" 7 ------WebKitFormBoundary4TPPQZu7B7WFrUsv Content-Disposition: form-data; name="naam" Eiusmod alias est do ------WebKitFormBoundary4TPPQZu7B7WFrUsv Content-Disposition: form-data; name="email" [email protected] ------WebKitFormBoundary4TPPQZu7B7WFrUsv Content-Disposition: form-data; name="adres" Voluptates adipisici ------WebKitFormBoundary4TPPQZu7B7WFrUsv Content-Disposition: form-data; name="telefoonnummer" 0612345678 ------WebKitFormBoundary4TPPQZu7B7WFrUsv Content-Disposition: form-data; name="rol" 4 ------WebKitFormBoundary4TPPQZu7B7WFrUsv-- )
Warning: Undefined array key "id" in C:\xampp\htdocs\P08\hoornhek\api\gebruiker.php on line 22

I also can't get the array keys. This is my jQuery AJAX request:

$('#bewerkGebruikerForm').on('submit', function(event) {
            event.preventDefault();
            const formData = new FormData(this);

            // Opslaan gegevens gebruiker
            $.ajax({
                url: 'http://localhost/P08/hoornhek/api/gebruiker.php',
                type: 'put',
                data: formData,
                dataType: 'json',
                cache: false,
                contentType: false,
                processData: false,
                success: function(response) {
                    if(response['success']) {
                        $('#bewerkGebruikerModal').hide();
                        window.location.reload();
                    } else {
                        showErrors(response.errors);
                    }
                },
                error: function(error) {
                    showErrors(error.errors);
                }
            });
        });

And this is my PHP (API) code:

$requestMethod = $_SERVER['REQUEST_METHOD'];
switch ($requestMethod) {
    case "GET":
        getGebruiker();
    case "POST":
        createGebruiker();
        break;
    case "PUT":
        parse_str(file_get_contents("php://input"), $_PUT);

        print_r($_PUT);
        echo $_PUT['id'];

        updateGebruiker();
        break;
    case "DELETE":
        parse_str(file_get_contents("php://input"), $_DELETE);
        deleteGebruiker();
        break;
    default:
        exit();
}

For my GET, POST and DELETE requests everything works fine. What am I doing wrong?

1 Answer 1

2

You cant access the data from a PUT request via $_REQUEST. You'd need something like:

case "PUT":
    parse_str(file_get_contents("php://input"), $sent_vars);

    echo json_encode(['response'=>$sent_vars['id']]); // use an array and json_encode toavoid messy string concatenation

    updateGebruiker();
    break;

See also Accessing Incoming PUT Data from PHP

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.