ajax成功后页面内容将如何重新加载

时间:2013-03-29 06:00:10

标签: php jquery ajax

如何在给定的php和javascript代码的jquery ajax成功函数之后只重新加载页面内容,而不是整个页面。最初它显示

  

成功

在ajax调用之后它将显示为

  

失败

重新加载整个页面..

Php代码:

 <?php
require 'dbconnect.php';
$sql="select * from tbl_status where id='1'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res);
if($row['status']=='1')
{
 ?>
Success <div class="status" style="visibility:hidden;">0</div>
 <?php
 }
 else
 {
 ?>
 Failed
<div class="status" style="visibility:hidden;">1</div>
 <?php
 }
 ?>
 <input type="submit" class="send"/>

Javascript代码:

<script type="text/javascript">
$(function(){
$('.send').click(function(){
var status=$('.status').text();
  $.ajax({
     type: 'POST', 
       url: 'http://localhost/status/updatestatus.php',
           data: 'status='+status,
         success  : function (e)
         {
           var response=e;
           alert(response);

         },
         error: function()
         {
         alert('error');
         }
      });
});
});
</script>

6 个答案:

答案 0 :(得分:1)

如果在记事本中写入了一些问题,请尝试尝试。

的index.php

<?php
require 'dbconnect.php';
$sql="select * from tbl_status where id='1'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res);
<div id="response">
if($row['status']=='1')
{
?>
   Success <div class="status" style="visibility:hidden;">0</div>
<?php
}
else
{
?>
Failed
<div class="status" style="visibility:hidden;">1</div>
<?php
}
?>
</div>
<input type="submit" class="send"/>


<script type="text/javascript">
$(document).ready(function(){
  $('.send').click(function(){
  var status=$('.status').text();
  $.ajax({
     type: 'POST', 
       url: 'http://localhost/status/updatestatus.php',
           data: 'status='+status,
         success  : function (data, status)
         {
           $("#response").html(data.status);
         },
         error: function()
         {
           alert('error');
         }
      });
});
});
</script>

updatestatus.php

<?php
require 'dbconnect.php';
$sql="update tbl_status set status=$_POST['status'] where id='1'";    
$res=mysql_query($sql);
$count = mysql_affected_rows();

if($count > 0){
    return json_encode(array('status'=>$count));
}else{
    return json_encode(array('status'=>0));
}

&GT;

答案 1 :(得分:1)

我已使用此代码更改状态并在页面上显示更改的值。这是一个Javascript函数,它使用对Ajax的Ajax请求来更改数据库中状态的值。

function changeStatus(divID,id,action,frontOrAdmin)
{


        xmlhttp=getobject();
        var query="id="+id+"&action="+action+"&frontOrAdmin="+frontOrAdmin;
        $oldStatus = document.getElementById(divID).innerHTML;
        document.getElementById(divID).innerHTML='<img src="images/loading_icon.gif">';
        xmlhttp.onreadystatechange=function()
        {
                if (xmlhttp.readyState==4)
                {
                        var response = xmlhttp.responseText;
                        var responseArr = response.split(",");

                        if(($.trim(responseArr[1])=="0")||($.trim(responseArr[1])=="1")){
                           document.getElementById(divID).innerHTML=responseArr[0];
                        }
                        else{           
                            alert(responseArr[0]);
                            document.getElementById(divID).innerHTML=$oldStatus;
                        }
                }
        }
        xmlhttp.open("GET","pass.php?type=changestatus&"+query,true);
        xmlhttp.send(null); 
}

答案 2 :(得分:0)

如果您在重新加载页面时遇到问题,则可以使用

window.location.href=window.location.href;

希望这有帮助

答案 3 :(得分:0)

考虑到你的ajax工作正常......你得到的答案是HTML

你不需要调用location.reload().. 你只需要将响应附加到DOMtree ..

<强>更新

<强> HTML

....
?>  //close php tag
<div id="displayStatus">  //create new div with id displayStatus
<?php
if($row['status']=='1')
{
 ?>
    Success <div class="status" style="visibility:hidden;">0</div>
<?php
 }
 else{
 ?>
    Failed <div class="status" style="visibility:hidden;">1</div>
 <?php
  }
 ?>
 </div> //end the div

<强> JQUERY

success  : function (e)
     {
       var response=e;
       alert(response);
       $('#displayStatus').html(response); //replace the content in that
     },

<强> updatestatus.php

 $status= $_POST['status'];
//make you query ..update your table 
 if($status == '0'){
     echo 'Failed <div class="status" style="visibility:hidden;">1</div>';
 }else{
    echo 'Success <div class="status" style="visibility:hidden;">0</div>';
 }

答案 4 :(得分:0)

使用load而不是重新加载

...
 success  : function (e)
     {
       $('your id or class name').load(php_file_name +'your_id_or_class_to_refresh');
       var response=e;
       alert(response);
       location.reload();
     },
....


// location.reload(); // <-- does not work so

答案 5 :(得分:0)

问题是:你的jQuery部分和你的PHP部分不兼容。 你没有真正改变你的回归状态,只是文字。要发出“失败”信号,请在PHP端使用一些标头。例如:

PHP中的

header('HTTP/1.0 403 Forbidden');
die('Failed');

然后你ajax会引发一个“错误”处理程序,而不是“成功”。

OR 您应该只使用“success”处理程序并测试返回的文本:

JavaScript中的

success: function(data) {
   if (data == 'Failed') {
       alert('ERROR');
       location.reload();
   } else {
        alert('Success');
   } 
},
相关问题