Reputation: 183
I've been doing well with all the Jquery so far but I've now run across a problem when I try to pull data from another php file... I'm just testing it out with one piece of data before I write in the rest of the data and I'm glad because its not working
This is my jquery that gets the information
$.post('php/getinfo.php', {}, function(data){
//Fill variables
var lvl1v = $('#lvl1vocab').text(data);
//Set div's to variables
$('#1v').text(lvl1v);
}, "html");
This is all that is on my getinfo.php page at the moment
<div id="lvl1vocab">120</div>
what I do get written to the screen instead of "120" is "[object Object]"
Upvotes: 1
Views: 64
Reputation: 1776
Looks to me like all you have to do is this:
$.post('php/getinfo.php', {}, function(data){
$('#1v').html(data);
}, "html");
This is assuming your full value of data is <div id="lvl1vocab">120</div>
and you want to put the div from your return value into your #1v div
This is your output:
<div id="1v">
<div id="lvl1vocab">120</div>
</div>
If you are planning on returning multiple div's in your data and they need to be split up and put into further divs already parsed to the page then you need to look into returning an object using json_encode php side and having a look at datatypes on the jQuery page: http://api.jquery.com/jquery.post/
Upvotes: 0
Reputation: 780818
The line:
var lvl1v = $('#lvl1vocab').text(data);
is looking for the lvl1vocab
ID in the current DOM, not in the data returned from the server. You should do:
var lvl1v = $(data).text();
to get the text of the top-level element in the returned data.
To be able to search for things in the returned data, use:
var $data = $('<div>', { html: data }); // Wrap another DIV around it
var lvl1v = $data.find('#lvl1vocab').text();
You need the extra DIV wrapper because find()
won't match the top-level element it's given, just its descendants.
Upvotes: 1