0

Ok so I am trying to echo an function from jQuery inside of PHP.

I was searching a little bit, I did not found any exact answer but I found that I need to use ajax for it ?

I am not familiar with ajax so I have found this.

<?php 
    $status = "<script>
                 $.ajax({ 
                    url: '/',
                    data: {action: 'test'},
                    type: 'post',
                    success: function(output) {
                       notifyBox();
                    }
                 });
               </script>";
?>

I also have this part somewhere on the page:

<?php if(isset($status)) { echo $status; } ?>

I am not quite sure if I need all of that to execute the function ? But it works only if I put jQuery in head of the website.

I usually put all my scripts above </body> (at closing tag) and now it bothers me if I have to put all these scripts into head

Because of many pages I cannot separate all scripts now just cause of one page that bother me at the moment.

Can anyone please tell me, how do I call that function without getting console error of undefined '$' because jQuery is loaded at the end of the page.

Thank you in advance.

2
  • 4
    Just move that echo statement to somewhere AFTER jQuery is loaded. Commented Nov 30, 2012 at 20:20
  • Oh my... Now I am blushing :D ! Yeh that was my problem, thank you @Benjam Commented Nov 30, 2012 at 20:22

4 Answers 4

2

That should work so long as you make sure that you put your PHP if statement after the <script> tag that includes jQuery.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is being caused by jQuery not being loaded when that variable is echoed. When Javascript sees a function call, it executes it right then and there. To prevent this, you will need to echo your variable after jQuery has been loaded.

Example:

<script type="text/javascript" src="path/to/jquery.js"></script>
<?php
    echo $script;
?>

OR, you can put your AJAX call into a function that will be called later:

<?php
    $script = '<script type="text/javascript">function myFunc() {
        //Call AJAX here
    }
    </script>';
    echo $script;
?>

Then later, you can call your function: myFunc();

Comments

0

Make sure jquery is loaded. This should work:

$status = "<script type='text/javascript'>
                $(function(){
                   $.ajax({ 
                    url: '/',
                    data: {action: 'test'},
                    type: 'post',
                    success: function(output) {
                       notifyBox();
                    }
                 });
               });
               </script>";

4 Comments

This still has the same problem that $ is undefined at the time of execution unless jQuery was loaded somewhere before this script.
Yes, you are right. Just put jquery in the header and problems are gone.
Which is what the OP was trying to avoid.
I can't think of a valid reason not to load jQuery in your header.
0

$(document).ready() is also a jquery function, hence, try it with window.onload.

Comments

Your Answer

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.