【发布时间】:2020-05-04 07:12:37
【问题描述】:
当用户点击提交时,使用<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post"> 会导致整个页面刷新吗?还是只是 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
});
});
【讨论】: