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
Timo Bontenbal
9,582 PointsAJAX vs PHP
Hi,
I am creating an application. I know PHP much more than javascript / jQuery. What is the best way of handling AJAX responses? Does it make sense to output everything whit PHP and is it possible? Because ```success:...´´´ I´m not quite sure how it works and how to output all the data.
success: function(response){
console.log(response);
}
7 Answers
Matthew Eaton
10,669 PointsI've been doing this a lot lately. The " success: " part of the jquery ajax syntax should contain an anonymous function with the argument "returnData".
If you specify the url for the ajax call as a php file with some code and some output, for example "echo" commands, the returnData will contain that output. You can then use jquery to do whatever you want to do with that response.
Hope that helps.
Timo Bontenbal
9,582 PointsHi Matthew Eaton,
Thanks for your fast response. But what if i have 4 "echo" statements. and they should all be in different places? How can i go through them whit jQuery and make them variables like i got in php.
Matthew Eaton
10,669 PointsHow about having your php backend echo out the four bits of information inside a bit of javascript containing four variables. Then you can split the four pieces of info up and use them wherever you need to on the page.
Something like
$returnScript = "<script type='text/javascript'>";
$returnScript .= "var variable1 = 'output1'";
$returnScript .= "var variable2 = 'output2'";
$returnScript .= "var variable3 = 'output3'";
$returnScript .= "var variable4 = 'output4'";
$returnScript .= "</script>";
echo $returnScript;
I haven't tested that, but it looks like it's worth a try. I think that might then put the four bits of output in your page for you to use with jquery/javascript wherever you need.
Timo Bontenbal
9,582 PointsSorry i tried to use a shortcut in my Explanations with the 4 "echo" statements but really there can be an infinity count of echoes. The information comes from a form and the form is dynamic the user can add as many inputs as they need.
Matthew Eaton
10,669 PointsPut the form results in an array and echo them out using a foreach loop?
Timo Bontenbal
9,582 PointsAt the moment I am doing that. but how can i say whit jQuery that some elements should be in a "<h1>" and some "<p>". The best way would be be to echo the code whit just "PHP".
I have a form where I ask basic questions Name: Last Name: Company: Position:... but some of the information should be in "<h1>" tags and some information should be "<p>" tags. I AJAX the form content to the same page as the form that the user can see what she or he has typed.
Jeremy Germenis
29,854 PointsYou can try using a delimiter. Have PHP insert delimiters between your content groups and use jQuery to break it up and place it into its corresponding regions.
You could also have PHP insert some regional tags
<tag1> content group 1</tag1> <tag2> content group 2</tag2>
and have jQuery search for the tags and pull out the content between them and place it into its corresponding regions.
If these wont work please more specifics or an example and I can try to come up with a better solution.