Json_encode返回所有站点

时间:2015-08-22 15:16:08

标签: php jquery json

我的代码有问题,我希望将json_encode的信息发送到jQuery和AJAX脚本,但是当我使用这个脚本时,我得到所有内容网站而不是准确的数据。请帮帮我。

我的用户控制器文件:

<?php defined('securityCMS') or die('Nie posiadasz uprawnień do przeglądania tego pliku!');

class ControllerUsers extends Controller {

protected $model;   
protected $controller_petle;

public function __construct() {
    $this->model = new ModelUsers();
    $this->controller_petle = new ControllerPetle();
}

public function manageUsers() {
    return View::make('viewSystem/users/manageUsers')->set('showUsers',$this->controller_petle->petlaUsers());
}

public function addUser() {       
    if($_SERVER['REQUEST_METHOD'] === 'POST') {           
        if($this->model->addUser($_POST['login'], $_POST['email'], $_POST['first_name'], $_POST['last_name'], $_POST['password'], $_POST['rp_password'], array($_POST['ranga_podzial'], $_POST['range_news'], $_POST['range_gallery'], $_POST['ranga_setting'], $_POST['range_layout'], $_POST['range_other']))) {
            json_encode('good');
            http_response_code(200);
            return View::make('viewSystem/users/addUser')->set('error', $this->loadError('goodAdd'));
        }else{
          json_encode('bad');
            http_response_code(500);
            return View::make('viewSystem/users/addUser')->set('error', $this->loadError('badAddUser'));
        }
    }else{
        json_encode('nothing');
        return View::make('viewSystem/users/addUser')->set('error', NULL);
    }
}
public function actionIndex() {
    if($_GET['f'] == 'manageAccounts') {
      return $this->manageUsers(); 
    }elseif($_GET['f'] == 'addAccount'){
      return $this->addUser(); 
    }elseif($_GET['f'] == 'editUser') {
      return $this->editUser();
    }elseif($_GET['f'] == 'editUserPassword') {
      return $this->changePassword();
    }elseif($_GET['f'] == 'editUserRange') {
      return $this->changeRange();
    }elseif($_GET['f'] == 'deleteUser') {
      return $this->deleteUser(); 
   }else{
      return View::make('viewSystem/errorSite'); 
   }
  }
 }
?>

模型用户

<?php defined('securityCMS') or die('Nie posiadasz uprawnień do przeglądania tego pliku!');

Model Users

class ModelUsers extends Model {

//Loading the database connection
function __construct() {
    parent::__construct();
}

// Show all users
public function showUsers() {
    $allUsers = $this->pdo->query("SELECT * FROM `cms_users` WHERE `login`!='MysteriousCore' ORDER BY id DESC");
    return $allUsers;
}

// Show information about user
public function showInfoUser($id) {
    $getInfoUser = $this->pdo->prepare("SELECT * FROM `cms_users` WHERE `id`=:id");
    $getInfoUser->bindValue(':id', $id, PDO::PARAM_INT);
    $getInfoUser->execute();

    $showInfoUser = $getInfoUser->fetch(PDO::FETCH_ASSOC);

    return $showInfoUser;
 }

// Add new user
public function addUser($login, $email, $first_name, $last_name, $password, $rp_password, $range) {
    $range = array();
    $hash_password = sha1('0aWxEMkp'.$password.'6gGZAzAn');

    $checkLogin = $this->pdo->prepare("SELECT COUNT(*) AS total FROM `cms_users` where `login`=:login OR `email`=:email");
    $checkLogin->bindValue(':login', $login, PDO::PARAM_STR);
    $checkLogin->execute();

    $result = $checkLogin->fetch(PDO::FETCH_ASSOC);

    if($result['total'] != 1) {
        if(!empty($login) && !empty($email) && !empty($first_name) && !empty($password) && !empty($rp_password)) {
            if($password === $rp_password) {
                // Add informations about user
                $add = $this->pdo->prepare("INSERT INTO `cms_users`(`login`, `email`, `first_name`, `last_name`, `password`)
                VALUES (:login, :email, :first_name, :last_name, :password)");
                $add->bindValue(':login', $login, PDO::PARAM_STR);
                $add->bindValue(':email', $email, PDO::PARAM_STR);
                $add->bindValue(':first_name', $first_name, PDO::PARAM_STR);
                $add->bindValue(':last_name', $last_name, PDO::PARAM_STR);
                $add->bindValue(':password', $hash_password, PDO::PARAM_STR);
                $add->execute();

                // Add user range
                $addRange = $this->pdo->prepare("INSERT INTO `cms_range`(`login`, `podzialy`, `news`, `gallery`, `users`, `setting`, `layout`, `other`)
                VALUES (:login, :podzialy, :news, :gallery, '0', :setting, :layout, :other)");
                $addRange->bindValue(':login', $login, PDO::PARAM_STR);
                $addRange->bindValue(':podzialy', $range[0], PDO::PARAM_INT);
                $addRange->bindValue(':news', $range[1], PDO::PARAM_INT);
                $addRange->bindValue(':gallery', $range[2], PDO::PARAM_INT);
                $addRange->bindValue(':setting', $range[3], PDO::PARAM_INT);
                $addRange->bindValue(':layout', $range[4], PDO::PARAM_INT);
                $addRange->bindValue(':other', $range[5], PDO::PARAM_INT);
                $addRange->execute();

                return TRUE;
            }else{
              return FALSE;
            }
        }else{ return FALSE;
       }
     }
    }
  }
 ?>

显示所有内容的文件:

<?php defined('securityCMS') or die('Nie posiadasz uprawnień do przeglądania tego pliku!');

class ControllerSystem extends Controller {

protected $model;
protected $model_login;
protected $controller_petle;
protected $controller_podzial;
protected $controller_articles;
protected $controller_gallery;
protected $controller_setting;
protected $controller_view;
protected $controller_other;
protected $controller_login;

public function __construct() {
    $this->model                = new ModelGlobal();
    $this->model_login          = new ModelLogin($_SESSION['loginCMS'], $_SESSION['passwordCMS']);
    $this->controller_petle     = new ControllerPetle();
    $this->controller_podzial   = new ControllerPodzial();
    $this->controller_articles  = new ControllerArticles();
    $this->controller_gallery   = new ControllerGallery();
    $this->controller_users     = new ControllerUsers();
    $this->controller_setting     = new ControllerSetting();
    $this->controller_view         = new ControllerView();
    $this->controller_other     = new ControllerOther();
    $this->controller_login     = new ControllerLogin();
}

public function actionIndex() {
    return View::make('viewSystem/index')->set('count', array('news' => $this->model->showAllCount('website_news')))->set('attempts', $this->controller_petle->petlaAttempts());
}

public function loadSystem() {
    if($_GET['s'] == 'users') {
        if($this->model_login->checkRang('users')) {
            echo $this->controller_users->actionIndex();
        }else{
            echo $this->loadError('badRang');
        }
    }else{
      echo $this->actionIndex();
    }

    if($_GET['s'] == 'articles' || ($_GET['s'] == 'adoptions' && $_GET['f'] == 'manageAdoptions') ||
    ($_GET['s'] == 'eksperci' && $_GET['f'] == 'addAnswer') || $_GET['s'] == 'other' || $_GET['f'] == 'uploadFile' || $_GET['s'] == 'gallery')
    {
        return View::make('viewSystem/global/footer-news');
    }

    elseif($_GET['s'] != 'logout')
    {
        return View::make('viewSystem/global/footer');
    }
  }
}

我的jQuery脚本:

$(document).ready(function() {

$('button[name=add-user]').click(function() {
    $.ajax({
        url: window.location.href,
        type: 'POST',
        data: $('form.formAjax').serialize()
    }).then(
        function(error)
        {
            console.log(error);
        },
        function(error)
        {
            console.log(error);
        }   
    );
    event.preventDefault();
   });
 });

1 个答案:

答案 0 :(得分:1)

您的ajax请求正在将POST发送到同一页面(window.location.href),因此除非指定此页面的一部分来处理POST请求,否则它自然会返回该页面的内容。< / p>

也许您可以尝试以下方式: -

if( $_SERVER['REQUEST_METHOD']=='POST' ){
    /* If you are using output buffering we are not interested in the buffer */
    @ob_clean();

    /* Process the POST request */

    /* Construct response to send back to ajax callback */

    /* send response */

    exit();
}