dqb14659 2015-11-28 21:13
浏览 19
已采纳

too long

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.

  • 写回答

1条回答 默认 最新

  • duanbiao4025 2015-12-01 23:07
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用