You are not passing JS variable value to PHP via ajax

Asked

Viewed 34 times

-1

Javascript

$('#save-update').data('eventId', info.event.id);

$('#delete-update').click(function (info) {
        info.preventDefault();        
        let eventID = $('#save-update').data('eventId');        
        $.ajax({
            type: 'POST',
            url: 'php/delete-events.php',             
            data: eventID,       
            contentType: false,
            processData: false,
            success: function(response) {
                location.reload();                       
            }
        })
    });

PHP

$id = $_POST['eventID'];

The id value for the PHP file is not coming. Notice: Undefined index: eventID

1 answer

-1


You need to send the variable in a JS object

$('#delete-update').click(function (info) {
        info.preventDefault();        
        let eventID = $('#save-update').data('eventId');        
        $.ajax({
            type: 'POST',
            url: 'php/delete-events.php',             
            data: {eventID:eventID},       // Alterado aqui
            contentType: false,
            processData: false,
            success: function(response) {
                location.reload();                       
            }
        })
    });

Explaining

           ⬐ ex. $_POST['eventID']
data: { eventID : eventID },
              Valor ⬏
  • It’s still the same, PHP can’t get the value. He is making the request and even gave an Alert in eventID in JS and leaving the id value, but not going to PHP.

  • removes the line from "processData" and "contenttype"

  • Yeah, it worked. Thank you very much!

  • happy to help you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.