Hello Stackers,
I'm having a lot of trouble with AJAX. I Want ajax to change a color for a div. First directly, which it does. Then it should save it in a Database, which it DOES NOT. No other questions on stackoverflow contained a solution for me.
The problem is this: It does not alter the column in the database. It's not stored.
The jQuery requesting the AJAX file
$(document).ready(function() {
var uid = <?php echo $user['id']; ?>;
$('#blue').click(function() {
$('#upbar').css({
'background-color': '#3498db',
'background-image': 'none',
});
$.ajax({
method: 'post',
url: 'app/tpl/skins/Magical/includes/ajax.php',
data: {
action: 'update_blue',
id: uid,
}
})
});
$('#purple').click(function() {
$('#upbar').css({
'background-color': '#9b59b6',
'background-image': 'none',
});
$.ajax({
method: 'post',
url: 'app/tpl/skins/Magical/includes/ajax.php',
data: {
action: 'update_purple',
id: uid,
}
})
});
$('#red').click(function() {
$('#upbar').css({
'background-color': '#e74c3c',
'background-image': 'none',
});
$.ajax({
method: 'post',
url: 'app/tpl/skins/Magical/includes/ajax.php',
data: {
action: 'update_red',
id: uid,
}
})
});
$('#yellow').click(function() {
$('#upbar').css({
'background-color': '#f39c12',
'background-image': 'none',
});
$.ajax({
method: 'post',
url: 'app/tpl/skins/Magical/includes/ajax.php',
data: {
action: 'update_yellow',
id: uid,
}
})
});
$('#image1').click(function() {
$('#upbar').css({
'background-image': 'url("app/tpl/skins/Magical/assets/images/header-magie.png"),
});
$.ajax({
method: 'post',
url: 'app/tpl/skins/Magical/includes/ajax.php',
data: {
action: 'update_image1',
id: uid,
}
})
});
});
ajax.php
error_reporting(E_ALL);
ini_set('display_errors', '1');
if(!isset($_POST["action"])) {
exit("fail");
}
$user = $_POST['id'];
switch ($_POST["action"]) {
case "update_blue":
echo "1";
$update = mysql_query("UPDATE users SET magical_header = '1' WHERE id = '".$user."'")or die(mysql_query());
break;
case "update_purple":
echo "2";
$update = mysql_query("UPDATE users SET magical_header = '2' WHERE id = '".$user."'");
break;
case "update_red":
echo "3";
$update = mysql_query("UPDATE users SET magical_header = '3' WHERE id = '".$user."'");
break;
case "update_yellow":
echo "4";
$update = mysql_query("UPDATE users SET magical_header = '4' WHERE id = '".$user."'");
break;
case "update_image1":
echo "5";
$update = mysql_query("UPDATE users SET magical_header = '5' WHERE id = '".$user."'");
break;
default:
echo 'I did not know that you were there.';
}
Am I overlooking something here? Why this is not offtopic; The expected behaviour is reported above, I Want a DIV to recolor, however, it didn't save, and that's the issue, again, clearly stated above. All the code can be used to reproduce the error, and there is no minimal version of it. This is the code which needed to be fixed, not a complete other one.