How to get php function ouptut on client side?What is the difference between client-side and server-side programming?How can I prevent SQL injection in PHP?startsWith() and endsWith() functions in PHPHow do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How to get the client IP address in PHPHow Do You Parse and Process HTML/XML in PHP?Get the full URL in PHPHow does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?Executing Server and Client functions from client side with php and js

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

What is the meaning of "of trouble" in the following sentence?

How to make payment on the internet without leaving a money trail?

What do you call a Matrix-like slowdown and camera movement effect?

Are white and non-white police officers equally likely to kill black suspects?

Extreme, but not acceptable situation and I can't start the work tomorrow morning

What defenses are there against being summoned by the Gate spell?

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

Why is this code 6.5x slower with optimizations enabled?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Can I make popcorn with any corn?

Example of a relative pronoun

What is the command to reset a PC without deleting any files

N.B. ligature in Latex

Is it possible to make sharp wind that can cut stuff from afar?

How do you conduct xenoanthropology after first contact?

Prevent a directory in /tmp from being deleted

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Can Medicine checks be used, with decent rolls, to completely mitigate the risk of death from ongoing damage?

Why is an old chain unsafe?

Why is "Reports" in sentence down without "The"

Is Social Media Science Fiction?

How to type dʒ symbol (IPA) on Mac?

What typically incentivizes a professor to change jobs to a lower ranking university?



How to get php function ouptut on client side?


What is the difference between client-side and server-side programming?How can I prevent SQL injection in PHP?startsWith() and endsWith() functions in PHPHow do I get PHP errors to display?How do I get a YouTube video thumbnail from the YouTube API?How to get the client IP address in PHPHow Do You Parse and Process HTML/XML in PHP?Get the full URL in PHPHow does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?Executing Server and Client functions from client side with php and js






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as name & in return I want output as Hello name.
I tried, but it's not showing,



Server-side

The PHP file name is "name.php" having function greet() with parameter $name is as follows:



<?php
function greet($name)

echo "hello $name";

?>


Client-side

The HTML file consists of a button "Click me" which should send the name John to PHP page, and the greet() function should execute and output should display at client side as "Hello John" is as follows:



<html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()

$("#button").click(function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(greet(data)

alert( "Data Saved: " + data);
);
);
);
</script>

<input type="button" id="button" value="Click me">

</html>


I have used Ajax method for calling PHP function if any other POST method can give output, then please let me know.

Can someone please help to how to get output from PHP function to client-side on button click.










share|improve this question



















  • 1





    done(greet(data) - You can't call PHP functions directly from JS. When you make the ajax call to name.php. That file (the PHP file) needs to call that function and return the value to your ajax callback. Your ajax response will simply be the outputted data from the PHP file, nothing else.

    – Magnus Eriksson
    Mar 8 at 6:16












  • First you need to call your ajax on button click move your ajax code to $("#button").click(function() //yourajaxcode ); block

    – Saad Suri
    Mar 8 at 6:16






  • 1





    If all you want to do is display "Hello John" (or "Hello + whatever $name equals") ... do you need to do ANYTHING client side? Why can't you just <?php echo greet($name) ?> in-line (changing "greet()" so it just concatenates the string, instead of calling "echo")????

    – paulsm4
    Mar 8 at 6:48


















1















I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as name & in return I want output as Hello name.
I tried, but it's not showing,



Server-side

The PHP file name is "name.php" having function greet() with parameter $name is as follows:



<?php
function greet($name)

echo "hello $name";

?>


Client-side

The HTML file consists of a button "Click me" which should send the name John to PHP page, and the greet() function should execute and output should display at client side as "Hello John" is as follows:



<html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()

$("#button").click(function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(greet(data)

alert( "Data Saved: " + data);
);
);
);
</script>

<input type="button" id="button" value="Click me">

</html>


I have used Ajax method for calling PHP function if any other POST method can give output, then please let me know.

Can someone please help to how to get output from PHP function to client-side on button click.










share|improve this question



















  • 1





    done(greet(data) - You can't call PHP functions directly from JS. When you make the ajax call to name.php. That file (the PHP file) needs to call that function and return the value to your ajax callback. Your ajax response will simply be the outputted data from the PHP file, nothing else.

    – Magnus Eriksson
    Mar 8 at 6:16












  • First you need to call your ajax on button click move your ajax code to $("#button").click(function() //yourajaxcode ); block

    – Saad Suri
    Mar 8 at 6:16






  • 1





    If all you want to do is display "Hello John" (or "Hello + whatever $name equals") ... do you need to do ANYTHING client side? Why can't you just <?php echo greet($name) ?> in-line (changing "greet()" so it just concatenates the string, instead of calling "echo")????

    – paulsm4
    Mar 8 at 6:48














1












1








1








I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as name & in return I want output as Hello name.
I tried, but it's not showing,



Server-side

The PHP file name is "name.php" having function greet() with parameter $name is as follows:



<?php
function greet($name)

echo "hello $name";

?>


Client-side

The HTML file consists of a button "Click me" which should send the name John to PHP page, and the greet() function should execute and output should display at client side as "Hello John" is as follows:



<html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()

$("#button").click(function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(greet(data)

alert( "Data Saved: " + data);
);
);
);
</script>

<input type="button" id="button" value="Click me">

</html>


I have used Ajax method for calling PHP function if any other POST method can give output, then please let me know.

Can someone please help to how to get output from PHP function to client-side on button click.










share|improve this question
















I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as name & in return I want output as Hello name.
I tried, but it's not showing,



Server-side

The PHP file name is "name.php" having function greet() with parameter $name is as follows:



<?php
function greet($name)

echo "hello $name";

?>


Client-side

The HTML file consists of a button "Click me" which should send the name John to PHP page, and the greet() function should execute and output should display at client side as "Hello John" is as follows:



<html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()

$("#button").click(function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(greet(data)

alert( "Data Saved: " + data);
);
);
);
</script>

<input type="button" id="button" value="Click me">

</html>


I have used Ajax method for calling PHP function if any other POST method can give output, then please let me know.

Can someone please help to how to get output from PHP function to client-side on button click.







php html ajax html5 client-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 6:36









Qirel

11.6k62542




11.6k62542










asked Mar 8 at 6:10









DavidDavid

679




679







  • 1





    done(greet(data) - You can't call PHP functions directly from JS. When you make the ajax call to name.php. That file (the PHP file) needs to call that function and return the value to your ajax callback. Your ajax response will simply be the outputted data from the PHP file, nothing else.

    – Magnus Eriksson
    Mar 8 at 6:16












  • First you need to call your ajax on button click move your ajax code to $("#button").click(function() //yourajaxcode ); block

    – Saad Suri
    Mar 8 at 6:16






  • 1





    If all you want to do is display "Hello John" (or "Hello + whatever $name equals") ... do you need to do ANYTHING client side? Why can't you just <?php echo greet($name) ?> in-line (changing "greet()" so it just concatenates the string, instead of calling "echo")????

    – paulsm4
    Mar 8 at 6:48













  • 1





    done(greet(data) - You can't call PHP functions directly from JS. When you make the ajax call to name.php. That file (the PHP file) needs to call that function and return the value to your ajax callback. Your ajax response will simply be the outputted data from the PHP file, nothing else.

    – Magnus Eriksson
    Mar 8 at 6:16












  • First you need to call your ajax on button click move your ajax code to $("#button").click(function() //yourajaxcode ); block

    – Saad Suri
    Mar 8 at 6:16






  • 1





    If all you want to do is display "Hello John" (or "Hello + whatever $name equals") ... do you need to do ANYTHING client side? Why can't you just <?php echo greet($name) ?> in-line (changing "greet()" so it just concatenates the string, instead of calling "echo")????

    – paulsm4
    Mar 8 at 6:48








1




1





done(greet(data) - You can't call PHP functions directly from JS. When you make the ajax call to name.php. That file (the PHP file) needs to call that function and return the value to your ajax callback. Your ajax response will simply be the outputted data from the PHP file, nothing else.

– Magnus Eriksson
Mar 8 at 6:16






done(greet(data) - You can't call PHP functions directly from JS. When you make the ajax call to name.php. That file (the PHP file) needs to call that function and return the value to your ajax callback. Your ajax response will simply be the outputted data from the PHP file, nothing else.

– Magnus Eriksson
Mar 8 at 6:16














First you need to call your ajax on button click move your ajax code to $("#button").click(function() //yourajaxcode ); block

– Saad Suri
Mar 8 at 6:16





First you need to call your ajax on button click move your ajax code to $("#button").click(function() //yourajaxcode ); block

– Saad Suri
Mar 8 at 6:16




1




1





If all you want to do is display "Hello John" (or "Hello + whatever $name equals") ... do you need to do ANYTHING client side? Why can't you just <?php echo greet($name) ?> in-line (changing "greet()" so it just concatenates the string, instead of calling "echo")????

– paulsm4
Mar 8 at 6:48






If all you want to do is display "Hello John" (or "Hello + whatever $name equals") ... do you need to do ANYTHING client side? Why can't you just <?php echo greet($name) ?> in-line (changing "greet()" so it just concatenates the string, instead of calling "echo")????

– paulsm4
Mar 8 at 6:48













2 Answers
2






active

oldest

votes


















1














You cannot call the PHP function from JavaScript, even from Ajax. What Ajax does, is ask for data that is outputted from a PHP file. So you will need to call the function in your name.php, which gives the output - which you can then print in PHP.



Ajax will only fetch the string that was printed from PHP.



Also note that you do not need to close PHP by doing ?> at the end of the file, unless there's some HTML or the likes coming after.



Server-side, you'll do something like this



<?php
// Define the function
function greet($name)
return "Hello $name";


// Call the function with the value sent from Ajax - this output is what Ajax fetches
echo greet($_POST['name']);


Client side you would do something like



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#button").on("click", function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(data)
alert("Data Saved: " + data);
);
);
</script>

<input type="button" id="button" value="Click me">


Then data will contain all the string that was printed from your PHP file. If you expect an array, you need to convert it into a JSON.



  • What is the difference between client-side and server-side programming?





share|improve this answer























  • Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

    – David
    Mar 8 at 6:53











  • Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

    – Qirel
    Mar 8 at 7:09











  • Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

    – David
    Mar 8 at 8:38












  • Just do url: "/name.php", no need to provide your (local) IP.

    – Qirel
    Mar 8 at 10:01











  • Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

    – David
    Mar 9 at 7:54



















0














First you need to bind your ajax call on your button click, so when the button click it will trigger the ajax call.



$(document).ready(function()

// when button click it will trigger ajax call
$("#button").click(function()
$.ajax(
type: "GET",
url: "name.php",
data: name: "John" ,
success: function(data)
// on successfull return it will alert the data
alert("Data saved: " + data);

);
);
);


And in your name.php



 <?php
// get your data you send from ajax
$name = $_GET['name'];
// it will echo the "hello $name" and return it
greet($name);

function greet($name)

echo "hello $name";

?>





share|improve this answer




















  • 3





    A good answer includes a proper explanation about what issue it solves and how.

    – Magnus Eriksson
    Mar 8 at 6:37












  • Thank you for your correction :) @MagnusEriksson

    – Muhammad Shareyar
    Mar 8 at 6:50











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057664%2fhow-to-get-php-function-ouptut-on-client-side%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You cannot call the PHP function from JavaScript, even from Ajax. What Ajax does, is ask for data that is outputted from a PHP file. So you will need to call the function in your name.php, which gives the output - which you can then print in PHP.



Ajax will only fetch the string that was printed from PHP.



Also note that you do not need to close PHP by doing ?> at the end of the file, unless there's some HTML or the likes coming after.



Server-side, you'll do something like this



<?php
// Define the function
function greet($name)
return "Hello $name";


// Call the function with the value sent from Ajax - this output is what Ajax fetches
echo greet($_POST['name']);


Client side you would do something like



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#button").on("click", function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(data)
alert("Data Saved: " + data);
);
);
</script>

<input type="button" id="button" value="Click me">


Then data will contain all the string that was printed from your PHP file. If you expect an array, you need to convert it into a JSON.



  • What is the difference between client-side and server-side programming?





share|improve this answer























  • Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

    – David
    Mar 8 at 6:53











  • Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

    – Qirel
    Mar 8 at 7:09











  • Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

    – David
    Mar 8 at 8:38












  • Just do url: "/name.php", no need to provide your (local) IP.

    – Qirel
    Mar 8 at 10:01











  • Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

    – David
    Mar 9 at 7:54
















1














You cannot call the PHP function from JavaScript, even from Ajax. What Ajax does, is ask for data that is outputted from a PHP file. So you will need to call the function in your name.php, which gives the output - which you can then print in PHP.



Ajax will only fetch the string that was printed from PHP.



Also note that you do not need to close PHP by doing ?> at the end of the file, unless there's some HTML or the likes coming after.



Server-side, you'll do something like this



<?php
// Define the function
function greet($name)
return "Hello $name";


// Call the function with the value sent from Ajax - this output is what Ajax fetches
echo greet($_POST['name']);


Client side you would do something like



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#button").on("click", function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(data)
alert("Data Saved: " + data);
);
);
</script>

<input type="button" id="button" value="Click me">


Then data will contain all the string that was printed from your PHP file. If you expect an array, you need to convert it into a JSON.



  • What is the difference between client-side and server-side programming?





share|improve this answer























  • Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

    – David
    Mar 8 at 6:53











  • Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

    – Qirel
    Mar 8 at 7:09











  • Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

    – David
    Mar 8 at 8:38












  • Just do url: "/name.php", no need to provide your (local) IP.

    – Qirel
    Mar 8 at 10:01











  • Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

    – David
    Mar 9 at 7:54














1












1








1







You cannot call the PHP function from JavaScript, even from Ajax. What Ajax does, is ask for data that is outputted from a PHP file. So you will need to call the function in your name.php, which gives the output - which you can then print in PHP.



Ajax will only fetch the string that was printed from PHP.



Also note that you do not need to close PHP by doing ?> at the end of the file, unless there's some HTML or the likes coming after.



Server-side, you'll do something like this



<?php
// Define the function
function greet($name)
return "Hello $name";


// Call the function with the value sent from Ajax - this output is what Ajax fetches
echo greet($_POST['name']);


Client side you would do something like



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#button").on("click", function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(data)
alert("Data Saved: " + data);
);
);
</script>

<input type="button" id="button" value="Click me">


Then data will contain all the string that was printed from your PHP file. If you expect an array, you need to convert it into a JSON.



  • What is the difference between client-side and server-side programming?





share|improve this answer













You cannot call the PHP function from JavaScript, even from Ajax. What Ajax does, is ask for data that is outputted from a PHP file. So you will need to call the function in your name.php, which gives the output - which you can then print in PHP.



Ajax will only fetch the string that was printed from PHP.



Also note that you do not need to close PHP by doing ?> at the end of the file, unless there's some HTML or the likes coming after.



Server-side, you'll do something like this



<?php
// Define the function
function greet($name)
return "Hello $name";


// Call the function with the value sent from Ajax - this output is what Ajax fetches
echo greet($_POST['name']);


Client side you would do something like



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#button").on("click", function()
$.ajax(
type: "POST",
url: "name.php",
data: name: "John"
).done(data)
alert("Data Saved: " + data);
);
);
</script>

<input type="button" id="button" value="Click me">


Then data will contain all the string that was printed from your PHP file. If you expect an array, you need to convert it into a JSON.



  • What is the difference between client-side and server-side programming?






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 6:32









QirelQirel

11.6k62542




11.6k62542












  • Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

    – David
    Mar 8 at 6:53











  • Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

    – Qirel
    Mar 8 at 7:09











  • Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

    – David
    Mar 8 at 8:38












  • Just do url: "/name.php", no need to provide your (local) IP.

    – Qirel
    Mar 8 at 10:01











  • Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

    – David
    Mar 9 at 7:54


















  • Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

    – David
    Mar 8 at 6:53











  • Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

    – Qirel
    Mar 8 at 7:09











  • Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

    – David
    Mar 8 at 8:38












  • Just do url: "/name.php", no need to provide your (local) IP.

    – Qirel
    Mar 8 at 10:01











  • Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

    – David
    Mar 9 at 7:54

















Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

– David
Mar 8 at 6:53





Thanks Qirel, I used the same code, which you mentioned. I created PHP script on server side and on client-side the ajax code in html file. But it is not executing.

– David
Mar 8 at 6:53













Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

– Qirel
Mar 8 at 7:09





Check your console in your browser for client-side errors, and PHP error logs on your server for clues.

– Qirel
Mar 8 at 7:09













Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

– David
Mar 8 at 8:38






Hey Qirel, as you said I checked on console and found this error, "Access to XMLHttpRequest at 'file:///C:/Users/niranjan.rao/Documents/Ampps/www/name.php?name=John' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." On the Client side, on the click event of button, in the ajax apart, I'm passing the php file path as url : "192.168.0.101:80/name.php" in the url.

– David
Mar 8 at 8:38














Just do url: "/name.php", no need to provide your (local) IP.

– Qirel
Mar 8 at 10:01





Just do url: "/name.php", no need to provide your (local) IP.

– Qirel
Mar 8 at 10:01













Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

– David
Mar 9 at 7:54






Thanks Qirel, but is it possible that you copy the client side script (mentioned in your answer) & save it in " .html ", then on button clicked by you, the ajax code will try to hit php page which is on my machine, will execute the function and output will display on your machine in your HTML page.

– David
Mar 9 at 7:54














