简体   繁体   English

如何使用Ajax刷新PHP文件本身?

[英]How can I refresh a PHP file with itself using Ajax?

Alright guys I'm trying to make a filter system for posts using ajax and a select box. 好的,我正在尝试使用ajax和选择框为帖子创建过滤器系统。 I am able to get the value from the select box no problem. 我能够从选择框中获取值没问题。 But my issue is that when I try to include the selected value in my PHP file it doesn't do anything. 但是我的问题是,当我尝试在PHP文件中包含选定的值时,它什么也没做。 I have a file called public_wall.php. 我有一个名为public_wall.php的文件。 This file contains PHP, Javascript, and HTML. 该文件包含PHP,Javascript和HTML。 How can I refresh this div whenever a user selects a different filter option? 每当用户选择其他过滤器选项时,如何刷新此div? Basically I need the selected value to be passed onto my public_wall.php file and then I want to plug it into the PHP function that fetches the posts thats's in the same file and then I want to refresh that same file to display the filtered results. 基本上,我需要将选定的值传递到我的public_wall.php文件中,然后将其插入获取相同文件中帖子的PHP函数,然后刷新该文件以显示过滤的结果。 Here is my Javascript code. 这是我的Javascript代码。

$("#postRatings").on("click", function(e) {
selectedRatingFilter = $("#postRatings option:selected").val();
    var dataString = "timeFilter="+selectedRatingFilter;    
    jQuery.ajax({
        type: "POST",
        url: site_url+"public_wall.php",
        data: dataString,
        dataType: "json",
        cache: false,
        success: function(response){
             hideSpinner();
             jQuery('#postsPagingDiv').remove();
             jQuery('#wsc_midbox').html(jQuery(response.htmls).fadeIn(400));
             setpost_ids(response.all_post_id);
             jQuery('#paging_in_process').val(0);   
        }
    });
});

When the dataType is set to "json" nothing happens. 当dataType设置为“ json”时,将不会发生任何事情。 But when it is set to html it prints some javascript code. 但是,当将其设置为html时,它将打印一些javascript代码。 Please help. 请帮忙。 The PHP file is too large to include here, but it basically contains PHP, HTML, and Javascript and some PHP functions that do sql queries. 该PHP文件太大,无法在此处包含,但它基本上包含PHP,HTML和Javascript以及一些执行SQL查询的PHP函数。 What is the best way to achieve a filter mechanism for my setup? 为我的设置实现过滤器机制的最佳方法是什么?

And on the public_wall.php file I want to get the value like so: 在public_wall.php文件上,我想像这样获取值:

$ratingFilter = isset($_REQUEST['timeFilter']) ? intval($_REQUEST['timeFilter']) : 0;

And then plug it into the PHP function that fetches the posts which is in the public_wall.php file also so that I can filter the posts based on the selected value. 然后将其插入到获取public-wall.php文件中帖子的PHP函数中,这样我就可以基于所选值过滤帖子。 And then finally I want to refresh the public_wall.php file with the new results. 最后,我想用新结果刷新public_wall.php文件。 I hope that makes sense. 我希望这是有道理的。 Please help. 请帮忙。

This is the output when I set my dataType to "html" 这是我将dataType设置为“ html”时的输出

    <script>
        function refreshPosts() {/* only posts comments likes and count updated. */ 
            var posts = jQuery("#all_post_id").val();   
            var arrays = posts.split(',');
            var dataString = "postids="+posts;



   jQuery.ajax({
        type: "POST",
        url: site_url+"includes/update_wall.php",
        data: dataString,
        dataType: "json",
        cache: false,
        success: function(response) {
            var x = response;
            //############ skip posts whose comments are being read by users
            var ExemptedPostsIDs = jQuery("#exemptedPostsID").val();


            var ExemptedArray    = ExemptedPostsIDs.split(',');
            ExemptedArray = ExemptedArray.sort();
            //////////////
            for (i=0; i<arrays.length; i++) {
                var val = 'row'+arrays[i];
                if(x[val]) {
                    if(!inArray(arrays[i], ExemptedArray))                                      
                    jQuery("#ajax_wall_"+arrays[i]).html(x[val]);
                } else {
                    jQuery('#PostBoxID'+arrays[i]).parent().fadeOut(500);
                }
            }
        }
    });
}
function inArray(needle, haystack) {
    var length = haystack.length;
    for (var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;

}
function refreshWall() {/* loads new posts real time */
    var posts = jQuery("#all_post_id").val();   
    var pageUsing = jQuery('#pageUsing').val();
    var dataString = "update_posts=1&postids="+posts+'&pagex='+pageUsing;
    jQuery.ajax({
        type: "POST",
        url: site_url+"public_wall.php",
        data: dataString,
        dataType: "json",
        cache: false,
                success: function(response) {
                    if(response.all_post_id) {
                        jQuery('#wsc_midbox').prepend(jQuery(response.htmls).fadeIn(400));
                        setpost_ids(response.all_post_id);
                    }
                }
            });
        }
    </script>

I suggest you keep the form with select element and any JavaScript on the outer frame. 我建议您将带有select元素的表单和所有JavaScript放在外部框架上。

Via ajax, only load the results to a seperate DIVision below that. 通过ajax,仅将结果加载到下面的单独的DIVision中。

When you put an Ajax response to a div, any JavaScript inside it will not be executed. 当您将Ajax响应放入div时,其中的任何JavaScript都不会执行。

For the best throughput with Ajax, you should consider loading a json response via Ajax and create HTML elements on the client side. 为了使用Ajax获得最佳吞吐量,您应该考虑通过Ajax加载json响应并在客户端创建HTML元素。 That way it becomes much easier to pull additional variables to front-end JS from server side along with the same request/response. 这样,将其他变量与相同的请求/响应一起从服务器端拉到前端JS变得容易得多。

But that becomes bit difficult when you have a template engine in the back-end. 但这在后端具有模板引擎时会变得有些困难。 You can still send the HTML content in a json value, so you can easily pass the "all_post_id" as well.. 您仍然可以通过json值发送HTML内容,因此也可以轻松地传递“ all_post_id”。

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

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