ajax does not return data

0

I have the following code that should bring a response from a PHP file but it does not return anything. The answer should print it in an input. Any suggestions? Thank you very much!

$('#servicio select').on('change', function() {
  
  var servicioVal = $(this).val(); 

  //petición ajax
    $.ajax({
    type: 'POST',
    url: 'actionservrem.php',
    dataType: 'html',
    data: servicioVal,
    async : false,
    success: function(respuesta) {
      preciounitario =  respuesta;
    },
	
	
  });

  var $previousInput = $(this).closest('tr').find('td:first input:text');
  $previousInput.val(preciounitario);
  

});

actionservrem.php

<?php

include("includes/conexion.php");

$Servicio = $_POST["servicioVal"];

$resultPrecios = mysql_query("SELECT * FROM listasyservicios 
WHERE id_listasyservicios = '$Servicio'"); 

while($row = mysql_fetch_array($resultPrecios)){

echo ''.$row["precioventa"].'';


}

 
?>
    
asked by pointup 15.02.2018 в 14:21
source

2 answers

1

Assuming that the PHP code is correct and returns what it has to return, there are some problems in the JavaScript:

  • AJAX is asynchronous, not only is it enough to read the value and save it in a variable, you must perform the operations with the value within the success (because you do not know when it is going to be executed, so have the operations where know that the value will be available).

    //petición ajax
    $.ajax({
      type: 'POST',
      url: 'actionservrem.php',
      dataType: 'html',
      data: servicioVal,
      async : false,
      success: function(respuesta) {
        preciounitario =  respuesta;
    
        var $previousInput = $(this).closest('tr').find('td:first input:text');
        $previousInput.val(preciounitario);
      } 
    });
    
  • I do not know if you are passing the value correctly. You are indicating that you pass the value of the field, but you are not giving the parameter any name (which is then read in PHP as servicioVal ). Then, you would need to indicate that name:

    var servicioVal = $(this).val(); 
    
    
    //petición ajax
    $.ajax({
      type: 'POST',
      url: 'actionservrem.php',
      dataType: 'html',
      data: { "servicioVal" : servicioVal },
      async : false,
      success: function(respuesta) {
        preciounitario =  respuesta;
    
        var $previousInput = $(this).closest('tr').find('td:first input:text');
        $previousInput.val(preciounitario);
      } 
    });
    
  • Apart from that, you should read about SQL injection and how to avoid it in PHP. This code is vulnerable to such attacks.

        
    answered by 15.02.2018 в 16:00
    1

    The attribute AJAX data is used to send the parameters to the url indicated in AJAX. The way you are sending it is not the correct one, since in itself you are not sending any parameters and it must be like Alvaro Montoro says.

    Another thing that would change is the way to receive the response from the server and instead of receiving it in html, I would receive it in json.

    $('#servicio select').on('change', function() {
      
      var servicioVal = $(this).val(); 
    
      //petición ajax
        $.ajax({
        type: 'POST',
        url: 'actionservrem.php',
        dataType: 'json',
        data: { "servicioVal" : servicioVal },
        async : false,
        success: function(respuesta) {
          //Abre la consola de tu navegador para ver la respuesta
          console.log(respuesta);
          /**
          *** Haz lo que quieras en el DOM con la respuesta.
          **/
        },	
      });
    });

    <?php
    
    include("includes/conexion.php");
    
    $Servicio = $_POST["servicioVal"];
    
    $resultPrecios = mysql_query("SELECT * FROM listasyservicios 
    WHERE id_listasyservicios = '$Servicio'"); 
    
    $data = array();
    while($row = mysql_fetch_array($resultPrecios)){
      $data['results'][] = $row['precioventa'];
    }
    
    //Convierto mi array a json y lo retorno.
    return json_encode($data);
     
    ?>

    I leave you a real example of AJAX that returns a JSON, so you can see it from another perspective. link and the URL where the request is made this example link

        
    answered by 15.02.2018 в 23:05