Ajax不发送变量,发送未定义

时间:2018-11-09 18:03:18

标签: javascript php jquery html ajax

我在html输入中有代码,并且选中了javascript代码以收集输入数据,而在ajax中,必须发送未到达php的信息并将其放入会话中。

ajax发送用于php文件的数组,但是数组为空,我不知道会发生什么,对不起,我的英语非常难看:(

function addcarto() {
    var quantity1 = document.getElementById("quiantitynice1").value;
    var quantity2 = document.getElementById("quiantitynice2").value;
    var quantity3 = document.getElementById("quiantitynice3").value;
    var quantity4 = document.getElementById("quiantitynice4").value;
    var quantity5 = document.getElementById("quiantitynice5").value;
    var quantity6 = document.getElementById("quiantitynice6").value;
    var quantity7 = document.getElementById("quiantitynice7").value;
    var quantity8 = document.getElementById("quiantitynice8").value;
    var quantity9 = document.getElementById("quiantitynice9").value;
    var quantity10 = document.getElementById("quiantitynice10").value;
    var quantity11 = document.getElementById("quiantitynice11").value;
    var quantity12 = document.getElementById("quiantitynice12").value;
    var quantity13 = document.getElementById("quiantitynice13").value;
    var quantity14 = document.getElementById("quiantitynice14").value;
    var quantity15 = document.getElementById("quiantitynice15").value;
    var quantity16 = document.getElementById("quiantitynice16").value;
    var quantity17 = document.getElementById("quiantitynice17").value;
    var quantity18 = document.getElementById("quiantitynice18").value;
    var quantity19 = document.getElementById("quiantitynice19").value;
    var quantity20 = document.getElementById("quiantitynice20").value;
    var quantity21 = document.getElementById("quiantitynice21").value;

    var quantities = [quantity1, quantity2, quantity3, quantity4, quantity5, quantity6, quantity7, quantity8, quantity9, quantity10, quantity11, quantity12, quantity13, quantity14, quantity15, quantity16, quantity17, quantity18, quantity19, quantity20, quantity21];
    
    $.ajax({
        type: 'post',
        url: 'php/addCart2.php',
        data: 'cantidades=' + quantities,
        //data: { 'cantidades': JSON.stringify(quantities)},
        success: function (response) {
            
            alert(response.status);
        },
        error: function () {
            alert("error");
        }
    });
}
<div class="form-check">
   <input class="form-check-input" name="ingrediente" type="checkbox" id="ing19" value="19">
   <label class="form-check-label" for="ing19">CAMARON</label>
</div>
<div class="quiantitynice" id="quiantitynice19" style='position:relative;display:none'>
   <input class="formgroup" type="number" name="quantity" id="quiantitynice19" min="1" value="" Style="width:45Px" placeholder="1">
   </div>
 
<div class="row text-center">
 <div class="col">
 <button class="btn btn-lg btn-wy" name ="btnsubmin" type = "submit" onclick="addcarto('');">AGREGAR</button>
  </div>
  </div>

这是php文件的代码:

<?php
  session_start();
  if(!empty($_POST)) {
   //si llega id y cantidad
   $canti = $_POST['cantidades'];
   $_SESSION["canti"] = $canti;
  }
?>

1 个答案:

答案 0 :(得分:0)

在您的ajax中使用这些代码:

 contentType: 'application/json; charset=utf-8',
 dataType: "JSON",