我想通過Ajax發送PHP類內的POST值,以更改我的SQL查詢。在PHP類中發送Ajax post值
PHP頁面
//Link page with Classes
require_once('mPortfolio.php');
$mPortfolio = new Portfolio();
$all = $mPortfolio->getAll();
//Echo sql results here
//AJAX to send filter value to PHP with Classes
$('#filters a').on('click', function(e){
var filter = $(this).data('filter');
$.ajax({
type: 'POST',
url: 'models/mPortfolio.php',
data: {filter: filter},
success: function(data){
alert(data);
}
});
PHP類mPortfolio.php
echo $_POST['filter']; //This works !
require_once $_SERVER["DOCUMENT_ROOT"].'/models/db/db.php';
class Portfolio{
private $_db;
public function Portfolio()
{
$this->_db = new db();
$this->_db->SQLRequest("SET NAMES utf8");
}
public function getAll()
{
if(isset($_POST['filter'])) :
$query = "SELECT * FROM portfolio WHERE category='".$_POST['filter']."' AND online = 1 ORDER BY category,pos ASC";
else :
$query = "SELECT * FROM portfolio WHERE online = 1 ORDER BY category,pos ASC";
endif;
return $this->_db->SQLRequest($query);
}
}
的mPortfolio.php頁面實際上可以在類的外部讀取$ _ POST [ '過濾器'],但公共函數getAll()不能。
你能幫我,請讓它工作。 謝謝
您需要instanticate對象並調用該方法。本身的類定義什麼都不做。 – Fracsi
您不是在代碼中創建該類的實例,那麼您如何期望該方法被執行......? – CBroe
** SQL注入**。在將它放入查詢之前,請先轉義'$ _POST ['filter']' –