So i've been working on this for days and it's driving me mad! It seems like it would be a pretty simple thing to do!
Heres what i'm trying to accomplish.
I have an admin section of website i'm building. It contains a "Role Manager" where an authorized admin can create user roles and assign them to users so they can perform certain tasks. It's a small-ish type script so the role manager is a bit on the small side and therefore I opted to use checkboxes for creating role permissions since there aren't that many.
While editing a role, as opposed to using straight PHP to update the record (I can do this easily enough w/o any issues as i've been coding on and off for years w/PHP - but i'm a relative newb when it comes to javascript & jQuery) - instead of presenting the admin with an actual form, I thought it would be fun to present the admin with images of empty / checked checkboxes. If the role has a permission, the checkbox is checked. If not - the checkbox image appears unchecked.
I have a bit of jQuery (using 1.11.1) that swaps the image out on click - just as if you were to click a checkbox in a form, it would input a check or remove it. This is functional.
Then I have a bit of jQuery to call my update script; This is where i'm running into issues! I can't seem to get the database record to update at all.
Before I go too deep into this post - I have a section where you can "Trash" a role. Basically throw it away but not delete it from the database. I use jquery to update this record / fade the table row out of view from the page and I have NO ISSUES! I only pass an "ID" to the "trash role" script whereas w/the edit script - I need to pass an ID, a database column name as well as a column value. I'm wondering if the way i'm passing the variables is part of the issue?
Anyway, Here's the jQuery that updates the trash role script and actually works
// Trash Role Script - jQuery 1.11.1 works
$(document).ready(function() {
$("#admin li a:last-child").click(function(e) {
var link = $(this).attr('href');
var pare = $(this).parent();
if (confirm('Trash Role?')) {
$.post(link, {}, function(data) {
if (data.result === 'Yes') {
$(pare).closest('tr').fadeOut('fast', function() {
$(this).remove();
});
e.preventDefault();
}
else {
alert(data.error);
e.preventDefault();
}
}, 'json');
}
else {
e.preventDefault();
}
e.preventDefault();
});
});
The actual link points ot a php script like so:
<a href=\"vl-role.trash.php?id={$userRoles->role_id}\" class=\"box\">Trash</a> <!-- basically id = 1 for example purposes -->
The PHP code i'm calling
<?php
require_once('../includes/classes/dl.config.php'); # defines database connection vars
require_once('../includes/classes/class.datalayer.php'); # the datalayer class i'm using
require_once('../includes/settings/vl-settings.load.php'); # connects to the database and defines $dl with a data source name
$updated_role = $dl->update('vlot_user_roles', array('role_trashed' => 1), array('role_id' => $getId)) or die($dl->getError()); # basically says update vlot_user_roles (values, role_trashed =1) where role_id = 1
if ($updated_role) {
echo '{"result":"Yes"}';
} else {
echo '{"error":"Error trashing the role!"}';
}
When clicking on the trash role link outlined above - the jquery posted above removes the table row it's contained in by fading it out - this works great so I thought I would use it as a template to update another mysql record.
The following code is INOP
$(document).ready(function() {
$(".edit").click(function(e) {
var pdata = {
id: "1", // hard coded values for testing
col: "role_edit_profile", // hard coded values for testing
colval: "1" // hard coded values for testing
};
var pare = $(this).parent();
$.post(pdata, {}, function(data) {
if (data.result === 'Yes') {
e.preventDefault(); // do Nothing but prevent link from being followed
}
else {
alert(data.error);
e.preventDefault();
}
}, 'json');
e.preventDefault();
});
});
I have the same link structure as before pointing to a similar file for php
<a href=\"vl-role.edit.php?id=$role_id&col=role_edit_profile&colval=1\"><img class=\"edit\" id=$role_id col=\"role_edit_profile\" colval=1 alt=\"unchecked\" title=\"unchecked\" src=\"../includes/assets/site_images/unchecked.png\" /></a> <!-- hard coded the values just for testing -->
PHP Code being called
require_once('../includes/classes/dl.config.php');
require_once('../includes/classes/class.datalayer.php');
require_once('../includes/settings/vl-settings.load.php');
$getCol = filter_input(INPUT_GET, 'col', FILTER_SANITIZE_STRING);
$getVal = filter_input(INPUT_GET, 'colval', FILTER_SANITIZE_NUMBER_INT);
$updated_role = $dl->update('vlot_user_roles', array($getCol => $getVal), array('role_id' => $getId)) or die($dl->getError());
if ($updated_role) {
echo '{"result":"Yes"}';
} else {
echo '{"error":"Error editing the role!"}';
}
For the life of me, I absolutely cannot see what the problem is and why i'm not actually getting an update to the database as it works in the first bit of code I posted and it doesn't seem that i'm doing much of anything differently. I've tried so many combinations of things it's hard to even begin to explain what i've tried and i've read up on .post - .get - .getJSON and the like - i'm not having any luck and i'm hoping someone can lend a helping hand and point out a hopefully simple mistake in the code. Thanks!