I'm coding the backend of a project and in the module that I'm trying to implement I need to manage users, like, get all the data from the table Users and also get just the data relative to the selected user using an input text, for example. It also has to be able to update and delete records.
So I already coded all the functions and they have to be ok but, the point comes in the MVC architecture. It's my first time working under this requirement and it's a bit messy.
Ordered respectively:
Model - View - Controller - JS Files
Where I have all the functions - My HTML - PHP function setter and caller - Ajax Handler
With this structure, let's check the code:
Model (Select * from users query)
public function listUsers(){ //check this
$result = $db->prepare('SELECT * FROM users');
if ($result->execute()){
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
echo '<table> <tr><td>DNI</td><td>Username</td><td>Firstname</td><td>Lastname</td><td>Email</td><td>Phone</td><td>Address</td><td>City</td><td>Postal</td><td>Province</td><td>CC</td><td>Admin</td><td>Creation</td></tr>';
echo '<tr><td>' . $row['dni'] . '</td><td>' . $row['username']. '</td><td>' . $row['firstname'] . '</td><td>' . $row['lastname'] . '</td><td>' . $row['email'] . '</td><td>' . $row['phone'] . '</td><td>' . $row['address'] . '</td><td>' . $row['city'] . '</td><td>' . $row['postal'] . '</td><td>' . $row['province'] . '</td><td>' . $row['creditcard'] . '</td><td>' . $row['creation'] . '</td><td>' . $row['isadmin'] . '</td></tr>';
}
}else{
echo 'There was an error during the execution. (listUsers)';
}
}
Controller
<?php
//list user from administrative panel controller
require "../model/backend.php";
$dbcom = new dbInteraction;
if(isset($_POST['action'])){
switch($_POST['action']){
case 'listUsers':
$dbcom->listUsers();
break;
default:
break;
}
}
$dbcom->conclose();
?>
AJAX Handler - It's supposed be the sender of the action to the controller
$(document).ready(function() {
$("#listUsers").click(function(e){
e.preventDefault(); // prevents submit event if button is a submit
ajax_route('listUsers');
});
function ajax_route(action_requested){
$.post("../controller/umanagement.php", {action : action_requested}, function(data){
if (data.length>0){
alert("Hoorah! Completed the action requested: "+action_requested);
}
})
}
}
And the last one, the button from the view which everything is started - View
<button id="listUsers">List all users</button>
I have been reading here in stackoverflow different methods to perform this actions and, actually, I'm using one of these suggested methods but it still does not working.
Are the params sent/received properly? It could be a good start point.
Any help will be really appreciated.
After some checks, my final and working code is the following one:
function op_User(action_requested){
var username = $('#username').val();
$.ajax({ url: '/youtube/controller/umanagement.php',
data: {
action: action_requested,
username: username
},
type: 'post',
success: function(output) {
document.write(output);
},
error: function(){
alert('ajax error');
}
});
}
So that's it, as @gskema and @VIDesignz said, the problem was the relative path in the ajax function. It has to be full path.
Thanks and hope it helps