I try to send a JS array to a php file via ajax.
JS code:
$('#basket').on('click',function(){
$.ajax({
type: "GET",
url: "basket.php",
data: {vektor: itemNameArray},
success: function(){
window.location.href ="basket.php";
console.log(itemNameArray);
}
});
});
php code:
<?php
echo("<script>console.log('PHP: test 1');</script>");
if(isset($_GET['vektor'])){
echo("<script>console.log('PHP: test 2');</script>");
$vektor = $_post['vektor'];
echo("<script>console.log('PHP: ".$vektor."');</script>");
}
But it seems that my get with key vektor is not working. I get in LOG my array and the first echo with test 1 but not second. I need to send a array to php file to create a html code. That is becuse I need to open "basket.php" to see result.
I have changed GET and post to POST. You messed with POST and GET, that the first major mistake.
But it seems that my get with key vektor is not working. I get in LOG my array and the first echo with test 1 but not second (post not POST). I need to send a array to php file to create a html code.(for that type of data POST is preferable) That is because I need to open "basket.php" to see result.
$('#basket').on('click',function(){
$.ajax({
type: "POST",
url: "basket.php",
data: {vektor: itemNameArray},
success: function(){
//window.location.href ="basket.php";
console.log(itemNameArray);
}
});
});
php code:
<?php
echo("<script>console.log('PHP: test 1');</script>");
if(isset($_POST['vektor'])){
echo("<script>console.log('PHP: test 2');</script>");
$vektor = $_POST['vektor'];
echo("<script>console.log('PHP: ".$vektor."');</script>");
}
Hi you can do it this way:
your php script:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'SLC':
if (isset($_POST["id"])) {
$id = $_POST["id"];
if (is_int($id)) {
$query = "select * from alumni_users where userId = '$id' ";
$update = mysqli_query($mysqli, $query);
$response = array();
while($row = mysqli_fetch_array($update)){
.......fill your response here
}
echo json_encode($response);
}
}
break;
}
}
Where action is a command you want to do SLC, UPD, DEL etc and id is a parameter
then in your ajax:
function getdetails() {
var value = $('#userId').val(); // value can be your array ! note: if you send a object json_encode(json_decode(,MyObj,true))
return $.ajax({
type: "POST",
url: "getInfo.php",
data: {action: "SLC",id: value }
})
}
call it like this:
getdetails().done(function(response){
var data=JSON.parse(response);
if (data != null) {
//do somthing with your Data
}
})