I wonder if I'm doing this ajax in the right way?

0

I have this ajax and a page called test.php that gets a value, I would like to know if my ajax is right and this value is being fetched inside the test.php page

<script>
  $('input[name=valor]').click(function() {  
var campoRadio =  $('input[name=valor]:checked').val();


$.ajax({
   method: "POST",
   data: { campoRadio: campoRadio },
   url: "test.php",
   dataType: "php"

});
 alert(campoRadio);

});


  </script>

test.php

$valor = $_POST["campoRadio"];
    
asked by anonymous 01.11.2017 / 17:06

2 answers

2

That's right, all it takes is the return treatment. (success, error) Here's an example:

$.ajax({
   method: "POST",
   data: { campoRadio: campoRadio },
   url: "test.php",
   success:function(data){
       //Coloque aqui o codigo que desejar, lembrando que ele cai aqui caso o retorno seja OK e a variavel data trás para vc o retorno da url chamada
   }

});

Detail: This dataType is half wrong, it is used to tell if the return is text or json or some other object in question. in case you can remove it

    
01.11.2017 / 20:13
-1

I believe the correct way would be:

script>


 $('input[name=valor]').click(function() {  
var _campoRadio =  $('input[name=valor]:checked').val();

  $.ajax({
    method: "post",
    url: "meu_script.php",
    data: {campoRadio : _campoRadio},
  }); 

I always tried to differentiate the names of the variables so as not to be confused.

    
01.11.2017 / 17:20