简体   繁体   English

在表单提交上发送错误/成功消息的路由

[英]Route for sending back an error / success message on form submission

I'm trying to make my contact form submission process work equally well whether the client has javascipt enabled or not. 无论客户端是否启用了javascipt,我都试图使我的联系表单提交过程同样有效。

If javascript is enabled, then validation will be handled client-side when an onsubmit event is triggered which calls my validateContactForm() function: 如果启用了javascript,那么当触发onsubmit事件并调用我的validateContactForm()函数时,将在客户端处理验证:

<form method="POST" action="email_processing.php" onsubmit="return validateContactForm();">

If false is returned, then the form isn't submitted and the client is directed to correct what he has done wrong. 如果返回false,则表单未提交,并指示客户端更正他所做的错误。

The form is submitted to the email_processing.php script on if true is returned by validateContactForm() or if javascript is disabled. 如果validateContactForm()返回true或者禁用了javascript,则表单将提交给email_processing.php脚本。 The php script then revalidates the data and if everything is OK, it sends an email. 然后php脚本重新验证数据,如果一切正常,它会发送一封电子邮件。

If I were relying on javascript, I could have made this process asynchronous and just have the php script return a message which would get handled by a callback function client-side. 如果我依赖于javascript,我本可以使这个过程异步,只需让php脚本返回一条消息,该消息将由回调函数客户端处理。 This would either be an error message or a success message. 这可能是错误消息或成功消息。

But because I want to make this work for people with javascript disabled, I can't do that. 但是因为我想让那些禁用javascript的人工作,我不能这样做。

If there's an error, I need to return the contact form page with error messages displayed in the contact form and if it's been a success I should maybe get rid of the contact form and just display a "thanks" message where the form used to be. 如果有错误,我需要返回联系表单页面,其中包含联系表单中显示的错误消息,如果成功,我应该删除联系表单,只显示一个“谢谢”消息,表单曾经是。

I'd be interested to know whether stackoverflow users would: 我有兴趣知道stackoverflow用户是否会:

  1. Submit the form to a php script which would then redirect to a different php / html page. 将表单提交到php脚本,然后重定向到另一个php / html页面。

  2. Have the validation / email sending functions at the top of the contact us php page itself and each time the contact us page is loaded we check to see if certains variables have been set and if so then we know that we need to validate and send the emails and display the correct format / message. 将验证/电子邮件发送功能放在联系我们php页面本身的顶部,每次加载联系我们页面时,我们检查是否已经设置了某些变量,如果是,那么我们知道我们需要验证并发送发送电子邮件并显示正确的格式/消息。

  3. If I should be thinking about this differently. 如果我应该以不同的方式思考这个问题。 This must be a very common situation to handle and I have done it many times in lttle applications for myself where I knew that I would have javascript enabled, but I need to do this for a client and I want to make sure that it's done right. 这一定是一个非常常见的情况要处理,我已经在自己的应用程序中多次完成了,我知道我会启用javascript,但我需要为客户端执行此操作,并且我想确保它已经正确完成。

Many thanks in advance 提前谢谢了

Here is what I do. 这就是我的工作。 first I use jQuery validate (thats just how I validate) and errors will show if there are any and won't let the user continue without validation 首先我使用jQuery验证 (这就是我如何验证),如果有任何错误将显示错误,并且不会让用户继续验证

then then I send form submission using jQuery ajax when it hits the server I also do a double check with php this allows me double protection and errors are sent back 然后我发送表单提交使用jQuery ajax当它命中服务器我也做了一个双重检查与PHP这允许我双重保护和错误被发回

if users don't have javascript enabled when the form is submitted validation is done errors messages are displayed 如果用户在提交表单时没有启用javascript,则验证完成后会显示错误消息

HTH HTH

It indeed is a very common situation to handle. 这确实是一个非常普遍的情况。

So you have two kinds of validation: Client-side and server-side. 因此,您有两种验证:客户端和服务器端。 Server-side is the most important, and I view client-side validation of more as a feature. 服务器端是最重要的,我将更多的客户端验证视为一项功能。 For example if you're filling out a form, it's "nice" to see whether it's filled out correctly without actually sending the request, but you should still do server-side validation for security reasons. 例如,如果您正在填写表单,那么在没有实际发送请求的情况下查看它是否正确填写是“很好”,但出于安全原因,您仍应该进行服务器端验证。

You'll want to do a server-side validation always, but it's actually better to do both. 您总是希望进行服务器端验证,但实际上两者都更好。 Here's how you should design it: 这是你应该如何设计它:

The form action should send the user to the .php-file. 表单操作应该将用户发送到.php文件。 If the content is validated, good, give him an ok message. 如果内容经过验证,那么请给他一个好消息。 If the content isn't validated, send him back to the form page and display an error message. 如果未验证内容,请将其发送回表单页面并显示错误消息。

If you're looking for a less abstract solution let me know. 如果您正在寻找一个不那么抽象的解决方案,请告诉我。

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

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