user1221098
user1221098

Reputation: 85

to get the values from php to jquery using JSON

I am very new to these things .. It might be the easiet question but sitll.....I have to get the date from my mysql database using php and from there to jquery file using JSON to make it work with my jquery plugin. I have put the values in array and used `

    $tDate['year']= 2012
    $tDate['month']=03
    $tDate['day']=06


echo json_encode($tDate);

` and in my jquery I need to read all these data how do I do it .. have been looking into quite a few websites but have no idea

Upvotes: 1

Views: 196

Answers (4)

Starx
Starx

Reputation: 78961

Use $.getjSON(), if the requirement is only to get the json data.

$.getJSON('your.php', function(data) {

  $.each(data, function(key, val) {
    alert(val);
  });

});

Upvotes: 3

Ravi Gadag
Ravi Gadag

Reputation: 15851

try the getJson method of Jquery $.getJson()

syntaxk would be like this

jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] )

Eg:

$.getJSON('Url of PHP', function(response) {

  $.each(resopnse, function(key, val) {
//do some thing with Val
  });

});

Upvotes: 0

mVChr
mVChr

Reputation: 50177

$.get('/path/to/your_php_file.php', function(data){
    parseInt(data.year, 10);  // => 2012
    parseInt(data.month, 10); // => 3
    parseInt(data.day, 10);   // => 6 
});

Upvotes: 1

Andreas Wong
Andreas Wong

Reputation: 60506

In your javascript:

$.get('/uri/to/script.php', function(data) {
   console.log(data);
}, 'json');

Upvotes: 1

Related Questions