0

I'm having a problem where the Ajax is not triggering the respected event.

I have two forms, one visible and one hidden.

  1. In the visible form, the user can enter an email address.
  2. When the user clicks the submit button of the visible form, the default submit is prevented, and through Ajax the email is validated.
  3. If the email address is not valid, an error message will appear.
  4. If the email address IS valid, the submit button of the hidden form should be clicked using JS (in the ajax-success function).

Here is more code for reference:

HTML

Visible Form

<form accept-charset="utf-8" id="contactForm1" style="margin-top:;" action="https://app.getresponse.com/add_contact_webform.html?u=IzDa" method="post">
    <input class="wf-input wf-req wf-valid__email" type="text" name="email" data-placeholder="yes" id="email" value="Enter Your Email Here" 
        onfocus="if (this.value == 'Enter Your Email Here') {this.value = '';}" 
        onblur="if (this.value == '') {this.value = 'Enter Your Email Here';}" 
        style="margin-top:0px;" />
    <br />
    <input type="submit" class="wf-button" name="submit1" value=" " style="display:inline !important; margin-top:-10px !important;" />    
</form>

Hidden Form

<form method="post" id="aweber" class="af-form-wrapper" action="http://www.aweber.com/scripts/addlead.pl">
    <div style="display:none;">
        <input type="hidden" name="meta_web_form_id" value="947846900" />
        <input type="hidden" name="meta_split_id" value="" />
        <input type="hidden" name="listname" value="awlist3599001" />
        <input type="hidden" name="redirect" value="http://www.aweber.com/thankyou.htm?m=default" id="redirect_37ecf313df3b6f27b92c34c2c00ef203" />
        <input type="hidden" name="meta_adtracking" value="ibb_test" />
        <input type="hidden" name="meta_message" value="1" />
        <input type="hidden" name="meta_required" value="email" />
        <input type="hidden" name="meta_tooltip" value="" />
    </div>

    <div id="af-form-947846900" class="af-form"><div id="af-body-947846900"  class="af-body af-standards">
        <div class="af-element">
            <label class="previewLabel" for="awf_field-66127140">Email: </label>
            <div class="af-textWrap"><input class="text" id="awf_field-66127140" type="text" name="email" value=""  />
            </div><div class="af-clear"></div>
        </div>
        <div class="af-element buttonContainer">
            <input name="submitaw" id="submitaw" class="submitaw" type="submit" value="Submit" tabindex="501" />
            <div class="af-clear"></div>
        </div>
        </div>
    </div>

    <div style="display:none;"><img src="http://forms.aweber.com/form/displays.htm?id=nCzsHCxsnAwM" alt="" /></div>
</form>

JS

Ajax for visible form

$('#contactForm1').click(function(e) {
    e.preventDefault();
    var email = $('#email').val();
    var self = this;

    $.ajax({
        type: 'POST',
        dataType: 'JSON',
        url: 'check.php',
        data: {
            email: email
        },
        success: function (data) {
            if (data.status == 'success') {
                self.submit();
            } else {
                alert('The e-mail address entered is not valid.');
            }
        }
    });
});

So here's how the scenario would be:

  1. User enters email address, and clicks the submit button.
  2. Ajax-call checks whether it's a valid email.
  3. If the email is valid, the Ajax success function of the visible form
    • first sets the email value of the hidden form
    • and then triggers the submit button of the hidden form

As said before, STEP 3 isn't working.

JayVicious
  • 25
  • 3
  • Not 100% sure about this because I use `$ .post()` myself.. but I think that instead of `,success:function...` you should use `.done(function...`. **source: http://api.jquery.com/jquery.ajax/** (bottom of the page) – myfunkyside Oct 01 '14 at 03:03
  • @myfunkyside , I think i found out the error here..The iframe is empty .. – JayVicious Oct 01 '14 at 03:05
  • `$.ajax({type:"POST", url:"some.php", data:{name:"John",age:"15"}}).done(function(msg){alert(msg);});` – myfunkyside Oct 01 '14 at 03:06
  • @myfunkyside, Can i ask a different favor instead? – JayVicious Oct 01 '14 at 03:06
  • @myfunkyside , I've edited the code on above.. Since the iframe method is not working. I set the form in the page hidden with different form action ID. Here's what I want it to happen , when the ajax trigger the click function in contactform1 , I want the same value to be passed in the form and click submit in the other's form – JayVicious Oct 01 '14 at 03:08
  • I don't fully understand why you have two forms – myfunkyside Oct 01 '14 at 03:34
  • @myfunkyside , I need to send it simultaneously if it's possible. – JayVicious Oct 02 '14 at 03:17
  • Is it correct what I said in my edits, that the problem only lies in the triggering of the submit-button of the hidden form? So the visible form does submit itself in the success function of the Ajax-call? Or was that also a problem? – myfunkyside Oct 02 '14 at 05:05
  • @myfunkyside, the visible form works like a charm just the invisible form doesn't want to get the value and submit it. Any luck? – JayVicious Oct 02 '14 at 05:08
  • could you update those two lines? You said you aren't using an iframe anymore, but in your question those two lines still have `frames[0]` in them.. how do they look now? – myfunkyside Oct 02 '14 at 05:14
  • @myfunkyside, I already removed the iframe planning cause it's appearing blank when I place it within the iframe. So i just display the form in the same page with none. So i'm wondering how to pass information to it and submit it. – JayVicious Oct 02 '14 at 05:19
  • This line.. `$(window.frames[0].document).find('#awf_field-66127140').val(email);` ..still tries to access an iframe. How have you changed that line (and the one after it) for your new situation without an iframe? – myfunkyside Oct 02 '14 at 05:21
  • @myfunkyside , I've removed it and trying different method. Which is the button with same class ID triggered with the Ajax also failed..So i'm stuck now. – JayVicious Oct 02 '14 at 05:24

1 Answers1

0

use complete instead success

$('#contactForm1').click(function(e) {
    e.preventDefault();
    var email = $('#email').val();
    var self = this;
    $.ajax({
        type: 'POST',
        dataType: 'JSON',
        url: 'check.php',
        data: {
            email: email
        },
        complete: function(data) {
            if (data.status == 'success') {
                self.submit();
            } else {
                alert('The e-mail address entered is not valid.');
            }
        }
    });
});
Nurdin
  • 23,382
  • 43
  • 130
  • 308