weixin_33690963 2018-03-04 09:41 采纳率: 0%
浏览 44

提交带有表单ID的AJAX表单

I have multiple AJAX forms on a single page but they won't submit. The forms are within a Wordpress loop and each form has a unique ID. I've defined the form ID variable so I can trigger the current form ID to submit.

JAVASCRIPT

<script type="text/javascript">
  jQuery(document).ready(function ($) {

      var formID = $('form').attr('id')
      var is_sending = false,
    failure_message = 'Whoops, looks like there was a problem. Please try again later.';

    formID.submit(function (e) {
      if (is_sending || !validateInputs()) {
    return false; // Don't let someone submit the form while it is in-progress...
      }
      e.preventDefault(); // Prevent the default form submit
      $this = $(this); // Cache this
      $.ajax({
    url: '<?php echo admin_url("admin-ajax.php") ?>', // Let WordPress figure this url out...
    type: 'post',
    dataType: 'JSON', // Set this so we don't need to decode the response...
    data: $this.serialize(), // One-liner form data prep...
    beforeSend: function () {
      is_sending = true;
      // You could do an animation here...
    },
    error: handleFormError,
    success: function (data) {
      if (data.status === 'success') {
        // Here, you could trigger a success message
      } else {
        handleFormError(); // If we don't get the expected response, it's an error...
      }
    }
      });
    });

    function handleFormError () {
      is_sending = false; // Reset the is_sending var so they can try again...
      alert(failure_message);
    }

    function validateInputs () {
      var $name = $('#contact-form > input[name="name"]').val(),
      $email = $('#contact-form > input[name="email"]').val(),
      $message = $('#contact-form > textarea').val();
      if (!$name || !$email || !$message) {
    alert('Before sending, please make sure to provide your name, email, and message.');
    return false;
      }
      return true;
    }
  });
</script>

FORM

<form id="contact-form-<?php echo $pid; ?>">
  <input type="hidden" name="action" value="contact_send" />
  <input type="text" name="name" placeholder="Your name..." />
  <input type="email" name="email" placeholder="Your email..." />
  <textarea name="message" placeholder="Your message..."></textarea>
  <input class="button expanded" type="submit" value="Send Message" />
</form>
  • 写回答

2条回答 默认 最新

  • weixin_33705053 2018-03-04 09:52
    关注

    You can give a separate class to each AJAX form just to recognize it and get its ID by using data-* attributes. Such as:

    <script type="text/javascript">
    jQuery.on('submit', '.ajax-form', function(e){
    e.preventDefault();
    var formID = $(this).data('pid');
    ...
    //call $.ajax();
    ...
    }
    </script>
    

    And HTML form as:

    <form class='ajax-form' data-pid='<?php echo $pid; ?>'>
    ...
    ...
    </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算