简体   繁体   English

Ajax Post PHP Div刷新

[英]Ajax Post PHP Div Refreshing

It is me again. 又是我。 I am getting so frustrated with this code it is not even funny. 我对这段代码感到非常沮丧,甚至都不是很有趣。 It's not that I am wanting to post it again. 不是我要再次发布。 It is just that now I understand the where the problem was in the code and wanted to see if you guys can help me figure the last part out. 只是现在我了解了代码中的问题所在,并想看看你们是否可以帮助我弄清最后一部分。

Basically I am trying to refresh a div without reloading the entire page. 基本上,我试图刷新div而不重新加载整个页面。 It's killing me. 这太痛苦了。 Here is some more information on it: 这里是一些更多的信息:

here is my js file first 这是我的js文件第一

    $(function() { 

   $(".button").click(function() { 
  // validate and process form 
     // first hide any error messages  
         var email = $("input#email").val();   
         //var dataString = '&email=' + email; commented out    
           var dataString = email;
            //try insted this    //alert (dataString);return false;  
              $.ajax({  type: "POST",  dataType:'HTML', 
              //or the appropiate type of data you are getting back 
               url: "http://www.edshaer.com/EdinburgCISD/Gorena/Gorena.php", data: {email:dataString},

                //in the php file do $email = $_POST['email']; 



                  //not a good practice but you can try with it and without it 
                   success: function() {  


                $("#div").fadeOut($("#div").html());

                   $("#div").fadeIn($("#div").html());
                       $("#email").val('');  

                   // Change the content of the message element    

                      // Fade the element back in 
                         }    });
                            //ajax ends 
                                         return false; });
                                         //click ends   
                                          });//document ready ends

Now the problem that I am running into with this code is on the Ajax part. 现在,我在此代码中遇到的问题在Ajax部分上。 After placing the alert(), I have relized that if I use the function() like this: 放置alert()之后,我认为如果使用如下function():

success: function(data)

Then the alert came out blank. 然后警报发出空白。 The reason behind it is that my URL is going to my php file, but my div that I am trying to refresh is on my html file. 其背后的原因是我的URL将转到我的php文件,但是我要刷新的div位于html文件中。 Meaning if I do this: 意思是如果我这样做:

 success: function(data) {  
                $("#div").html(data)}

I am sending blank data because it's trying to get the div from my php file instead of my html file. 我正在发送空白数据,因为它试图从我的php文件而不是html文件中获取div。

Now if I do this: 现在,如果我这样做:

    $("#div").html()

Then that gives me the div that is in my html file. 然后,这给了我html文件中的div。

By knowing what is going on now, Can you guys please help me??? 通过了解现在发生的事情,你们能帮我吗???

This might be a problem relating to the response from the php script. 这可能是与来自php脚本的响应有关的问题。 Jquery doesn't always correctly render the Ajax response as html. jQuery并不总是正确地将Ajax响应呈现为html。

Setting dataType: html in the $.ajax({ ... }) call can help. $.ajax({ ... })调用中设置dataType: html会有所帮助。 Also setting header("Content-Type: text/html"); 还要设置header("Content-Type: text/html"); at the top of your php ajax script. 在您的php ajax脚本的顶部。

My dear you should generate some sort of html in your php file that you want to generate in your div. 亲爱的,您应该在要在div中生成的php文件中生成某种html。 Then you will see that you are having some content in data in the success function. 然后,您将看到成功函数中的data中包含一些内容。 This is an easy approach. 这是一个简单的方法。 But there is also some other approach that is more efficient but it needs some sort of search. 但是,还有其他一些更有效的方法,但是它需要某种搜索。 This is the implementation of Client Side Scripting . 这是客户端脚本的实现。 You can do this with the help of a jquery plugin jquote2 . 您可以在jquery插件jquote2的帮助下完成此操作 I hope it will work for you. 希望它对您有用。

You're using 您正在使用

$("#div").fadeOut($("#div").html());
$("#div").fadeIn($("#div").html());

Both are wrong, jQuery .fadeIn() and .fadeOut() arguments are either [duration,] [callback] or [duration,] [easing,] [callback]. 两者都是错误的,jQuery .fadeIn().fadeOut()参数是[duration,] [callback]或[duration,] [easing,] [callback]。 None take HTML as input. 没有一个将HTML作为输入。

Try changing 尝试改变

$("#div").fadeOut($("#div").html());

to

$("#div").fadeOut();

and moving it outside the $.ajax call to hide the previously showed (if any) results before the post and also change 并将其移至$ .ajax调用之外,以将先前显示的结果(如果有的话)隐藏在发布之前,并进行更改

$("#div").fadeIn($("#div").html());

to

$("#div").html(result).fadeIn();

Also change 也改变

success: function()

to

success: function(result)

Hope it helps. 希望能帮助到你。

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

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