0

I have code in html inputs and checkbox the javascript code collects the data of the inputs and in ajax must send the information does not reach the php where it receives it and puts it in session.

ajax send the array for the php file, but the array is empty and i dont know what happen, im sorry my english is very ugly :(

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>

this is the code of the php file:

<?php
  session_start();
  if(!empty($_POST)) {
   //si llega id y cantidad
   $canti = $_POST['cantidades'];
   $_SESSION["canti"] = $canti;
  }
?>
  • Try using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent on your data you are trying to send to the endpoint, because the string you are building to send may not be encoding the `[]` of the array – Taplar Nov 09 '18 at 18:15
  • Possible duplicate of [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – miken32 Nov 09 '18 at 18:18
  • @Taplar the problem is more likely that they are trying to append an array to a string. – miken32 Nov 09 '18 at 18:19
  • var quantity1 = document.getElementById("quiantitynice1").value; It gives you the error, because in your html code, JS engine does not find any id called "quiantitynice1" . And once if u get JS error it doesn't go for further processing. – lifeisbeautiful Nov 09 '18 at 18:21
  • i solved the problem, in javascript the variable quantities , does not have the "var" and the php file is missing, but now it is more complex because the error that it says to me is: undefined, im going to edit the post – Juanjose Herrera Nov 09 '18 at 18:25
  • @Aravind Bhat K yeah if it exists, what happens is that I have 21 inputs and only paste in this post 2 the list is long but if it exists – Juanjose Herrera Nov 09 '18 at 18:27

1 Answers1

0

use these code in your ajax:

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