how to receive data in PHP sent from AJAX in wordpress

0

I am not receiving the id from ajax in PHP, I have tried several forms but it always remains null, if in the file content.php I load the variable "$ post_id = 123" statically shows the data correctly.

I have my JS, where I get the id of a HTML select

 function infodeptos(id) {
     var parametros = {
        "id" : id
      };

     $.ajax({
        data:  parametros,
        url:   '/wp-content/themes/brotec-wp/content.php',
        type:  'post', 
        success:  function (response) {
            // console.log(response);
        },
        error: function(jqXHR, status, error) {
            // Error handling
            console.log(error);
        }
      });
   }

and I have the content.php where I intend to get the id of the JS, load the variable $ post_id and pass it to HTML.

<?php 
    // post_id = 123, 126, 1
    $post_id = 123;
    // echo "post====>";
    $id = $_POST['id'];

   var_dump($id);
?>
<div class="info" id="info">
 <ul>
   <?php if(get_field('dormitorios',$post_id)): ?>
     <li class="dormitorio"><?php the_field('dormitorios',$post_id); ?> dormitorios</li>
   <?php endif; ?>
   <?php if(get_field('banos',$post_id)): ?>
     <li class="banos"><?php the_field('banos',$post_id); ?> baños</li>
   <?php endif; ?>
   <li class="sup-total"><strong>Sub. Total <br /><?php the_field('sub_total',$post_id); ?></strong></li>
   <li class="item">Depto <br /><?php the_field('depto',$post_id); ?></li>
   <li class="item">Terraza <br /><?php the_field('terraza',$post_id); ?></li>
   <li class="sup-util"><strong>Sup.Útil <br /><?php the_field('sup_util',$post_id); ?></strong></li>
   <li class="item">Depto <br /><?php the_field('depto2',$post_id); ?></li>
   <li class="item">Terraza<br /><?php the_field('terraza2',$post_id); ?></li>
   <li class="orientacion">Orientacion: <?php the_field('orientacion',$post_id); ?></li>
   <li class="entrega">Entrega segundo semestre <?php the_field('entrega',$post_id); ?></li>
 </ul>
 <a class="cotizar" href="#">COTIZAR</a>
 </div>

What am I doing wrong, or am I missing something, maybe in another way?

    
asked by Leslie Redlich 16.11.2018 в 14:48
source

1 answer

1

Solved, the url is formed in a different way:

On the AJAX:

$.ajax({
    type:  "post", 
    dataType: "json",
    url: "/wp-admin/admin-ajax.php?action=get_select",
    data:  parametros,
success : function (response){

}
error: function(jqXHR, status, error) {
   console.log(error);
}

In the wordpress function.php file, I create the function that will communicate with AJAX:

add_action('wp_ajax_get_detalles', 'getDetalles');

add_action('wp_ajax_nopriv_get_detalles', 'getDetalles');

function getDetalles (){
   $id = $_POST['id'];
   $dormitorios = $_POST['dormitorios'];
   $campo1 = get_field($dormitorios, $id);

   echo json_encode([
        'respuesta1' => $campo1,
   ]);
    exit;
}
    
answered by 19.11.2018 в 19:36