How can I send data from ajax directly to a php method?

0

// what I want to do is pass directly parameters to (method) that is in a .php file, the problem I have is that the parameters reach the file but the function does not directly affect them what should I do?

// jquery.js file

$('#distritos').on('change', function(){
        let distrito= $('#distritos').val();

        $.ajax({
            url: 'cargar_provincias.php',
            type:'POST',
            data:{dato2:distrito}

        })
        .done(function(lista_rep3){

            if(lista_rep3 == "conectado a base de datos<option value=\"\">Elige</option>\r\n"){

                $('#provincias').html("<option value=\"\">no hay data</option>\r\n");

            }else{

                $('#provincias').html(lista_rep3);
            }
        })
        .fail(function(){
            alert('error al cargar ');
        });
    });

.php file where the data is received but not directly in the only method that has the same.

<?php
require_once 'conexion.php';

$dato=$_POST['dato2'];

echo getListPro($dato);

function getListPro($provincia){
    $con = getCon();
    $query = "SELECT commerce_district, commerce_province
FROM
  commerc
WHERE
  commerce_district ='$provincia'
GROUP BY
  commerce_province
 ORDER BY commerce_province, commerce_district";
    $result = $con->query($query);
    $listas_distritos = '<option value="">Elige</option>';
    $listas_distritos .= '';
    while($row = $result->fetch_array(MYSQLI_ASSOC)){
        if($row == "conectado a base de datos "){
          $listas_distritos .="no hay datos";
          break;
        }else{
          $listas_distritos .= "<option value='$row[commerce_province]'>$row[commerce_province]</option>";
        }

    }
    return $listas_distritos;
}


?>
    
asked by javier 26.10.2018 в 18:05
source

1 answer

0

You are not calling your function properly. Just like that they are called.

<?php
function foo() {
    echo "En foo()<br />\n";
}

function bar($arg = '')
{
    echo "En bar(); el argumento era '$arg'.<br />\n";
}

// Esta es una función de envoltura alrededor de echo
function hacerecho($cadena)
{
    echo $cadena;
}

$func = 'foo';
$func();        // Esto llama a foo()

$func = 'bar';
$func('prueba');  // Esto llama a bar()

$func = 'hacerecho';
$func('prueba');  // Esto llama a hacerecho()
?>

PHP variable functions

    
answered by 26.10.2018 в 18:22