简体   繁体   中英

Load array from PHP via jQuery.ajax

OK, so I want to get a row of data from a DB via jQuery.ajax()

Here is the code where I want to load it:

$.ajax({
  url: 'mypage.php?id=345',
  success: function(data) {
    //process array of data
  }
});

Here is the code on the page that jQuery is loading from:

$DBH = new PDO(DB_DSN,DB_USER,DB_PASSWORD);

$STH = $DBH->("SELECT * FROM mytable WHERE id = :id");    

$STH->setFetchMode(PDO::FETCH_ASSOC);

$data = array( 'id' => $_GET['id']);  
$returnArray = $STH->fetch($data);

// How do I return $returnArray as an Array to jquery?

Not sure what to do to return the array. I guess I can implode() it as a string and then split() once returned but I thought there might be a better way.

Also, I am new to using PDO so if I did it wrong please let me know. Thanks.

使用json_encode() - 您可以在Javascript本地读取它。

$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$testArray=array('a'=>array('b','c'));
print Zend_Json::encode($testArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM