am I trying to send the id of a tag to ajax but when I pick it up in php it returns to me as null which should be the correct way?

0

 <script type="text/javascript">
$(document).ready(function(){
$(".vista_rapida").click(function() {
var vista_rapidas=$(this).attr("id");
         $.ajax({
          url:'aindex.php',
          method:'post',
          data:{"producto_indice":vista_rapidas}
          });
          });
           });
</script>

 <?
  $data =$_POST['producto_indice'];
   echo var_dump($data);
  ?>
    
asked by Leonardo Mancero 06.11.2017 в 21:26
source

1 answer

0

You need to put more part of your HTML code, but I modified your code a bit, so I'll explain what happened :)



   var vista_rapidas=$(this).attr('name'); /*con esta linea capturas el valor que tiene tu 'input' */
        var parametros = {  
                "producto_indice" : vista_rapidas, /* con esta linea vas a crear una variable con el nombre vista_rapidas, el cual contendra el contenido del 'input' antes mencionado*/

                        };
        $.ajax({
                data:  parametros, //datos que se envian a traves de ajax
                url:   'aindex.php', //archivo que recibe la peticion
                type:  'post', //método de envio, puede ser GET
                beforeSend: function () {
                        //$("#resultado").html("Procesando, espere por favor..."); /*esta parte es opcional, es para mostrar un mensaje en alguna parte de tu pagina hasta que el php procese la información que has enviado */
                },
                success:  function (response) { //una vez que el archivo recibe el request lo procesa y lo devuelve
                       $("#resultado").html(response); /*lo que podes hacer es poner la respuesta dentro de un div con id "resultado". */

                }

and your php would be the same :)

 

You could give a revision to the documentation that exists regarding sending information through ajax, here in the forum there are related topics :) regards.

    
answered by 06.11.2017 в 22:30