【问题标题】:Why my php function does not receive the array I sent to it by ajax为什么我的 php 函数没有收到我通过 ajax 发送给它的数组
【发布时间】:2018-12-11 19:28:59
【问题描述】:

我有一个通过 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"
  }
}

【问题讨论】:

  • 您使用的是什么请求内容类型以及您得到什么响应状态?
  • 是否在某处定义了rutaOculta 变量(用于AJAX 请求URL)?
  • @DarraghEnright 我定义的 ajax 是它获取数组的原因。这不是问题所在。问题是如何将我从 ajax 获得的数组发送到我在问题开头放置的函数。

标签: javascript php arrays ajax


【解决方案1】:

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

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

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

$_POST["registroUsuario"]

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

$_POST[0]["titulo"]

做一个

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

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

【讨论】:

  • 您好!您所说的索引名称不一致,这就是为什么我在问题中间解释的原因,当我澄清 if 中的这些验证是通过已经来自另一方的 post 变量时。
猜你喜欢
  • 2021-05-24
  • 2018-03-27
  • 2014-04-16
  • 2020-01-07
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多