1

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.

2 Answers 2

4

Use json_encode() - you can read that in Javascript natively.

Sign up to request clarification or add additional context in comments.

4 Comments

will also need to specify dataType: 'json', in the $.ajax() call or send back a JSON mime-type, in order that jquery will read the response correctly
@Tom I used $.getJSON instead of $.ajax
@John Isaacks: ah ok. it was just the example used $.ajax().
@Tom right, I originally planned to use $.ajax but did a google search on jquery json after reading this answer and found the getJSON method. But thanks for the comment, it can save someone a headache!
0
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$testArray=array('a'=>array('b','c'));
print Zend_Json::encode($testArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.