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/
Related
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.
This question already has answers here:
How can I pass variables from JavaScript to PHP?
(2 answers)
Closed 9 years ago.
I want to pass a variable from a function in JavaScript to PHP that is also inside the JavaScript function.
For Example:
myfunction('qwerty');
<script>
myfunctionjs(x)
{
<?php echo $x?>
}
</script>
note: code above is just a sample
Your code sample, while it expresses what you want quite clearly, should also show how impossible it is.
PHP is a server-side language whereas javascript is a client-side language. The only way for javascript to "talk" to PHP is to submit a request to the server. You can do this either through a standard HTTP-GET request or a POST request (through a form submission, or more commonly via Ajax).
The PHP isn't inside the JavaScript function. The PHP code is executed on the server.
When the Javascript is executed, the code looks like this:
myfunction('qwerty');
<script>
myfunctionjs(x)
{
alert(x); //or whatever $x is
}
</script>
If you want to pass 'x' to a PHP script, you'll have to make an AJAX request.
You can't have javascript telling PHP to do something inline. JavaScript runs client side, while PHP runs server side.
Instead, you need to have your javascript send the data to your server, and you'll have PHP do something when receiving that second request.
If you are using jQuery, have a look at the ajax method for example on how to send data: http://api.jquery.com/jQuery.ajax/
Passing variable from javascript to php right away is not possible. You will have to use AJAX for this. You have to understand the difference between server-side scripting and client-side scripting.
https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
Read the above link and it will give you a full understanding of the process. if you have any issues let me know.
Read more
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.
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
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"); } );