dongma7725 2015-12-09 18:16
浏览 87

使用自定义MVC从JQuery ajax(),PHP和MySQL检索JSON数据

I have been searching everywhere and I am not getting anywhere. This is what I am using: Custom PHP MVC this is what I am trying to get: From userView.php I want to replace an id="ShowName" with JSON data from a ajax Call using JQuery

This is what I have: userController.php

if (isset($_POST['id'])) {
    $this->getView('users', $this->object->getUser($_POST['id']));
}

userModel.php

public function getUser($id) {
    $this->user = array();

    parent::getConnection();
    $this->sql = $this->dbh->query("SELECT * FROM User WHERE idUser='". $id ."'");
    parent::errorHandler();
    $this->results = $this->sql->fetch(PDO::FETCH_ASSOC);
    $this->user[] = array(
        'name' => $this->results['name'],
        'lastname' => $this->results['lastname'],
        'email' =>  $this->results['email']
        );
    $obj['user'] = $this->user;

    parent::closeConnection();
    header('Content-Type: application/json');
    print json_encode($obj);

}

userView.php

<select class="form-control blue select" id="getUserID">
    <option value="default">Seleccione un Usuario</option>
    <?php $this->object->getUserslist();?>
</select>

<span id="name"></span>

Method getUsersList() inside userModel

public function getUsersList() {

    parent::getConnection();
    $this->sql = $this->dbh->query("SELECT * FROM User ORDER BY name ASC");
    parent::errorHandler();
    while($this->results = $this->sql->fetch(PDO::FETCH_ASSOC)) {
        echo "<option value='" . $this->results['idUser'] . "'>" . $this->results['name'] . "</option>";
    }
    parent::closeConnection();

}

JQuery

$("#getUserID").change(function(){
    var userID = $("#getUserID").val();
    $.ajax({
        type: 'POST',
        url: 'user',
        data: 'id='+userID,
        dataType: 'json',
        success: function(data){
            var obj = $.parseJSON(data);
            alert(obj.name)
        }
    });
});

It retrieves JSON data at the very top of the html file, but the alert is not prompting, so I guess it gets data before the success is called.

Direct Question: How can I retrieve the JSON data from PHP so I can use it in html from JQUERY ajax Response?

Thanks!!!!

  • 写回答

1条回答

  • douju2012 2015-12-09 18:44
    关注

    In userModel.php

    change

    $this->user[] = array(
        'name' => $this->results['name'],
        'lastname' => $this->results['lastname'],
        'email' =>  $this->results['email']
        );
    $obj['user'] = $this->user;
    

    to:

    $obj = array(
        'name' => $this->results['name'],
        'lastname' => $this->results['lastname'],
        'email' =>  $this->results['email']
        );
    

    See if the alert prompt returns what you expect.

    评论

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'