Ajax不止一次提交表单

时间:2015-12-12 07:04:32

标签: javascript php jquery ajax

  function form5() {
    var finance = [];
    var paymentmode = "";
    var errors = [];
    if (!$("input[id='payment[]']:checked").length > 0) {

      errors = "Please select at least one payment mode.";

    }
    if ($("input[id='payment[]']:checked").length > 0) {
      $("input[id='payment[]']:checked").each(function() {
        paymentmode += $(this).val() + ',';

      });
    }

    var x = _("form4");
    var k;
    if ($("input[id^='i-']").val() == null || $("input[id^='i-']").val() == "") {

      errors = "Please fill in all the fields properly .";

    }
    if ($("input[id^='i-']").val() == null || $("input[id^='i-']").val() == "") {

      errors = "Please fill in all the fields properly .";

    }
    if ($("textarea").val() == null || $("textarea").val() == "") {
      errors = "Please fill the cancellation policy ."

    }
    /*   for (k = 4; k <= 7; k++) {
                                        var b_val = [];
                                        b_val[k] = x.elements[k];
                                        finance[k-4] = x.elements[k].value;
                                        if (b_val[k].checkValidity() == false) {

                                            errors = 'Please ' + b_val[k].placeholder + ' Correctly .';
                                        }

                                    }  */
    if ($("input[id='declaration']:checked").length == 0) {

      errors = "Please select the declaration to proceed.";

    }
    if ($("input[id='terms']:checked").length == 0) {

      errors = "Please agree to the terms & conditions to proceed.";

    }

    if (errors.length <= 0) {
      for (k = 4; k <= 7; k++) {
        var b_val = [];
        b_val[k] = x.elements[k];
        finance[k - 4] = x.elements[k].value;

      }

      $.ajax({
        type: 'POST',
        url: 'submit.php',
        data: {
          finance: JSON.stringify(finance),
          paymentmode: paymentmode,
          username: username
        },
        success: function(data) {
          $.Notify({

            caption: 'finance Details Saved',
            type: 'success',
            timeout: 3000,
            content: data,
            keepOpen: false
          });
          $("button[id='form5']").prop('disabled', true);
          next();

        },

        error: function() {
          alert('An Error Occured Please try Again');

        }
      });

    } else {

      $.Notify({
        caption: 'Alert',
        type: 'success',
        timeout: 3000,
        content: errors,
        keepOpen: false

      });
    }
  }



<div id="section5" class="container sections ">
  <div class="col-lg-12">
    <h1 class="main-header">Finances Details </h1>
    <label><a href="Login/logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a>
    </label>
  </div>
  <div class="row main-form">
    <label>&#2660 Payment Modes :</label>
    <form id="form4" onsubmit="return false">
      <div class="payment-mode">
        <div class="col-md-6 ">
          <label class="input-control checkbox small-check">
            <input id="payment[]" type="checkbox" value="cash">
            <span class="check"></span>
            <span id="cash" class="caption">Cash</span>
          </label>
          <label class="input-control checkbox small-check">
            <input id="payment[]" type="checkbox" value="credit-card">
            <span class="check"></span>
            <span id="cc" class="caption"> Credit Card</span>
          </label>
          <label class="input-control checkbox small-check">
            <input id="payment[]" type="checkbox" value="demand-draft">
            <span class="check"></span>
            <span id="dd" class="caption">Demand Draft</span>
          </label>

        </div>
        <div class="col-md-6 ">
          <label class="input-control checkbox small-check">
            <input id="payment[]" type="checkbox" value="bank-transfer">
            <span class="check"></span>
            <span id="bt" class="caption">Bank Transfer</span>
          </label>

        </div>
      </div>

      <div class="clearfix"></div>
      <label>&#2660 Payment :</label>
      <div class="payment">

        <div class="col-md-6 ">
          <label>&#2660 Down Payment</label>
          <div class="input-control select ">
            <span class=" prepend-icon">Rs.</span>
            <input id="i-downpayment" type="text " placeholder=" Enter Down Payment">

          </div>

        </div>
        <div class="col-md-6 ">
          <label>&#2660 Full payment to be made</label>
          <div class="input-control select ">
            <span class=" prepend-icon"></span>
            <input id="i-fullpaydays" type="text " placeholder=" Enter Days">
            <div class="button-group">
              <span class="button" disabled>days before the event</span>
            </div>
          </div>

        </div>

      </div>
      <div class="clearfix"></div>

      <div class="col-md-4 ">
        <label>Deposit :</label>

      </div>
      <div class="col-md-4 ">
        <div class="input-control select ">
          <span class=" prepend-icon">Rs.</span>
          <input id="i-deposit" type="number " placeholder=" Enter Deposit Price">

        </div>

      </div>
      <div class="clearfix"></div>
      <div class="col-md-12 col-xs-12">
        <label class="input-label">Cancellation Policy :</label>
        <div class="input-control textarea">
          <textarea placeholder="Enter the cancellation policy" rows="4" cols="50"></textarea>
        </div>
        <div class="clearfix"></div>
        <label class="input-control checkbox small-check">
          <input id="declaration" placeholder="" type="checkbox" value="1">
          <span class="check"></span>
          <span id="" class="caption">The above mentioned details are accurate to my best knowledge</span>
        </label>
        <div class="clearfix"></div>
        <label class="input-control checkbox small-check">
          <input id="terms" placeholder="" type="checkbox" value="1">
          <span class="check"></span>
          <span id="" class="caption">I agree to the <a href="terms.html">terms and conditions</a></span>
        </label>
      </div>
      <div class="clearfix"></div>

      <div style="padding-top:5em;" class="col-md-12 col-md-offset-4">
        <button onclick="form5()" name="" class="button success">Submit</button>

      </div>
    </form>

任何人都可以帮助我,我在数据库中看到两个条目。我检查了后端PHP。很好。请检查上面的代码。 任何帮助都会很棒。 我正在调试,但我需要一个快速帮助。这可能会有所帮助。

2 个答案:

答案 0 :(得分:1)

问题已解决,意外删除了检查错误的条件,然后提交..

答案 1 :(得分:0)

在这里你我猜你的脚本可能是在没有范围和搁浅设计模式的情况下编写的。我不确定但试试这个。  把&#34;返回;&#34;每个if-else验证结束