首页 > 解决方案 > 为什么我的 php 函数没有收到我通过 ajax 发送给它的数组

问题描述

我有一个通过 ajax 查询获得的数组,我需要将它发送到一个 php 文件的函数来操作它并使用数组索引中的元素。

这是php函数:

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

}

php 函数已经接收 POST 变量,因此内部有一些验证。当尝试通过 AJAX 使用 JSON.stringify 发送数组时,如果我执行 print_r ($_POST),浏览器会告诉我只有我在函数开头验证的变量到达,但数组中没有任何内容

我从 AJAX 请求中得到的数组,在这个 Javascript 函数中:

$("#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) }

    });

})

我现在必须做什么才能将这个数组发送到 php 文件的函数并能够对其进行操作?

如果我在 Javascript 中执行数组的 console.log,它看起来像这样:

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"
  }
}

标签: javascriptphparraysajax

解决方案


您在 $_POST 中收到一个包含三个元素的数组。遍历它们以获取您要定位的索引:

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

目前您正在寻找一个不存在的索引,至少在您发布的数据样本中:

$_POST["registroUsuario"]

您的代码必须使用在数据中发送的相同索引名称。

$_POST[0]["titulo"]

做一个

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

深入了解您的数据在服务器上接收时的外观。这应该告诉您如何正确处理它。


推荐阅读