PHP 页面上的多个联系表单 - 无需页面刷新

发布于 2024-09-30 15:48:24 字数 399 浏览 0 评论 0原文

是否可以在一个页面上包含多个联系表单,并且仍然可以在不刷新页面的情况下验证和发送电子邮件。

我已经使用了下面的教程,但如果明显复制,这仍然只能验证原始表单...

http://www.ajaxfreak.com/2009/12/03/submit-a-form-without-page-refresh-using-jquery/

我可以为每个所需的表单复制 JS 等,但必须有一种更有效的方法来做到这一点,也许可以使用隐藏值和隐藏值。表格ID?

有人有什么想法吗?

谢谢

Is it possible to have multiple contact forms on one page and still validate and send an email without the page refreshing.

I have used the tutorial below but if copied obviously this still only validates the original form...

http://www.ajaxfreak.com/2009/12/03/submit-a-form-without-page-refresh-using-jquery/

I could copy the JS etc for each of the required forms but there must be a more efficient way of doing this, maybe with hidden values & form ids?.

Anybody have any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-10-07 15:48:24

这当然是可能的,不幸的是演示使用 ID 手动构建数据字符串,这并不容易转换为多种形式。在表单上使用 .serialize() 方法会方便得多。

以下(简化的)示例适用于页面上具有“ajax”类的任意数量的表单。具有“required”类的输入元素将首先检查值,如果缺少,将应用背景红色:

$('form.ajax').submit(function() {
   var validates = true;
   $(this).find('input.required').each(function() {
      if($(this).val() == '') {
         $(this).css('background', '#ff9999');
         validates = false
      } else {
         $(this).css('background', '#ffffff');
      }
   }
   if(validates)  {
      $.ajax({
         type: "POST",
         url: "bin/process.php",
         $(this).serialize(),
         success: function() {
             // things to do on success here!
         }
      });
   }
   return false; // prevent normal form submission.
});

It is certainly possible, unfortunately the demo manually builds the data string manually using IDs, which doesn't make it easy to convert to multiple forms. It would be much more convenient to use the .serialize() method on the form.

The following (simplified) example should work for any number of forms on the page that have the class "ajax". Input elements with the 'required' class will be checked for a value first, and will have background red color applied if it's missing:

$('form.ajax').submit(function() {
   var validates = true;
   $(this).find('input.required').each(function() {
      if($(this).val() == '') {
         $(this).css('background', '#ff9999');
         validates = false
      } else {
         $(this).css('background', '#ffffff');
      }
   }
   if(validates)  {
      $.ajax({
         type: "POST",
         url: "bin/process.php",
         $(this).serialize(),
         success: function() {
             // things to do on success here!
         }
      });
   }
   return false; // prevent normal form submission.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文