Javascript in PHP isn't working

151 views Asked by At

I am using the supersized jquery script to use various sliding backgrounds for my wordpress page.

Now I wanted to make different slides for each site and need a php if request.

My code:

<?php if ( is_page(array('Restaurant'))) { 
    echo"<script type="text/javascript">
        jQuery(function($) {

            $.supersized({

                // Functionality
                slide_interval: 9000, // Length between transitions
                transition: 1, // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed: 1400, // Speed of transition

                // Components                           
                slide_links: 'blank', // Individual links for each slide (Options: false, 'num', 'name', 'blank')
                slides: [ // Slideshow Images
                    {
                        image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
                        title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
                    }, {
                        image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
                        title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
                    }, {
                        image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
                        title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
                    },
                ]
            });
        });
    </script>";}
?>

But after saving the code in my .php file the site doesn't load anymore. If I remove the php if request everything works fine again.

3

There are 3 answers

5
Dupuis David On BEST ANSWER

First I can suggest you to remove the echo of the code.

    <?php if ( is_page(array('Restaurant'))) { ?>
<script type="text/javascript">

        jQuery(function($){

            $.supersized({

                // Functionality
                slide_interval          :   9000,       // Length between transitions
                transition              :   1,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed        :   1400,       // Speed of transition

                // Components                           
                slide_links             :   'blank',    // Individual links for each slide (Options: false, 'num', 'name', 'blank')
                slides                  :   [           // Slideshow Images
                                                    {image : 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg', title : 'Hotel-Pension-Restaurant Zur Traube in Altenahr'}, 
                                                    {image : 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg', title : 'Hotel-Pension-Restaurant Zur Traube in Altenahr'},  
                                                    {image : 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg', title : 'Hotel-Pension-Restaurant Zur Traube in Altenahr'},
                                            ]
            });
        });

    </script>
<?php } ?>

You'll have a best visibility of the code and will not have quotes errors. Check your console with this method and tell us if you have an error

0
Velimir Tchatchevsky On

change <script type="text/javascript"> to <script type='text/javascript'> you are using double quotes - the same that open and close the echo statement

0
AudioBubble On

Your code is not working because you have not escaped your strings.

When you use double-quotes as your opening and closing quotes then you must escape other double-quotes inside them.

This applies also for single-quotes meaning you must escape single-quotes that are inside strings that use single-quotes as the opening and closing quotes.

NOTE: You do not need to escape double-quotes that are inside single-quotes and single-quotes that are inside double-quotes.

You need your escape your quotes like this:

<?php
  if(is_page(array('Restaurant'))) {
    echo("<script type=\"text/javascript\">
      jQuery(function($) {
        $.supersized({
          // Functionality
          slide_interval: 9000,
          // Length between transitions
          transition: 1,
          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
          transition_speed: 1400,
          // Speed of transition
          // Components                           
          slide_links: 'blank',
          // Individual links for each slide (Options: false, 'num', 'name', 'blank')
          slides: [
            // Slideshow Images
            {
              image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
              title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
            }, {
              image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
              title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
            }, {
              image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
              title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
            }
          ]
        });
      });
    </script>");
 } ?>

You can also remove echo altogether like this:

<?php if(is_page(array('Restaurant'))) { ?>
  <script type="text/javascript">
    jQuery(function($) {
      $.supersized({
        // Functionality
        slide_interval: 9000,
        // Length between transitions
        transition: 1,
        // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
        transition_speed: 1400,
        // Speed of transition
        // Components                           
        slide_links: 'blank',
        // Individual links for each slide (Options: false, 'num', 'name', 'blank')
        slides: [
          // Slideshow Images
          {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }
        ]
      });
    });
  </script>
<? } ?>

To make the code look a bit tidier you can remove the comments like this:

<?php if(is_page(array('Restaurant'))) { ?>
  <script type="text/javascript">
    jQuery(function($) {
      $.supersized({
        slide_interval: 9000,
        transition: 1,
        transition_speed: 1400,
        slide_links: 'blank',
        slides: [
          {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }
        ]
      });
    });
  </script>
<? } ?>