简体   繁体   中英

Why my php function does not receive the array I sent to it by ajax

I have an array that I obtain by means of an ajax query, and I need to send it to a function of a php file to manipulate it and use the elements that are in the indexes of the array.

This is the php function:

class ControladorCompraEfectivoYTarjeta {

   public function ctrCompraEfectivo(){

      if(isset($_POST["registroUsuario"])){

          if(preg_match('/^[a-zA-ZñÑáéíóúÁÉÍÓÚ ]+$/', $_POST["registroUsuario"]) &&
             preg_match('/^([0-2][0-9]|3[0-1])(\/|-)(0[1-9]|1[0-2])\2(\d{4})$/', $_POST["registroCalendario"]) &&
             preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/', $_POST["registroEmail"]) &&
             preg_match('/^(?:\D*\d){2,4}\D*$/', $_POST["registroDireccion"]) &&
             preg_match('/^[0-9]{7,12}$/', $_POST["registroTelefono"])) {

             //here is where I need to manipulate the array and be able to perform other tasks with its elements

}

The php function already receives POST variables, for that reason there are some validations inside. When trying to send the array with JSON.stringify by AJAX, if I do a print_r ($_POST) the browser tells me that only the variables I'm validating at the beginning of the function arrive, but nothing in the array

The array I get from an AJAX request, within this Javascript function:

$("#btnCheckout").click(function(){ 

    var total = $(".valorTotalCompra").html();
    var envio = $(".valorTotalEnvio").html();
    var subtotal = $(".valorSubtotal").html();
    var titulo = $(".valorTitulo");
    var cantidad = $(".valorCantidad");
    var valorItem = $(".valorItem");

    var idProducto = $('.cuerpoCarrito button, .comprarAhora button');

    var tituloArray = [];
    var cantidadArray = [];
    var valorItemArray = [];
    var idProductoArray = [];

    for(var i = 0; i < (titulo.length/2); i++){

        tituloArray[i] = $(titulo[i]).html();
        cantidadArray[i] = $(cantidad[i]).html();
        valorItemArray[i] = $(valorItem[i]).html();
        idProductoArray[i] = $(idProducto[i]).attr("idProducto");

    }

    var datos = new FormData();

    datos.append("total",total);
    datos.append("envio",envio);
    datos.append("subtotal",subtotal);
    datos.append("tituloArray",tituloArray);
    datos.append("cantidadArray",cantidadArray);
    datos.append("valorItemArray",valorItemArray);
    datos.append("idProductoArray",idProductoArray);

    $.ajax({

         url:rutaOculta+"ajax/carritoEfectivo.ajax.php",
         method:"POST",
         data: datos,
         success:function(response){
            console.log("The response is: ", response) }

    });

})

What do I have to do now, to be able to send this array to the function of the php file and be able to manipulate it?

If I do console.log of the array in Javascript, it looks like this:

array(3) {
  [0]=>
  array(4) {
    ["titulo"]=>
    string(25) "Crea aplicaciones con PHP"
    ["cantidad"]=>
    string(1) "1"
    ["valorItem"]=>
    string(2) "10"
    ["idProducto"]=>
    string(3) "400"
  }
  [1]=>
  array(4) {
    ["titulo"]=>
    string(29) "Vestido Clásico - 36 - negro"
    ["cantidad"]=>
    string(1) "7"
    ["valorItem"]=>
    string(2) "77"
    ["idProducto"]=>
    string(1) "3"
  }
  [2]=>
  array(4) {
    ["titulo"]=>
    string(29) "Aprende Javascript desde Cero"
    ["cantidad"]=>
    string(1) "1"
    ["valorItem"]=>
    string(2) "10"
    ["idProducto"]=>
    string(3) "401"
  }
}

You're receiving an array of three elements in the $_POST. Iterate through them to get at the indexes you're targeting:

foreach($_POST as $i => $el) {
      if(isset($el["registroUsuario"])){...

And currently you're looking for an index that doesn't exist, at least in the data sample you posted:

$_POST["registroUsuario"]

Your code has to use the same index names that are sent in the data.

$_POST[0]["titulo"]

do a

echo "<pre>";
print_r($_POST);
echo "</pre>"; 

to get some insight as to how your data looks when received on the server. This should tell you how to handle it properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM