How to execute/call a PHP function via ajax? [duplicate] - php

This question already has answers here:
How to call a specific function in a PHP script via Ajax?
(5 answers)
Closed 9 years ago.
I have a PHP file which consists of a number of functions, and when I tried to call one of them via ajax, it executes all the function on that page.
How can I execute only one function at a time via ajax? I am using simple PHP without class-object pattern.

Maybe you can pass a parameter in the URL telling the PHP which function you want to execute e.g.
$.ajax({
url: "http://www.url.com/file.php?functiontocall=functiona",
success: function(){
// Do something
}
});
and on the PHP side
if ($_GET["functiontocall"] == "functiona") {
// Call functiona and output response
}

you need 2 PHP for this. One with all the functions fileA.php, and the other which you will call through ajax fileB.php. fileB.php should include fileA.php and you can call whichever function you want.

Related

How to use a javascript parameter inside php? [duplicate]

This question already has answers here:
how to write php function inside javascript function
(11 answers)
Closed 9 years ago.
There is a javascript function with a parameter :
<script type="text/javascript">
function afficheHelp(aide_htm)
{
... // I want to code PHP here with the parameter "aide_htm"
}
</script>
How to use the "aide_htm" javascript parameter inside PHP code within the javascript function ?
You need to invoke serverside code from your clientside Javascript. This is commonly done with an Ajax call. How to exactly do this depends enormously on what you want to do with the result and which JS library you are using. I'd recommend reading up on Ajax in general, and jQuery or Mootools on how to execute a call specifically.
Javascript is client side and executed in the users browser whereas PHP is server side and executed on your server as such you can't use javascript directly in PHP. You can however use javascript to perform an AJAX request to PHP on your server. If you're using jQuery have a look at http://api.jquery.com/jQuery.ajax/

Calling a php function via ajax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating jQuery AJAX requests to a PHP function
I am trying to run a really simple formula,
Call a web page, and every few seconds or so Ajax calls a php function to echo "Hello World"
<?php
function test(){
echo "Hello World";
}
?>
<script type="text/javascript">
function testingTimer()
{
//CALL PHP FUNCTION CALLED TEST
}
setInterval('testingTimer()',5000);
</script>
All I need is the code that calls the already declared php function.
You cannot call that directly. Write code of AJAX and call URL like myfile.php?action=test and in file myfile.php write if action GET variable is equal to test then call that function and don't forget to exit code to prevent any other output.
Google on how to do ajax calls. It's basic knowledge explained in too many tutorials, and varies depending on if you are using a framework or not, and which.
You can post something for example :
function testingTimer()
{
$.post(URL,
{
foo : 'foo'
}
);
}
and in php check post:
if (isset($_POST['foo']))
test();
AJAX does not have the ability to call an arbitrary php function on its own. AJAX allows you to connect to a given page and request data. That being said, you can use AJAX to load a given page and all that page does is call your test function:
Based on your example code I assume you're trying to do this all within a single file. The issue you have is that PHP is run server side before/as the page loads. Once loaded it cannot be re-executed without a refresh.
You need to have your PHP in a separate file so that it can be called over HTTP by AJAX and return a response.
So something like this (if using jQuery):
$.ajax('helloworld.php', function(data){
alert(data);
}, 'html');
Should popup with hello world if implemented correctly.
You should use jQuery to use the ajax function.
You should not use a setInterval, but a setTimeout after the response of server because if the server take more than 5000 seconds to respond, then you'll have two requests at the same time.
If you want to put a timeout, you can always see the abort function on the jqXHR Object.
For example, you could do:
function refresh() {
$.ajax({
type: 'GET', // can be POST or GET
url: 'page.php' // php script to call
// when the server responds
}).done(function(response) {
console.log(response);
// call your function automatically
setTimeout(refresh, 5000);
});
}

How to call a php function from javascript by specify its name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call PHP Function using jQuery AJAX
Call php function from javascript
I need
callPhpFnc("myPhp"); // javascript function
how can i call the php function with myPhp as function name.
in my php file
<?php
function myPhp()
{
echo 'Hello';
}
?>
Its not possible my dear. Coz your php is runned once at server side before the page is loaded. But the javascript at client side. Option is to use the ajax.
You cant call a PHP function by JS.You have to use ajax for this.

calling a php function from Javascript same file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling PHP Function within Javascript
I have a small JavaScript code in a php file. This JavaScript code runs some logic and in the end should call a php function which is defined in the same php file. How can I do this ? any guidance ?
you can use ajax for that and call this same file. because php function will not initialize from javascript.
I think using PHPLIVEX, can be useful too in this scenario.
PHPLIVEX has a little tag line -
Call your php functions and class methods from javascript in one line
This can be useful if you need to call php functions from javascript several times in your application.
if an server-side code need to be executed , you need to call that via ajax.
this is for sure not the best answer there is, but it should work: have your javascript make an ajax call, to your same page, using a POST parameter, so u know this is the call for that function .. hope it is clear enough in large lines ..
You cannot. JavaScript is executed in a browser, PHP on server. The only way to do it is using AJAX request, e.g. with jQuery:
$.get('myurl.php', function(data){ ... });
http://api.jquery.com/jQuery.get/
That will return output produced by myphp.php file; if you want to run a single function from it, you'll need some logic that determines which function it should execute, e.g. URL parameter. However, I'd strongly advise against using url parameter value as direct function name.
you should use jquery's ajax or post

append PHP in jquery [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating jQuery AJAX requests to a PHP function
How can i write PHP in Jquery
is there is a way to do this
$('button').click(function(){
<?php session_destroy();?>
}) ;
You can't execute PHP client-side.
But
You can call a php script on click to do what you want:
$('button').click(function(){
$.get("destroySession.php");
});
You can also get or post some values:
var values = {
destroy: true
};
//get request:
$.get("destroySession.php", values);
//post request:
$.post("destroySession.php", values);
You can't execute PHP client-side.
You cannot. PHP is interpreted on the server side and jQuery is interpreted on the client side.
You should either use an anchor <a href="session_destroy.php"> to go to another PHP page and destroy the session or use AJAX to call the destroy function without leaving the page.
jQuery('button').click( function () { jQuery.get("session_destroy.php"); } );

Categories