0

How do I call a jQuery function from PHP?

<?php
if ($error == 1) {
?>
 <script type="text/javascript">
 error('1');
 </script>
<?
}
?>

It doesn ́t work.

asked Jul 17, 2011 at 8:42
2
  • 1
    What exactly do you want to achieve? (You cannot call jQuery from PHP per se) Commented Jul 17, 2011 at 8:48
  • PHP is executed in the server and the output is sent to the browser, jQuery is Javascript which runs in the browser. You can do something like user834929 answered, however, it is a better idea to call PHP from jQuery using AJAX. Commented Jul 17, 2011 at 10:52

5 Answers 5

6

No you cannot call jQuery functions from PHP. But you can generate html with PHP that will execute jQuery functions at runtime.

  • Javascript is sent to the client and executed there
  • PHP is compiled at the server and then sent to the client

Big difference in that

answered Jul 17, 2011 at 8:52
Sign up to request clarification or add additional context in comments.

Comments

6

The script tags cannot be injected directly into php tags.... But if you echo it, the script will work.... The following code works :

<?php
if( some condition ){
 echo "<script>alert('Hello World');</script>";
}
?>

In case of using jQuery, you have to wrap the code in:

jQuery(function(){
 //Your Code
});

and then echo it....

Sharlike
1,7992 gold badges19 silver badges30 bronze badges
answered Sep 13, 2013 at 12:50

1 Comment

I just forgot to add ' ' into the alert() function... So please don't forget them... Thank You...
4

It depends on where you place this code fragment within the HTML output.

One of these solutions, I think, should work:

<?php
if ($error == 1) {
?>
<script type="text/javascript">
$(function() {
 error('1');
})
</script>
<?
}
?>
<?php
if ($error == 1) {
?>
<script type="text/javascript">
$(document).ready(
 function() {
 error('1');
 }
)
</script>
<?
}
?>
answered Jul 17, 2011 at 8:51

1 Comment

Okay, I can see other answerers suggesting that you can't invoke JS from PHP, and things like that. Of course, you can't. But it also doesn't mean that, for example, displaying some jQuery animation if some condition is met on PHP (server) side, is something you should avoid. So I think the question is legitimate.
0

You need to specify that error('1') is a call to a jQuery function:

jQuery.error('1');
// or, uning the $ alias:
$.error('1');
answered Jul 17, 2011 at 8:48

Comments

0
<?php
 if(this == that) {
 //do something in php
 } else {
 //do something with jquery (i.e. fade something in/out
 }

?>

OR using jquery $.ajax to call a PHP function

answered Jul 17, 2011 at 8:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.