donglvhe7591 2015-08-22 15:16
浏览 41

Json_encode返回所有站点

I have problem with my code, I want send informations for json_encode to jQuery and AJAX script but when I use this script I get all contents site instead accurate data. Please help me with that.

My users controller file:

<?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'); 
   }
  }
 }
?>

Model users

<?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;
       }
     }
    }
  }
 ?>

And file which show all things:

<?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');
    }
  }
}

My jQuery script:

$(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条回答 默认 最新

  • dongyang2229 2015-08-22 15:52
    关注

    your ajax request is sending the POST to the same page ( window.location.href ) so unless there is a portion of this page designated to handle the POST request then naturally it will return that page's content.

    Perhaps you could try something like:-

    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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图