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
Related
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/
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
i started to learn jquery yesterday so i'm not so good.
I've a question:
as title there's a way to pass a php function instead of a file to jquery $.post method?
Here's an example of what i would to do:
function send(str)
{
$.post
(
"<?php PHPfunction(); ?>",
{send:str},
function(data)
{
$("#output-text").html(data.reply);
},
"json"
);
}
there's a way to do that? or i need always to pass a file to the first parameter?
Just another question:
how can i pass more than 1 row from php to jquery in the ajax reply?
You'll want to use AJAX to make a request for that function. For more information, see http://api.jquery.com/jQuery.ajax/. Google can also provide you a ton of examples because what you're asking is a fairly common problem with plenty of good solutions.
PHP is run on the server before the page is fully loaded, so you can't call a PHP function on the fly like that. Javascript runs within the browser afterwards, on the clients machine. They are two separate environments, code wise.
You're better off calling a page that uses the function, using Ajax or something.
For instance I have a php function
<?php
function SayHi($msg){
return $msg;
}
?>
The following part triggers the onclick event
Click
How can I merge the two such that when I click on Click, the php function is invoked..?
You can not. PHP is executed serverside, while javascript is executed clientside.
THe generic solution for this is to use asynchronous Javascript (ajax) to call on your webserver, which in turn can invoke PHP code to do various things.
jQuery is a very nice library to handle this, and much more.
There are many ways to do this, most are complicated.
One easier way is:
<?php
function myFunc() { die('yay'); }
if (isset($_REQUEST['myButton'])) myFunc();
?>
<form><button name="myButton">Click Me</button></form>
Whaaat? PHP is server-side you cannot invoke a php function from javascript. However you can do this with ajax.
AJAX
You'll want to use AJAX. You need to call a server-side resource from the JavaScript code, and jQuery has a very handy function for making the call to the server. But making the call from the JavaScript is only half the story, you'll also need something on the server listening for that call. It would essentially be another PHP script which acts as a page in and of itself, but would return data in the form of (most likely) JSON instead of HTML. It's not meant to be human-readable, but rather to be a sort of web service for your JavaScript code to use.
You can find a simple example here.
please get involved with the basics of PHP: http://tut.php-quake.net/en/
I want to call a JavaScript function through PHP and store the returned value in a PHP variable. How to do this?
You can't do that - javascript being a client side technology will execute after your PHP script has already finished so there's no way to do what you are looking to accomplish.