ajax dont send the variables, send undefined
up vote
0
down vote
favorite
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;
?>
javascript php jquery html ajax
|
show 1 more comment
up vote
0
down vote
favorite
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;
?>
javascript php jquery html ajax
Try using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… on your data you are trying to send to the endpoint, because the string you are building to send may not be encoding theof the array
– Taplar
Nov 9 at 18:15
Possible duplicate of How to get useful error messages in PHP?
– miken32
Nov 9 at 18:18
@Taplar the problem is more likely that they are trying to append an array to a string.
– miken32
Nov 9 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.
– Aravind Bhat K
Nov 9 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 9 at 18:25
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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;
?>
javascript php jquery html ajax
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;
?>
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>
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>
javascript php jquery html ajax
javascript php jquery html ajax
edited Nov 9 at 18:32
asked Nov 9 at 18:03
Juanjose Herrera
166
166
Try using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… on your data you are trying to send to the endpoint, because the string you are building to send may not be encoding theof the array
– Taplar
Nov 9 at 18:15
Possible duplicate of How to get useful error messages in PHP?
– miken32
Nov 9 at 18:18
@Taplar the problem is more likely that they are trying to append an array to a string.
– miken32
Nov 9 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.
– Aravind Bhat K
Nov 9 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 9 at 18:25
|
show 1 more comment
Try using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… on your data you are trying to send to the endpoint, because the string you are building to send may not be encoding theof the array
– Taplar
Nov 9 at 18:15
Possible duplicate of How to get useful error messages in PHP?
– miken32
Nov 9 at 18:18
@Taplar the problem is more likely that they are trying to append an array to a string.
– miken32
Nov 9 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.
– Aravind Bhat K
Nov 9 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 9 at 18:25
Try using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 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 9 at 18:15
Try using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 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 9 at 18:15
Possible duplicate of How to get useful error messages in PHP?
– miken32
Nov 9 at 18:18
Possible duplicate of How to get useful error messages in PHP?
– miken32
Nov 9 at 18:18
@Taplar the problem is more likely that they are trying to append an array to a string.
– miken32
Nov 9 at 18:19
@Taplar the problem is more likely that they are trying to append an array to a string.
– miken32
Nov 9 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.
– Aravind Bhat K
Nov 9 at 18:21
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.
– Aravind Bhat K
Nov 9 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 9 at 18:25
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 9 at 18:25
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
use these code in your ajax:
contentType: 'application/json; charset=utf-8',
dataType: "JSON",
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
use these code in your ajax:
contentType: 'application/json; charset=utf-8',
dataType: "JSON",
add a comment |
up vote
0
down vote
accepted
use these code in your ajax:
contentType: 'application/json; charset=utf-8',
dataType: "JSON",
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
use these code in your ajax:
contentType: 'application/json; charset=utf-8',
dataType: "JSON",
use these code in your ajax:
contentType: 'application/json; charset=utf-8',
dataType: "JSON",
answered Nov 10 at 7:35
shaghayegh sheykholeslami
986
986
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231122%2fajax-dont-send-the-variables-send-undefined%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try using developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 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 9 at 18:15
Possible duplicate of How to get useful error messages in PHP?
– miken32
Nov 9 at 18:18
@Taplar the problem is more likely that they are trying to append an array to a string.
– miken32
Nov 9 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.
– Aravind Bhat K
Nov 9 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 9 at 18:25