javy
javy

Reputation: 275

Can't get form data to post via AJAX using Fancybox

I'm using Fancybox to have a contact form pop up when a link is clicked. Then it POSTs the form data to a php file, an email goes out and a success message comes back.

After I submit my form is that the page reloads and the data seems to go nowhere. If I submit the form without using AJAX it works fine but then loads a new page.

Form:

<div style="display:none">
     <div id="questions">
       <form id="question-form" action="" method="POST">
         <p>Name</p> <input type="text" name="name">
         <p>Email</p> <input type="text" name="email">
         <p>Item</p> <input type="text" name="item">
         <p>Message</p><textarea name="message" rows="6" cols="25"</textarea>
         <br/>
         <input type="submit" value="Send">
      </form>
    </div>
</div>

Script

$("#question-form").bind("submit", function() {

    $.fancybox.showActivity();

    $.ajax({
    type        : "POST",
    cache       : false,
    url         : "/includes/question-mailer.php",
    data        : $(this).serializeArray(),
    success     : function(data) {
                      $.fancybox(data);
                  }
});

return false;
});

What am I doing wrong?

Upvotes: 1

Views: 5769

Answers (1)

javy
javy

Reputation: 275

It was a simple mistake, I was putting the script above in its own <script> tags instead of in the main fancybox attach script. My final code is:

<script type="text/javascript">
$(document).ready(function(){
    $("a.lightbox").fancybox({
        'transitionIn'  :   'fade',
        'transitionOut' :   'fade',
        'speedIn'       :   600, 
        'speedOut'      :   200, 
        'overlayShow'   :   false
    });

    $("#question-form").bind("submit", function() {

        $.fancybox.showActivity();

        $.ajax({
            type        : "POST",
            cache       : false,
            url         : "/includes/question-mailer.php",
            data        : $(this).serializeArray(),
            success     :function(data){
                            $.fancybox(data);
                         }
        });

        return false;
    });

});     
</script>

Upvotes: 2

Related Questions