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

对不起该主题的标题可能不正确,但这是我提出的最好的标题。

所以,我正在建立一个网站的管理面板。

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

让我们说添加一个时间表,在页面的某个地方,我希望只要点击链接就会显示这个表单。

当用户保存它时,我希望该表单消失,并且代替显示所有日程表的列表。

在此处输入图像描述

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

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

是的,这将用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 – 单击function启动对服务器的XHR调用。

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

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

5 – 在这种情况下,dataType将是html。

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

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

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

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

10 – 全部完成。