Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Ryan Smee
3,392 Pointspost Var to php with $.getJSON
I have a simple webapp where I can call data from database using php, It then gets turned into JSON before being called with a $.getJSON to the be formatted on the page in the front end. In the getJSON request I have each() loop that runs through the data formatting it on the page.
I now want to add a search facility which will add the search criteria to a variable, post that to the php file to be used as part of the database request. I have managed to bind a php variable to my stored proc execution in my php file, and am now outputting json (So hard part done)
Now i'm having trouble passing a javascript variable to the php file, with my '$.getJSON' request so that i can dynamically set the php variable before I bind it.
I hope this makes sense..
2 Answers

Joe Bruno
35,909 PointsRyan,
I think I understand some of what you are trying to accomplish; however, without posting your code, it will be hard to be helpful. Anytime you are trying to pass a JS variable to PHP, perhaps the easiest way is to utilize the jQuery ajax method (http://api.jquery.com/jquery.ajax/). Pass the name of your PHP function into the data (action:) property of the method like so.
$.ajax({
type: "POST",
url: ajaxurl,
data: { action: "get_active_cart_items"},
success: function(data) {
//callback
}
});
If you provide your code (or a link to your code), better answers could be provided. Also, if your PHP is running in WordPress' framework, there are a few extra steps to making ajax calls work with WordPress (http://codex.wordpress.org/AJAX_in_Plugins).
Good Luck!

Ryan Smee
3,392 PointsIts not a World Press frame work.
I figured Ajaxing would be the best solution but i was trying to be a little cheeky and not make much changes to the code i already had in place.
getJSON request is as below. (I have simplified the formatting as that isn't the important part).
$(document).ready(function(){
// call the php that has the php array which is json_encoded
$.getJSON('SP.php', function(data) {
// data will hold the php array as a javascript object
$.each(data, function(key, val) {
key = key + 1;
// share pannel stays the same for each post variant
if (j_sourceId = 1) {
var feedPost = '<div class="postOut" id="post">' + receivedJSONdata + '</div>';
}
else {
// another feedPost Var
}
//create feed post
$('body').append(feedPost);
});
});
});
});
I now want to push data with this request in this manner:
Keyword entered -> Keyword sent to php -> php binds keyword to DB request -> DB returns data -> Php Turns to JSON -> getJSON pulls through info and formats
Currently i have this entire process working with the exception of the passing the Key Word from the front end. All the PHP is working.
Ryan Smee
3,392 PointsRyan Smee
3,392 PointsCheers, I went away and realised my problem was actually a lot more simple that I thought. Here's what i did in the end:
JQuery bit:
PHP Bit:
I hope this helps anyone