简体   繁体   English

JS在PHP可以发送email之前删除表单数据

[英]JS removes form data before PHP can send email

Ran into a small oddity here.在这里遇到了一个小怪。

When I submit a form, I have a JS click event listener on the submit button - this is to remove the HTML values and add a thank you message (pretty simple stuff).当我提交表单时,我在提交按钮上有一个 JS 单击事件侦听器 - 这是为了删除 HTML 值并添加感谢消息(非常简单的东西)。

Oddly, however, it seems that the JS is acting before the PHP because all emails have blank values until I remove the JS that removes the HTML values.然而,奇怪的是,JS 似乎在 PHP 之前起作用,因为在我删除删除 HTML 值的 JS 之前,所有电子邮件都有空白值。


The code is:代码是:

pretty simple HTML form (this has been cut down to show what the problem might be)非常简单的 HTML 形式(这已被削减以显示问题可能是什么)

HTML: HTML:

<form method="post" action="contactForm.php" id="contact-form" target="hiddenForm">
<textarea class="form-control input-outline reqItem" rows="6" cols="50" name="message" form="contact-form" placeholder="Message" required></textarea>
<button type="submit" class="btn btn-outline rounded-0 d-block d-md-inline-block submit-button">Submit</button>
</form>

JS (has an event listener on the submit button for click): JS(在点击提交按钮上有一个事件监听器):

if(document.querySelector('.submit-button')){
  
  document.getElementById('contact-form').addEventListener('submit', function(e){
    // Check that form is valid
    if(document.querySelector('form').checkValidity()){
    
      document.querySelectorAll('.checkbox').forEach(e => {
        if(e.classList.contains('checked')){
          e.classList.remove('checked')
        }
      })
    
      // Change messagae to thank you
      e.target.classList.add('submit-hide')
      document.querySelector('.submit-thankyou').classList.add('submit-show')
  
      // remove all field values  <<<<< issue seems to be being caused here

      document.querySelectorAll('.form-control').forEach(e => {
        // this causes the form to lose all data
        e.disabled = true;
      }) 
    }
  })
}

The e.disabled = true; e.disabled = true; Is going over each form input and making it disabled, I have also used e.value = "" .正在检查每个表单输入并将其禁用,我还使用e.value = "" Both of these stop PHP action in the form of setting variables as the values of the inputs.这两者都以将变量设置为输入值的形式停止 PHP 操作。

So the question is,所以问题是,

  1. am I doing something wrong (maybe right.....?)我做错了什么(也许是对的.....?)
  2. is there a way to allow the PHP to act first getting the values before removing them with the JS?有没有办法让 PHP 在用 JS 删除它们之前先获取值?

Feel free to ask any questions here.随时在这里提出任何问题。 Any insights are welcome.欢迎任何见解。

The submit event as part of the browser's form submission algorithm, before the browser even looks at the form's fields, so any changes you make as part of that event will be submitted to the server and you'll have access to them from PHP. submit事件作为浏览器表单提交算法的一部分,甚至在浏览器查看表单字段之前,因此您在该事件中所做的任何更改都将提交给服务器,您可以从 PHP 访问它们。

In order to change the form without affecting the submission, you'll have to do that at some point after the browser's form submission finishes, but before the page navigates to the form's action:为了在不影响提交的情况下更改表单,您必须在浏览器的表单提交完成后、页面导航到表单操作之前的某个时间点执行此操作:

document.getElementById('contact-form').addEventListener('submit', function(){
    setTimeout(function() {
        // Modify the form here.
    }, 0);
});

Remember that setTimeout(func, msecs) means "run func whenever you have the chance , but no earlier than msec milliseconds".请记住, setTimeout(func, msecs)的意思是“只要有机会就运行func ,但不早于msec毫秒”。 While the browser is running the form submission algorithm, no Javascript code (other than that for the synchronous events submit and formdata ) can run, so the earliest chance for func to run will be after the form is submitted.当浏览器运行表单提交算法时,没有 Javascript 代码(同步事件submitformdata )可以运行,因此func最早运行的机会将在表单提交之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM