-2

I have the following function:

function loader($time,$page){
    echo "<script type='text/javascript'>
    $( document ).ready(function() {
            $('.register-container').hide();
            $('.top-color').animate({height:'100%'}, 500);
                window.setTimeout(  
                    function() {  
                         $('.loading-overlay').show();
                    },  
                    600
                );
                //Simulate loading... and then redirect.
                window.setTimeout(  
                    function() {  
                         $('.loading-overlay').hide();
                         $('.top-color').animate({height:'0px'}, 500);
                    },  
                    2600
                );




        }); 

    </script>";
    echo '<div class="loading-overlay" >
                      <div class="loading-circles">
                        <!-- MAINTAINS CONSISTANT COLOR CENTER -->
                        <div class="circle hold" ></div>
                        <!-- ITERATION POINT FOR ANIMATION -->
                        <div class="circle first"></div>
                        <div class="circle"></div>
                        <div class="circle"></div>
                      </div>
                      <!-- ICON -->
                      <div class="name"><img src="images/loader/loadertext-green.png" /></div>
           </div>';
    echo "<meta http-equiv=refresh content=$time;URL='$page'>";
}

Which should add the above jQuery code to the page, and execute it.

Now my problem is, that when the function is called in the page, like below:

<?php
if($checksuccess){ loader("4","/account"); }
?>

I get the following jQuery error:

Uncaught ReferenceError: $ is not defined

What am I doing wrong?

oliverbj
  • 5,771
  • 27
  • 83
  • 178

2 Answers2

2

Php is expecting a variable name after the $ sign because you are using double quotes.

Try changes the single quotes with double quotes inside the echo and sorround it with single quotes. The opposite that you have.

Alvaro Flaño Larrondo
  • 5,516
  • 2
  • 27
  • 46
0

Your second echo block starts and ends with a single quote, it should be a double quote. Also, the string inside of an echo must escape double quotes. You can generally use single quotes in html, but it is probably better to escape the double quotes.

echo "<div class=\"loading-overlay\" > <div class=\"loading-circles\"> Use escape \" on html attributes when echoing from PHP </div></div>";

Kirk Powell
  • 908
  • 9
  • 14