我是ajax和json的新手。希望在这里有一些帮助!
my.php:
jQuery(".btn").click(function() {
var catid = jQuery(this).attr('id');
jQuery.ajax({
type: "POST",
url: '../ajax.php',
data: "catid=" + catid,
success: function(data)
{
alert("success!");
}
});
});
ajax.php:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__) );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$catid = $_POST['catid'];
$db = JFactory::getDBO();
$user = JFactory::getUser();
if (isset($catid)){
$sql = 'SELECT a,b,c,d';
$sql .= ' FROM `#__table1` 1 INNER JOIN `#__table2` 2 ON 1.id = 2.id';
$sql .= ' WHERE title COLLATE UTF8_GENERAL_CI like "%'.$catid.'%"';
$db->setQuery($sql);
$db->query();
$num_rows = $db->getNumRows();
$result = $db->loadRowList();
}
我需要将$ num_rows和$ result(列a,b,c,d)返回给my.php继续。我怎样才能做到这一点?必须json从数组中返回值吗?
谢谢!