0

I've got some jquery that submits a form for me. It works just fine. The only major problem is how simple it is.

The form submits the info to the database using my php script without refreshing the page, but the page isn't updated in any way to show the new data. What are some good ways to update the page or div so my new data is displayed after submitting the form. Below is my code

$(function() {
    $('#form').submit(function(e) {

        var data = $(this).serialize();

        // Stop the form actually posting
        e.preventDefault();

        // Send the request
        $.ajax({
            type: "POST",
            url: "submit.php",
            data: data,
            cache: false,
            success: function(html){
                $('textarea#joke').val('');
            }
        });
    });
});
4

2 回答 2

1

您非常接近,只需根据您的需要使用 html() 方法或 text() ,对于您的示例,我认为 text 更好,因为您想将文本放入 textarea

success: function(html){
                $('textarea#joke').text(html);
            }

但如果你想将一些 html 放入自定义 div 中

success: function(html){
                    $('#custom-div').html(html);
                }
于 2013-10-27T12:14:32.610 回答
0

假设从 submit.php 中,您返回一些值,就status = true好像表单成功提交一样status = false

然后在您的 ajax 代码中,您可以将其用作

success: function(html){
  if(html.status == true)
    $('textarea#joke').html('Form submitted succcessfully');
  else
    $('textarea#joke').html('ERROR!');
}

或者

success: function(html){
    $('textarea#joke').val(html.status);
}

这将更新 div 的内容$('textarea#joke')

希望这会对你有所帮助。

谢谢。

于 2013-10-27T12:15:05.307 回答