send attr value for $.ajax

Asked

Viewed 27 times

1

I have a link a, that if clicked, leads to a scrpt ajax for the deletion of ID.

With the código below I can’t catch the id in page of php

// JavaScript Document
$(document).ready(function(e) {

  $("a.bloqDesbloq").click(function() {

        $.ajax({
            url: "../_requeridos/bloqueiaAdministrador.php",
            type: 'POST',            
            data : {
                    'idadministrador'   : $(this).attr('idadmin'),
                    'bloq'              : $(this).attr('bloq')
                   },
            beforeSend: function() {
            },
            success: function (retorno) {

                if (retorno == "OK") {

                    alert('Bloqueado com sucesso');
                    location.reload();

                } else {

                    alert("Erro na bloqueio");

                }

            },
            cache: false,
            contentType: false,
            processData: false
        });

          return false;

  });

});

the $_post arrives in the php empty array.

Where am I going wrong?

Note: If I do

alert( $(this).attr('idadmin'))   

Before calling the ajax, the values appear

  • The data that will be sent to PHP should be in data. Try to do something like data: {idadministrador: ...}

  • I did this, but it is undefined in php. post e get in php with Array ( ) Array ( ). That is, empty arrays

  • how is the code before calling ajax? maybe your "$(this)" no longer represents the field you want to pull the information.

  • before ajax has nothing. But as stated in the question, if I give an Alert in $(this). attr('idadmin') before ajax, the value is normal

  • added the whole question js to make it clearer

  • Place the contents of your a.bloqDesbloq, I want to understand why you’re picking up an attribute idadmin and another bloq of the same tag <a>

Show 1 more comment

1 answer

2


I tested your current code here, it doesn’t really pass anything to PHP, but then I removed the last two lines of ajax and it worked:

},
cache: false,
contentType: false,
processData: false
});

for

},
cache: false
});

(Remember to take the comma out after caching too)

I believe that some of these values prevented sending the post correctly. I will search better to know what they do and then further edit my reply.

-Edit:

From what I saw in http://api.jquery.com/jquery.ajax/ using contenttype: false; will force you to send the data without a "content type". this value you can remove, so that ajax uses its pattern.

When using processData: false you tell ajax not to "organize" your object as STRING, but "application/x-www-form-urlencoded" (Standard ajax) needs to be passed as STRING. The Default of this option is also true, so you can delete this line.

"cache: false" did not cause any errors in your code.

  • Thank you, that was it. Programming and learning!

Browser other questions tagged

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