Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a question about using data pulled from the database in javascript.

In my controller I have this:

$this->view->projects = $projectsarray;

I send the array to my view. There I will loop through my array and show the titles. Now I need to get that array in javascript because I want to create a highchart with the data...

Does anyone knows how I could do this easily?

EDIT: I now have this in my controller: (overviewcontroller)

public function getprojectdataAction($id){

}

In my javascript file:

var id = 1;
$.post('/overview/getprojectdata/',{id:id},function(data){
    alert("test");
});

But I get the error that the resource can't be found:

POST http://www.namesite.com/overview/getprojectdata/ 404 (Not Found)

What am I doing wrong?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I assume $this->view->projects is array or object

in your view file

<script>

var projects = <?php echo json_encode($this->projects);?>;
console.log(projects);
</script>

then watch your firebug ...

share|improve this answer

you can do this by converting array to JSON you can send a ajax request to same controller

and return echo json_encode($array); then you can use directly with responce by decoding it...

$.post('controller/action',{id:id},function(data){
    var arrayJson=JSON.parse(data);
});

you can use arrayJson as data array...

share|improve this answer
    
Got an error now, updated begin post. –  nielsv Aug 12 '13 at 9:20

you can use json_encode in the php part of the code to generate a javascript object out of your array. For example:

$php = array("myAttrib" => "hallo");
echo json_encode($php);

and in javascript you can use

alert (output.myAttrib);

, where output refers to echo json_encode(php). This code would open a box showing "hallo". Your question seems to be quite similar to how to use json_encode. For creating of a highchart also this post Highcharts data series issue with ajax/json and PHP could be of interest for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.