0

I'm working on a project and my problem is calling a jQuery function like this placing it in my php file. I've tried searching a lot but I'm always coming out with an error:

Function:

$(function(){
 $(".panorama-view").panorama360();
 });

Php file

<?php

echo '<link rel="stylesheet" href="/css/panorama360.css" rel="stylesheet" >';
echo '<script src="/js/jquery.mousewheel.min.js" ></script>';
echo ' <script src="/js/jquery.panorama360.js" ></script>';
echo '<script>  $(function(){ $(\'.panorama-view\').panorama360(); }); </script>';
echo '</script>';

 if(isset($_POST['upload'])) {

 $image_name= $_FILES['image']['name'];
 $image_type= $_FILES['image']['type'];
 $image_size= $_FILES['image']['size'];
 $image_tmp= $_FILES['image']['tmp_name'];

 if(move_uploaded_file($image_tmp,"uploadedimg/$image_name"))
 {
     echo "<script type='text/javascript'>alert('File Uploaded!');</script>";
 }
$folder= "uploadedimg/";
if(is_dir($folder)) {

    if($handle = opendir($folder)){
        while(($file= readdir($handle)) !=false){
            if($file === '.' || $file === '..') 
                continue;
            echo '<div class="panorama round" style=" width:1200px; height:500px; padding:10px ;background-color:#444; position: relative;">';
            echo '<div class="panorama-view">';
            echo '<div class="panorama-container">';
            echo '<img src="uploadedimg/'.$file.'" data-width="4077" data-height="500" alt="Panorama" />';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
         closedir($handle);
    }   
}     
} ?>
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
menna
  • 41
  • 1
  • 10
  • 5
    you can _not_ call a client side function from a server side script. But you _include_ javascript depending on php code. – Jeff Dec 17 '16 at 01:44
  • please be more specific: which jQuery function do you want to call based on which php-state/action/... And what error do you get now? – Jeff Dec 17 '16 at 01:46
  • i want to call this function: $(function(){ $(".panorama-view").panorama360(); }); – menna Dec 17 '16 at 01:47
  • when do you want to "call" this function? – Jeff Dec 17 '16 at 01:48
  • i've tried putting the function in the php file in this way but im messing up in someway here: echo ''; – menna Dec 17 '16 at 01:49
  • that's alright! What error did you get? – Jeff Dec 17 '16 at 01:49
  • this is the error: Parse error: syntax error, unexpected '').panorama360(); }); – menna Dec 17 '16 at 01:51
  • i want to call it when im previewing my image after uploading – menna Dec 17 '16 at 01:51
  • you need to escape the quotes `'` inside the js-code with `\\`` like this: `echo '';` or use double quotes `...(".panorama-view")...` – Jeff Dec 17 '16 at 01:52
  • 1
    Possible duplicate of [PHP Echo - Escaping quotes](http://stackoverflow.com/questions/25593049/php-echo-escaping-quotes) – Jeff Dec 17 '16 at 01:55
  • i've made your edits but its still not working – menna Dec 17 '16 at 02:00
  • "still not working" means what exactly? – Jeff Dec 17 '16 at 02:04
  • an you please edit your post and add the line where you are echoing the JavaScript? – EhsanT Dec 17 '16 at 02:12
  • @jeff im getting an error – menna Dec 17 '16 at 09:35
  • @EhsanT Post is edited – menna Dec 17 '16 at 09:35
  • First of all, you can use double quotes so you do not need to escape single quotes like this: `echo '';` and second, Please be as more specific as you can. What do you mean be _getting an error_. Still getting the same error as `Parse error: syntax error, unexpected '').panorama360(); });...`? at same line? – EhsanT Dec 17 '16 at 15:58
  • also you have an extra `echo '';` line in your code which you have to delete it – EhsanT Dec 17 '16 at 16:20
  • I've edited my code accordingly but now i'm getting different errors, You can check them from this link [(https://drive.google.com/file/d/0B6qEe3DyVcm6OEpFdnBDN3QzWXM/view?usp=sharing)] And the JQuery error im getting it from two other js files i got them from an internet to help me but i didnt want to upload them as it would be too much code? Please never mind me as i am a beginner in php html & javascript and a new user in Stackoverflow – menna Dec 19 '16 at 09:22
  • @menna: I've checked your Google Drive screenshot about the error you are facing; do check my post below for an effective resolution. – nyedidikeke Dec 19 '16 at 11:12

1 Answers1

1

You get Uncaught ReferenceError: jQuery is not defined or Uncaught ReferenceError: $ is not defined error (as seen in your shared screenshot in comments under your post) because you have not included the jQuery library in your project.

Both errors are identical and pointing to the same problem as explained above.

Note that you CANNOT use panorama360 without making use of the jQuery library as it (jQuery library) is a required dependency.

To include the jQuery library in your project, you have two (2) main options; either:

You consume it from a content delivery network (CDN) by including it in your project as shown in the snippet below,

<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>

or,

downloading and referencing it directly from your stored location

<script src="/path/to/jquery-3.1.1.min.js"></script>

There is a last option which has to do with combining the earlier two mentioned above, using one as a graceful failover; here is the illustration:

<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="path/to/jquery-3.1.1.min.js"><\/script>')</script>

(With the above, you can make use of the jQuery library in your project by consuming it from the CDN and automatically load the version on your sever should the later fail. More details here)

It's important to note that your jQuery library declaration SHOULD occur before the referencing of your panorama360 JavaScript resources; either:

<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
<script src="/js/jquery.mousewheel.min.js"></script>
<script src="/js/jquery.panorama360.js" ></script>

Should you chose to use PHP echo() function to handle your file inclusion, do use a single quote or escape your double quotes. ... more details here.

So, you should be doing something like this:

<?php
echo "<link rel='stylesheet' href='/css/panorama360.css'>";
echo "<script
    src='https://code.jquery.com/jquery-3.1.1.min.js'
    integrity='sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8='
    crossorigin='anonymous'></script>";
echo "<script src='/js/jquery.mousewheel.min.js'></script>";
echo "<script src='/js/jquery.panorama360.js'></script>";
echo "<script>$(function(){ $('.panorama-view').panorama360(); });</script>";

To reference your resources without making use of PHP the raw HTML way as discussed in comment below,

<?php
    // Should you want to run any PHP codes before referencing your resources,
    // you may do so here.
    // Remember: you MUST close this section as below this comments so as to
    // mark the end of your PHP code
?>
<link rel="stylesheet" href="/css/panorama360.css">
<script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
<script src="/js/jquery.mousewheel.min.js"></script>
<script src="/js/jquery.panorama360.js"></script>
<script>$(function(){ $('.panorama-view').panorama360(); });</script>

<?php
    // Here, other PHP codes
?>
Community
  • 1
  • 1
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
  • Phewww thankyouu very much but i would just ask for a last thing if i am linking a .css file in my php file is it possible to do so? and if yes is this syntax of linking is correct? `echo "";` – menna Dec 19 '16 at 12:06
  • Yes; it is possible. Using PHP **`echo()`** function as illustrated in the last example above, you can achieve that just as you've did in your comment right above, where the value of your `href` attribute ought to be the path to your resource *(desired stylesheet)*. You can equally do that the plain HTML way without having to use PHP for both CSS and JavaScript file inclusions should you desire to do so. Updating my answer with an example to that effect. – nyedidikeke Dec 19 '16 at 12:12
  • And unfortunately im getting this error in this jquery file [(https://drive.google.com/file/d/0B6qEe3DyVcm6bmw1TUJUdG9VbE0/view?usp=sharing)] – menna Dec 19 '16 at 12:14
  • Please post a new question with that error as it isn't related to this current one and link it up here so I could check it up. Remember to post your current code as it were when you encountered that error. – nyedidikeke Dec 19 '16 at 12:37
  • @menna: Any progress *(relating to your last shared [screenshot](https://drive.google.com/file/d/0B6qEe3DyVcm6bmw1TUJUdG9VbE0/view?usp=sharing)) above)*? – nyedidikeke Dec 19 '16 at 13:31
  • Yes i've posted a question and thanyou for your reply [http://stackoverflow.com/questions/41224280/insert-jquery-function-in-php-file-uncaught-typeerror()] – menna Dec 19 '16 at 14:03