-1

我在 php 文件中编写了一个 javascript,但是像这样从我的主页调用它

     <script type="text/javascript" src="<?php echo base_url() . 'js/inf-scroll/javascript_showuserinst.php' ?>"> </script>

问题:如果我在 javascript_showuserinst.php 文件中看到一行,因为$.post('/instruction/show_user_inst/<?php echo $userid; ?>', { 这里<?php echo $userid; ?>给了我问题。那行语法是否正确?

我看到view source它的显示<b>Notice</b>: Undefined variable: userid in <b>D:\xampp\htdocs\js\inf-scroll\javascript_showuserinst.php</b> on line <b>46</b><br />

但是,如果我在主页中复制并粘贴相同的 js 内容而不从外部文件中调用它,它就可以完美地工作。

javascript_showuserinst.php

Header("content-type: application/javascript");

?>  
(function($) {

        $.fn.scrollPagination = function(options) {

                var settings = { 
                        nop     : 10, // The number of posts per scroll to be loaded
                        offset  : 0, // Initial offset, begins at 0 in this case
                        error   : 'No More Data To Display!', // When the user reaches the end this is the message that is
                                                    // displayed. You can change this if you want. 
                                     .............
                                     .............
4

3 回答 3

3

JavaScript 文件应包含 JavaScript。

您的 JavaScript 文件包含一段 HTML。

不要<script>在 JavaScript 文件中包含 HTML 标记。

于 2013-05-14T10:54:37.053 回答
0
 <?php 
    echo "<script type='text/javascript' src='".base_url()."js/inf-scroll/javascript_showuserinst.php"."'></script>"
 ?>
于 2013-05-14T10:57:18.850 回答
-2

这必须在您的 PHP 脚本文件中

<script type="text/javascript" src="<?php echo base_url() . 'js/inf-scroll/javascript_showuserinst.php' ?>"> </script>

现在 javascript_showuserinst.php 可以如下所示

(function($) {

        $.fn.scrollPagination = function(options) {

                var settings = { 
                        nop     : 10, // The number of posts per scroll to be loaded
                        offset  : 0, // Initial offset, begins at 0 in this case
                        error   : 'No More Data To Display!', // When the user reaches the end this is the message that is
                                                    // displayed. You can change this if you want.
                        delay   : 500, // When you scroll down the posts will load after a delayed amount of time.
                                       // This is mainly for usability concerns. You can alter this as you see fit
                        scroll  : true // The main bit, if set to false posts will not load as the user scrolls. 
                                       // but will still load if the user clicks.
                }

                // Extend the options so they work with the plugin
                if(options) {
                        $.extend(settings, options);
                }

                // For each so that we keep chainability.
                return this.each(function() {       

                        // Some variables 
                        $this = $(this);
                        $settings = settings;
                        var offset = $settings.offset;
                        var busy = false; // Checks if the scroll action is happening 
                                          // so we don't run it multiple times

                        // Custom messages based on settings
                        if($settings.scroll == true) $initmessage = 'No more data to show';
                        else $initmessage = 'Click for more';

                        // Append custom messages and extra UI
                        $this.append('<div class="content"></div><div class="loading-bar">'+$initmessage+'</div>');

                        function getData() {  
                                // Post data to ajax.php
                                $.post('/instruction/show_user_inst/<?php echo $userid; ?>', {

                                        action        : 'scrollpagination',
                                    number        : $settings.nop,
                                    offset        : offset

                                }, function(data) {

                                        // Change loading bar content (it may have been altered)
                                        $this.find('.loading-bar').html($initmessage);

                                        // If there is no data returned, there are no more posts to be shown. Show error
                                        if(data == "") { 
                                                $this.find('.loading-bar').html($settings.error);   
                                        }
                                        else {

                                                // Offset increases
                                            offset = offset+$settings.nop; 

                                                // Append the data to the content div
                                                $this.find('.content').append(data);

                                                // No longer busy!  
                                                busy = false;
                                        }   

                                });

                        }   

                        getData(); // Run function initially

                        // If scrolling is enabled
                        if($settings.scroll == true) {
                                // .. and the user is scrolling
                                $(window).scroll(function() {

                                        // Check the user is at the bottom of the element
                                        if($(window).scrollTop() + $(window).height() > $this.height() && !busy) {

                                                // Now we are working, so busy is true
                                                busy = true;

                                                // Tell the user we're loading posts
                                                $this.find('.loading-bar').html('Loading...');

                                                // Run the function to fetch the data inside a delay
                                                // This is useful if you have content in a footer you
                                                // want the user to see.
                                                setTimeout(function() {

                                                        getData();

                                                }, $settings.delay);

                                        }   
                                });
                        }

                        // Also content can be loaded by clicking the loading bar/
                        $this.find('.loading-bar').click(function() {

                                if(busy == false) {
                                        busy = true;
                                        getData();
                                }

                        });

                });
        }

})(jQuery);

试试看

于 2013-05-14T11:04:40.450 回答