Hi, leider muss ich mich nochmal an euch wenden, denn leider finde ich den Fehler nicht...
Gestern bemerkte ich dass mein Script nicht mehr richtig funktioniert. Als ich es geschrieben habe hat es noch einwandfrei funktioniert. Technisch verwende ich JQuery um die Ajaxabzuschicken, um eine rückgabe zu erhalten greife ich schlicht auf das "echo" zurück, welches von Ajax zuerst abgefangen wird.
Script:
<script>
function main() {
$('#createLink').on('click', createLink);
$('#deleteAccount').on('click', deleteAccount);
$('#lockAccount').on('click', lockAccount);
}
function createLink() {
$('#cont').empty();
$('#cont').append('<h2>Create Link</h2>');
$('#cont').append('<form id="linkForm">');
$('#linkForm').append('<input type="text" placeholder="Name of your Link" name="link">');
$('#linkForm').append('<input type="hidden" name="hidden" value="1">')
$('#linkForm').append('<input type="submit" value="Save">');
$('#linkForm').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: '/forms/linkForm.php',
data: $('#linkForm').serializeArray(),
type: 'post',
success: function(data) {
$('#messageBox').html(data);
}
});
});
}
function deleteAccount() {
$('#cont').empty();
alert('Attention! You are about to delete your Account!');
$('#cont').append('<p>Do you want to delete your account now?</p>');
$('#cont').append('<form id="deleteForm">');
$('#deleteForm').append('<input type="password" name="password" placeholder="enter your password">');
$('#deleteForm').append('<input type="hidden" name="hidden" value="1">');
$('#deleteForm').append('<input type="submit" value="Delete">');
$('#deleteForm').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: '/forms/deleteForm.php',
data: $('#deleteForm').serializeArray(),
type: 'post',
success: function(data) {
$('#messageBox').html(data);
if(data == "One moment please...") {
window.location.href = "/log-out.php";
}
}
});
});
}
function lockAccount() {
$('#cont').empty();
alert('Attention! You are about to lock your Account');
var date = new Date();
var day = date.getDate()+1;
var month = date.getMonth()+1;
var sixthMonth = date.getMonth()+7;
var year = date.getFullYear();
if(day < 10) {
day = '0' + day
}
if(month < 10) {
month = '0' + month
}
if(sixthMonth < 10) {
sixthMonth = '0' + sixthMonth
}
var currentDate = year + "-" + month + "-" + day;
var inSixMonths = year + "-" + sixthMonth + "-" + day;
var valueDate = '<input type="date" name="date" value="' + inSixMonths + '" min="' + currentDate + '">'
$('#cont').append('<p>Do you want to lock your account now?</p>');
$('#cont').append('<form id="lockForm">');
$('#lockForm').append('<input type="password" name="password" placeholder="enter your password">');
$('#lockForm').append(valueDate);
$('#lockForm').append('<input type="hidden" name="hidden" value="1">');
$('#lockForm').append('<input type="submit" value="Lock account">');
$('#lockForm').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: '/forms/lockForm.php',
data: $('#lockForm').serializeArray(),
type: 'post',
success: function(data) {
$('#messageBox').html(data);
if(data == "One moment please...") {
window.location.href = "/log-out.php";
}
}
});
});
}
window.addEventListener('load', main);
</script>
Alles anzeigen
php:
<?php
if(empty($_POST['link']) or empty($_POST['hidden'])) {
foreach($_POST as $key => $value) {
if(empty($value) or !isset($value)) {
echo "$key is empty.<br>";
}
}
exit;
}else {
$link = $_POST['link'];
$link = strip_tags($link);
$hidden = $_POST['hidden'];
$hidden = strip_tags($hidden);
session_start();
require '../../inc/db/connect.php';
$checkLink = $connection ->prepare("SELECT link FROM linksystem WHERE link=?");
$checkLink -> execute([$link]);
$linkExitsts = $checkLink -> fetchColumn();
if($linkExitsts) {
echo "Sorry, this Link already exists. Please choose a different name";
$connection = null;
exit;
}else {
$id = $_SESSION['userDatas']['id'];
$saveLink = $connection -> prepare("UPDATE linksystem SET link = ? WHERE userid = ? LIMIT 1");
if($saveLink -> execute([$link, $id])) {
echo "Link wurde gesetzt.";
$connection = null;
exit;
}
}
}
?>
Alles anzeigen
Zu beachten ist jetzt das JS. Es scheint alles außer die AJAX request zu funktionieren. Zumindest nicht immer. Die ajax in createLink funktioniert gar nicht, die in lockAccount anscheinend schon, gibt aber nur ein Resultat zurück, wenn alles stimmt, der Account also gelocked wurde.
Jetzt frage ich mal in die Runde: Liegts daran dass ich kein json_decode verwende, oder hat es andere Hintergründe. ( Sonst sehe ich jeden Code im Netz mit json_decode arbeiten, da die standard Variante aber auch funktionierte sah ich keinen Grund das zu nutzen )
EDIT: Errors gibts nicht, urls sind richtig, sonst würde es bei dem lockAccount() ja auch keine Rückgabe geben.