添加表单时PHP重新加载页面(可能需要ajax)

时间:2011-09-27 17:28:10

标签: php ajax jquery

很抱歉该主题的标题可能不正确,但这是我提出的最佳标题。

所以,我正在为网站构建管理员面板。

我有一个页面,在页面的某些部分,我想刷新它并加载另一个表单。

让我们说添加一个时间表,在页面的某个地方,我想在点击链接后立即显示此表单。

当用户保存它时,我希望该表单消失,而不是具有显示所有计划的列表。

enter image description here

我不想使用框架 - 我不是框架的支持者。该面板使用PHP构建。

也许这可能是用Ajax实现的?如果是 - >怎么样?任何好示例或教程的链接。

1 个答案:

答案 0 :(得分:1)

是的,这将通过ajax解决。

这是一个应该刷新页面的代码示例

$('#button').click(function() {
    $.ajax({
        url: 'path/to/script.php',
        type: 'post',
        dataType: 'html', // depends on what you want to return, json, xml, html?
                       // we'll say html for this example
        data: formData, // if you are passing data to your php script, needed with a post request
        success: function(data, textStatus, jqXHR) {
            console.log(data); // the console will tell use if we're returning data
            $('#update-menu').html(data); // update the element with the returned data
        },
        error: function(textStatus, errorThrown, jqXHR) {
            console.log(errorThrown); // the console will tell us if there are any problems
        }
    }); //end ajax

    return false; // prevent default button behavior
}); // end click

jQuery Ajax

http://api.jquery.com/jQuery.ajax/

剧本解释。

1 - 用户点击按钮。

2 - Click功能启动对服务器的XHR调用。

3 - url是php脚本,它将根据发布的值处理我们发送的数据。

4 - 类型是POST请求,需要数据才能返回数据。

5 - 在这种情况下,dataType将为html。

6 - 我们发送到脚本的数据可能是分配给变量formData的表单元素的序列化。

7 - 如果XHR返回200,则在控制台中登录返回的数据,以便我们知道我们正在使用的是什么。然后将该数据作为html放在所选元素中(#update-menu)。

8 - 如果出现错误,控制台会为我们记录错误。

9 - 返回false以防止默认行为。

10 - 全部完成。

相关问题