PHP Function call with AJAX - Update cookie
Get the solution ↓↓↓I'm currently trying to update my cookie in a online-store. When the amount of a specific product in the store is updated, the cookie has to be updated and reload the page.
I'm trying to do this with aonchange
event withJQuery
.
I coded this as follows:
$.ajax({
type: 'POST',
url: "./DAO/WinkelwagenDAO.php",
data: {function: "UpdateAantal", productId: productId, Aantal: aantal},
success: function (data) {
console.log("Succeeded!", data);
//location.reload(); //To reload page
},
error: function (err) {
console.log("Something went wrong in the AJAX-request: ", err);
}
});
});
This works and is logging the correct things.
Now for my PHP-file I catch this event:
if (isset($_POST['function'])) {
$action = $_POST['function'];
switch ($action) {
case 'UpdateAantal' :
echo "\nAmount to change for ProductId: " . $_POST['productId'] . " use function: " . $_POST['function'] . " set amount to: " . $_POST['Aantal'];
WinkelwagenDAO::UpdateAantal($_POST['productId']);
}
}
Here theUpdateAantal()
-function will be called which is defined inside this php_file. Therefore I have to include another PHP file to get access to some getters/setters. (I think there is also a problem here because when I doinclude_once './Model/WinkelwagenItem.php';
it gives a warning that file is not found (Altough theclass/class/...
IS CORRECT). When I do aFile_exists
like in the code below it says that file DOES exist (a bit stuck here)
public static function UpdateAantal($productId) {
if (file_exists('Model/WinkelwagenItem.php')) {
//echo "\nFile that you wanted to import in winkelwagenDAO does exist!\n<br>";
include_once 'Model/WinkelwagenItem.php';
} else {
//echo "File that you wanted to import in WinkelwagenDAO does NOT exist!";
}
echo "\nUpdateAantal function called";
$Array = self::getWinkelwagenItems();
echo $Array[0]->getProductId(); //HERE IT GOES WRONG
if (isset($_COOKIE["winkelwagen"])) { //COOKIE EXISTS?
$Array = self::getWinkelwagenItems();
$index = 0; //positie bijhouden
foreach ($Array as $product) {
if ($productId == $Array[$index]->getProductId()) {
//echo $productId . " == " . $Array[$index]->getProductId();
$Array[$index]->setAantal($_POST['Aantal']);
$SerializeArray = serialize($Array);
setcookie("winkelwagen", $SerializeArray);
break; //Quit if match
} else {
$index++;
}
}
} else {
echo "This cookie did not exist";
}
}`
This is what I get in the console (error at the bottom) :
Seems that it can not call the->GetProductID()
because of theinclude_once()
is probably not done right? However when I do include_once it gives warning that: no such file in directory, I already double checked this and file does exist. It says it DOES EXISTS when i use if (File_exists in php).
Source/Model/WinkelwagenItem -> Getters and setters
Source/DAO/WinkelwagenDAO -> UpdateAantal function
Can anyone help me out ? Been trying all day
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.