简体   繁体   English

用Ajax响应刷新div

[英]Refresh a div with ajax response

So i have a comments box with a form (textbox and submit). 所以我有一个带有表单的评论框(文本框和提交)。 This gets validated and then sent to the php page via ajax. 这将得到验证,然后通过ajax发送到php页面。

If all okay, the response is set and the page continues. 如果一切正常,则设置响应,然后页面继续。

I want however the div to refresh itself so that you can see the new comment. 但是,我希望div刷新自身,以便您可以看到新的注释。

I dont want anything complicated like appending etc just simply to reload the div (data is pulled from database). 我不希望任何复杂的事情,例如追加等,仅仅是为了重新加载div(数据是从数据库中提取的)。

I have used this in the past but it doesnt work: 我过去曾经使用过,但是没有用:

 $('#divid').load('page.php #divid'),

any idea how i can convert this: 任何想法,我怎么可以转换这个:

        if (check == true) {
        $.ajax({
        type: "POST",
        url: "process/addcomment.php",
        data: $targetForm.serialize(),
        dataType: "json",
        success: function(response){

    if (response.databaseSuccess)
       $comment.after('<div class="error">Comment Added</div>');
    else
       $ckEditor.after('<div class="error">Something went wrong!</div>');

}
        });
    }
    return false;
});

}); });

To refresh the div once the success gets responded? 成功响应后要刷新div吗?

if i try to add another $return to my php eg 如果我尝试将另一个$ return添加到我的php中,例如

} catch (Exception $e) {
$return['databaseException'] = $e->getMessage();
}

$return['databaseSuccess'] = $dbSuccess;
$return['responseHtml'] = 'Post Updated';

echo json_encode($return);

the javascript doesnt work and i just get the php page load with the returns printed.. 的JavaScript无法正常工作,我只是得到与打印返回的PHP页面加载。

Thanks. 谢谢。

If you do 如果你这样做

$("#divid").html( "Comment Added" )

it will set the html within that div. 它将在该div中设置html。

I think you are meaning to do this 我认为您的意思是这样做

$('#divid').load('page.php');

checkout the jQuery .load documentation 查看jQuery .load文档

First you need some PHP code on the sever side that generates the HTML for the div. 首先,您需要在服务器端生成一些PHP代码,以便为div生成HTML。 Then use this JavaScript 然后使用此JavaScript

if (check == true) {
  $.ajax({
    type: ...,
    url: ...,
    data: ...,
    dataType: "html"
  })
    .done(function(responseHtml) {
      $("#yourDiv").html(responseHtml);
    });
}

I assume you are processing the comment on the server after it has been submitted (validation/sanitation/etc.). 我假设您在提交评论(验证/卫生/等)之后在服务器上处理评论。 Therefor I would return the processed comment as part of the response and then insert it into the HTML: 因此,我将返回处理后的注释作为响应的一部分,然后将其插入HTML:

$.ajax({
    type: "POST",
    url: "process/addcomment.php",
    data: $targetForm.serialize(),
    dataType: "json",
    success: function(response){
        if (response.databaseSuccess) {
            // if you have a designated empty div already, you can use .html()
            $("#divid").html('<div class="success">'+response.commentText+'</div>');
            // if you want to append it to an already existing list of comments, you can use .append()
            $("#divid").append('<div class="success">'+response.commentText+'</div>');
        }
        else {
            $ckEditor.after('<div class="error">Something went wrong!</div>');
        }
    }
});

Of course, this will require you to add response.commentText to your returned JSON object. 当然,这需要您将response.commentText添加到返回的JSON对象中。

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

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