0














First you need to bind your ajax call on your button click, so when the button click it will trigger the ajax call.



$(document).ready(function()

// when button click it will trigger ajax call
$("#button").click(function()
$.ajax(
type: "GET",
url: "name.php",
data: name: "John" ,
success: function(data)
// on successfull return it will alert the data
alert("Data saved: " + data);

);
);
);


And in your name.php



 <?php
// get your data you send from ajax
$name = $_GET['name'];
// it will echo the "hello $name" and return it
greet($name);

function greet($name)

echo "hello $name";

?>





share|improve this answer




















  • 3





    A good answer includes a proper explanation about what issue it solves and how.

    – Magnus Eriksson
    Mar 8 at 6:37












  • Thank you for your correction :) @MagnusEriksson

    – Muhammad Shareyar
    Mar 8 at 6:50















0














First you need to bind your ajax call on your button click, so when the button click it will trigger the ajax call.



$(document).ready(function()

// when button click it will trigger ajax call
$("#button").click(function()
$.ajax(
type: "GET",
url: "name.php",
data: name: "John" ,
success: function(data)
// on successfull return it will alert the data
alert("Data saved: " + data);

);
);
);


And in your name.php



 <?php
// get your data you send from ajax
$name = $_GET['name'];
// it will echo the "hello $name" and return it
greet($name);

function greet($name)

echo "hello $name";

?>





share|improve this answer




















  • 3





    A good answer includes a proper explanation about what issue it solves and how.

    – Magnus Eriksson
    Mar 8 at 6:37












  • Thank you for your correction :) @MagnusEriksson

    – Muhammad Shareyar
    Mar 8 at 6:50













0












0








0







First you need to bind your ajax call on your button click, so when the button click it will trigger the ajax call.



$(document).ready(function()

// when button click it will trigger ajax call
$("#button").click(function()
$.ajax(
type: "GET",
url: "name.php",
data: name: "John" ,
success: function(data)
// on successfull return it will alert the data
alert("Data saved: " + data);

);
);
);


And in your name.php



 <?php
// get your data you send from ajax
$name = $_GET['name'];
// it will echo the "hello $name" and return it
greet($name);

function greet($name)

echo "hello $name";

?>





share|improve this answer















First you need to bind your ajax call on your button click, so when the button click it will trigger the ajax call.



$(document).ready(function()

// when button click it will trigger ajax call
$("#button").click(function()
$.ajax(
type: "GET",
url: "name.php",
data: name: "John" ,
success: function(data)
// on successfull return it will alert the data
alert("Data saved: " + data);

);
);
);


And in your name.php



 <?php
// get your data you send from ajax
$name = $_GET['name'];
// it will echo the "hello $name" and return it
greet($name);

function greet($name)

echo "hello $name";

?>






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 6:42

























answered Mar 8 at 6:37









Muhammad ShareyarMuhammad Shareyar

1117




1117







  • 3





    A good answer includes a proper explanation about what issue it solves and how.

    – Magnus Eriksson
    Mar 8 at 6:37












  • Thank you for your correction :) @MagnusEriksson

    – Muhammad Shareyar
    Mar 8 at 6:50












  • 3





    A good answer includes a proper explanation about what issue it solves and how.

    – Magnus Eriksson
    Mar 8 at 6:37












  • Thank you for your correction :) @MagnusEriksson

    – Muhammad Shareyar
    Mar 8 at 6:50







3




3





A good answer includes a proper explanation about what issue it solves and how.

– Magnus Eriksson
Mar 8 at 6:37






A good answer includes a proper explanation about what issue it solves and how.

– Magnus Eriksson
Mar 8 at 6:37














Thank you for your correction :) @MagnusEriksson

– Muhammad Shareyar
Mar 8 at 6:50





Thank you for your correction :) @MagnusEriksson

– Muhammad Shareyar
Mar 8 at 6:50

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057664%2fhow-to-get-php-function-ouptut-on-client-side%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved