【问题标题】:Do PHP form submissions cause the whole file to refresh?PHP 表单提交是否会导致整个文件刷新?
【发布时间】:2020-05-04 07:12:37
【问题描述】:

当用户点击提交时,使用<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post"> 会导致整个页面刷新吗?还是只是 PHP 部分?

【问题讨论】:

    标签: php html ajax xss


    【解决方案1】:

    页面通常会在提交表单后重新加载以显示在提交表单后收到的响应。

    为了防止你有两种方法:

    1. 在表单标签中使用target=_blank
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="_blank">
    
    1. 使用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
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多