I am trying to update a SQL table after a user clicks on a button. I am using Laravel, and having trouble making the ajax call work, i.e. i get a 404 response. My current approach was to route the request to a Route that would pass it to the controller. Could anyone please help me understand my mistake(s)?
ROUTE
Route::post('/controllers/AccountController', array(
'as' => 'userManagementAjax',
'uses' => 'AccountController@deleteAccount'
));
CONTROLLER
public function deleteAccount(){
if($_POST['action'] == "delete") {
DB::table('users')
->where('email', $_POST['email'])
->update(array('suspended' => 1));
echo "ok";
}
}
HTML & JAVASCRIPT
<script>
function rmuser(a) {
alert('Removing user: '+a);
$.ajax({
url: '/controller/userManagementAjax',
type: 'POST',
data: {'action': 'delete', 'email': a},
success: function(data, status) {
if(data == "ok") {
$('#tr'+a).remove();
}
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
}); // end ajax call
};
</script>
<button class="btn" onclick="rmuser('some_email')">Delete</button>