简体   繁体   English

使用JavaScript将php变量从一页发布到另一页

[英]Posting a php variable from one page to another using javascript

I'm trying to send aa php variable to another page using JS. 我正在尝试使用JS将php变量发送到另一个页面。 My hopeless attempt so far: 到目前为止我绝望的尝试:

header.php: header.php文件:

<script>
    var county = "<?php echo $county; ?>";
    $.post("ajax.php",county);
</script>

ajax.php: ajax.php:

<?php
  $county = $_POST['county'];
  echo $county;
?>

For context, I'm attempting to refine an infinite scroll script ( http://www.inserthtml.com/2013/01/scroll-pagination/ ). 对于上下文,我正在尝试优化无限滚动脚本( http://www.inserthtml.com/2013/01/scroll-pagination/ )。

I am currently using this script and want to add to it: 我当前正在使用此脚本,并想添加到它:

(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 Posts!', // 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 = '';
            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('ajax.php', {

                    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 images');

                        // 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);
var county = "<?php echo $county; ?>";
$.ajax({
     type: "POST",
        url: "ajax.php",
        data: {county:county},
        success: function (data)
        {
           alert(data);
        }
    });   

UPDATE: 更新:

It is now working. 现在正在工作。 Thanks for your help. 谢谢你的帮助。

Try this: 尝试这个:

$.post("ajax.php", { county: county }, function(result){
    console.log(result);  // response, if any
});
var county = "<?php echo $county; ?>";
$.ajax({
     type: "POST",
        url: "ajax.php",
        data: {county:county},
        success: function (data)
        {
           alert(data);
        }
    });   

Please use this code it will work 请使用此代码它将起作用

Please give some detail what you exactly want to do 请提供一些细节,您到底想做什么

or the easiest way you can use ajax jQuery for plugin 或最简单的方法,可以使用ajax jQuery进行插件

$('#myForm').ajaxForm(function(response){
   //Print response
   $('#ResultDiv').html(response);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM