php - PHP 表单提交是否会导致整个文件刷新?
问题描述
当用户点击提交时,使用<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
会导致整个页面刷新吗?还是只是 PHP 部分?
解决方案
该页面通常会在提交表单后重新加载,以显示提交表单后收到的响应。
为了防止你有两种方法:
target=_blank
在您的表单标签中使用
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="_blank">
- 用于
event.preventDefault();
防止表单的默认操作,然后使用手动发送数据,fetch
或者如果您使用 JQuery,您可能会使用ajax
. (我将使用 fetch 标准 API)
document.getElementByTagName("form")[0].addEventListener('submit', function(event) {
event.preventDefault();
const url = 'https://randomuser.me/api';
// The data we are going to send in our request
const data = {
name: 'Sara'
}
// The parameters we are gonna pass to the fetch function
const fetchData = {
method: 'POST',
body: data,
headers: new Headers()
}
fetch(url, fetchData)
.then(function() {
// Handle response you get from the server
});
});
推荐阅读
- python - 是否有与 Borland C 命令 putpixel 等效的 Matplotlib?
- r - 如何在 R 中安装 PolyaGamma 包?
- python - 如何仅选择 scipy.sparse csr_matrix 的某些行?
- javascript - Microsoft.XMLHTTP 不适用于已缩短的 URL
- python - 如何更改我的代码以使字符串不会更改为浮动
- jquery - ASP.Net MVC:具有动态列的 Jquery 数据表与 JSON 绑定
- python - 如何使用 Knee 算法自动有效地确定 K-means 聚类算法的聚类数?
- octobercms - OctoberCMS静态页面中的模型记录
- hadoop - HBase master 马上就下来了
- python - 三个字符重复的所有五个字母单词?