0

A php include (a form submission) prints this when the form inputs were valid.

if ( $valid ) {
    echo "<script type='text/javascript'>
    $(document).ready(function() {
        console.log('success');
        notificationOutput('success', 'Successfully subscribed');
    });
    </script>";

In my header.php I'm including jQuery.

However when doing the thing above my console tells me that it doesn't know $. It doesn't make a difference if I write jQuery(document).ready(…

What do I miss here? As said above the echo is inside an included file so the header with all its scripts is already on the page.

Thank you in advance.

matt
  • 42,713
  • 103
  • 264
  • 397
  • 1
    probably jquery is not properly included – ianace Aug 29 '12 at 07:48
  • i can't see closer for ' {' , is it a typo? – Kuf Aug 29 '12 at 07:48
  • 1
    Make sure the jQuery is included before the echoing of that script, that the jQuery path is correct and that you didn't call `$.noConflict` anywhere. Or, just look at the source of the generated page and check if the script is there with the right path. – Fabrício Matté Aug 29 '12 at 07:48
  • Always get it working in html/js first, before trying to involve PHP, as said, likely caused because js reads your code sequentially. – Cups Aug 29 '12 at 07:49
  • @FabrícioMatté Well, you're right. The thing is it doesn't work anywhere on the page and I found out why. I had `` When removing the `async` everything works fine. Is there something I'm missing here? Can I keep the `async` but wait for the file to be loaded for inline scripts? – matt Aug 29 '12 at 10:13
  • a similar question http://stackoverflow.com/questions/2194992/jquery-is-not-defined – ianace Aug 29 '12 at 10:20
  • Well, `async` is part of the HTML5 spec and as the name implies, it tells the browser to load the script asynchronously (e.g. don't wait for it to load before executing other scripts). So, there's your problem. `:P` Here's some [reference](https://developer.mozilla.org/en-US/docs/HTML/Element/Script). – Fabrício Matté Aug 29 '12 at 10:26

2 Answers2

0

you use " so the $ is interpreted, switch the quotes and you'll be fine

Soundz
  • 1,308
  • 10
  • 17
  • php does not evaluate "$()" but prints it out as is. You are referring to ${} – timidboy Aug 29 '12 at 07:56
  • But `$` is immediately followed by `(` so it prints as one would expect. – Ja͢ck Aug 29 '12 at 07:56
  • At least in PHP5.4+, `$(` is clearly a not valid variable name so it gets ignored (echoes as it was typed). The OP also says that switching to `jQuery(document)...` doesn't make a change, but single quotes are good practice anyway. – Fabrício Matté Aug 29 '12 at 07:56
  • Or a [NOWDOC](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc)* in this case. – Fabrício Matté Aug 29 '12 at 08:00
  • 1
    doing a ` ` will help clear things up – ianace Aug 29 '12 at 08:12
  • @ianace good idea. However, it doesn't make a difference. I still get `Uncaught ReferenceError: $ is not defined `… @Soundz chaning the quote doesn't make a difference either. – matt Aug 29 '12 at 10:09
0

This isnt my answer i just copied it in here JQuery - $ is not defined

credit goes to @Mike Trpcic

That error can only be caused by one of three things:

Your JavaScript file is not being properly loaded into your page You have a botched version of jQuery. This could happen because someone edited the core file, or a plugin may have overwritten the $ variable. You have JavaScript running before the page is fully loaded, and as such, before jQuery is fully loaded. You should check the Firebug net panel to see if the file is actually being loaded properly. If not, it will be highlighted red and will say "404" beside it. If the file is loading properly, that means that the issue is number 2.

Make sure all javascript code is being run inside a code block such as:

$(document).ready(function () { //your code here }); This will ensure that your code is being loaded after jQuery has been initialized.

One final thing to check is to make sure that you are not loading any plugins before you load jQuery. Plugins extend the "$" object, so if you load a plugin before loading jQuery core, then you'll get the error you described.

Community
  • 1
  • 1
ianace
  • 1,646
  • 2
  • 17
  • 31