Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  36] [ 3]  / answers: 1 / hits: 18452  / 13 Years ago, tue, october 11, 2011, 12:00:00

I want javascript to be able to call a php script (which just echos a string) using jQuery.



I think $.get is the right way, but not too sure.



I then want to use the returned string as a javascript variable.


More From » php

 Answers
37

$.get() is the way to go, indeed.



First of all, you will need a page / url that outputs the result of the function you want to use (for example, *www.yoursite.com/test_output.php* ). You should create that page and call the function you want to use there.
Also keep in mind that I said output, not return, because .get will fetch the output of the http response, not the returned value of the php function.



So if you have the following function defined in your site (you can also define it in test_output.php, of course):



<?php 
function say_hello() {
return 'hello world';
}
?>


In test_output.php, you will need something like this:



<?php
echo say_hello();
?>


Then on the client side, you need some JavaScript / jQuery:



var data_from_ajax;

$.get('http://www.yoursite.com/test_output.php', function(data) {
data_from_ajax = data;
});


Now you have the output of the ajax .get() function stored in data_from_ajax and you can use it as you please.


[#89679] Monday, October 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
raveno questions
;