I wonder if I’m doing this ajax the right way?

Asked

Viewed 75 times

0

Guys, I have this ajax and a page called test.php that gets a value, I wonder if my ajax is right and this value is being picked correct 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"];
  • Yeah, you’re doing it right, just put a success: function(data){} to have the return of php and perform some action

  • That one dataType: "php" that didn’t make much sense, only. With it you will inform jQuery what is the type of return expected in the answer. Usually it is text, html, json or xml.

  • I think with this code is not passing the value campoRadio for the page test.php, What else does the page test.php besides taking the value from the form? Perform some routine with the $valor to load something into the ajax page? Or just want to pass the value to test.php?

  • Leo, this test would be a pagseguro.php, and I send is the value of a plan inside it, but it’s not working, it seems to me that I get the value inside of pagseguro.php, but pagseguro can not run!!

2 answers

2

This certain yes, only missing the return treatment. (Success, error) Follows 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 ta half wrong, it serves to tell if the return is text or json or some other object in question. in case you can remove it

-1

I believe the right 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 name of the variables so as not to get confused

Browser other questions tagged

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