简体   繁体   English

$ POST没有更新

[英]$POST isn't updating

I'm having a problem whit my script here and I don't understand why. 我的脚本在这里遇到问题,我不明白为什么。 After testing a lot I understood that the main problem is the Ajax-request. 经过大量测试后,我了解到主要问题是Ajax请求。 Also I have been using the Ajax-request in another version of my script... Back then the changing of my content went very well... But now I wanted to enable the back-button so I asked here how that would work... I got a very good answer, so I tried to do that whit the help of this tutorial now this below is the result: now the changing of the url works and the old-content is fading out but the new-content (from the request) isn't updating… Update : its's like not even sending the request... Could someone please tell me why?? 另外,我还在其他版本的脚本中使用了Ajax请求...那时我的内容更改非常顺利...但是现在我想启用后退按钮,所以我在这里问这将如何工作。 ..我有一个很好的答案,所以我尝试在本教程的帮助下做到这一点,现在的结果如下:现在更改url的工作原理,旧内容逐渐消失,而新内容(来自请求)没有更新… 更新 :甚至没有发送请求…有人可以告诉我原因吗?

Thank you in advanced 谢谢高级

And sorry for my bad English. 抱歉我的英语不好。

JavaScript: JavaScript:

$(function () {

    var newHash = "",
        $content = $("#content"),
        $el;

    $('ul li a').click(function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function () {

        newHash = window.location.hash.substring(1);
        url=$(this).attr("href");
        if (newHash) {
            $content

            .fadeOut(200, function () {

                //                 url=url.replace('#','');

                $('#loading').css('visibility', 'visible'); //show the rotating gif animation

                $.ajax({
                    type: "POST",
                    url: "load_page.php",
                    data: 'page' + url,
                    success: function (msg) {

                        if (parseInt(msg) != 0) //if no errors
                        {
                            $('#content').html(msg); //load the returned html into pageContet

                        }
                        $('#loading').css('visibility', 'hidden'); //and hide the rotating gif
                    }

                });

                $('ul li a').removeClass("current");
                $("'ul li a'[href='" + newHash + "']").addClass("current");
            });

        };

    });

    $(window).trigger('hashchange');

});

PHP: PHP:

<?php
if(empty($_POST['page'])) die("0");

$page = $_POST['page'];

//  validate it as alphanumeric before reading the filesystem
if (preg_match('/^[a-z0-9]+$/i', $_POST['page']) && file_exists('article/'.$page.'.html')) {
  echo file_get_contents('article/'.$page.'.html');
}
else echo 'There is no such page!';
?>

Update: In the firebug-plugin I get this: 更新:在firebug插件中,我得到了:

Error: Syntax error, unrecognized expression: 'ul li a[href='anmeldung'] 错误:语法错误,无法识别的表达式:'ul li a [href ='anmeldung']

这就是我的FireBug Consol的样子... does this meens the URL was empty? 这是否表示网址为空?

Change this line: 更改此行:

$("'ul li a'[href='" + newHash + "']").addClass("current");

Into this one: 进入这一个:

$('ul li a[href="' + newHash + '"]').addClass('current');

At least that's the syntax error FireBug is complaining about. 至少那是 FireBug抱怨的语法错误。

You are sending data to your PHP script in the wrong way. 您以错误的方式将数据发送到PHP脚本。 You should be sending it as key=value , you are sending it like keyvalue , so your PHP script never gets the page variable. 您应该以key=value形式发送它,就像keyvalue那样发送,因此您的PHP脚本永远不会获得page变量。

So change your script from 因此,从

data: 'page'+url,

to

data: 'page='+url,
//         ^
 $(function() {

    var newHash      = "",
        $content = $("#content");



    $('ul li a').click(function() {
        window.location.hash = $(this).attr("href");
        return false;
    }); 

    $(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);
        url=newHash
        if (newHash) {
            $content

                .fadeOut(200, function() {

         url=url.replace('#','');

 $('#loading').css('visibility','visible'); //show the rotating gif animation

   $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        success: function(msg){

               if(parseInt(msg)!=0) //if no errors
            {
                 $content.fadeIn(200, function() {
            $('#content').html(msg);}); //load the returned html into pageContet

            }  $('#loading').css('visibility','hidden');//and hide the rotating gif
        }

    });

                        $('ul li a').removeClass("current");
                        $('ul li a[href="' + newHash + '"]').addClass('current');
                    });

        };

    });

    $(window).trigger('hashchange');

});

Ahhh I found the Error the problem was that I set url to late... Thanks every person - robot who looked and helped me solve this ^^ 啊,我发现了错误,问题是我将网址设置为延迟...感谢所有人-看着并帮助我解决了这个问题的机器人^^

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

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