0

I have a form that is called via the fancybox plugin login example.

Here is the code I have:

Form:

<form method="post" action="" id="events_form">
    <p class="clearfix"><label for="Name">Name:</label> <input type="text" name="Name" id="Name" /></p>
    <p class="clearfix"><label for="Company">Company:</label> <input type="text" name="Company" id="Company" /></p>
    <p class="clearfix"><label for="Email">Email:</label> <input type="text" name="Email" id="Email" /></p>
    <p class="clearfix"><label for="Tel">Tel:</label> <input type="text" name="Tel" id="Tel"/></p>
    <p class="clearfix"><input type="submit" value="Submit details" /></p>
 </form>

JavaScript / jQuery:

<script type="text/javascript">
    $(document).ready(function(){
        $("#event_trigger").fancybox({
            'padding'  : 0,
            'scrolling'  : 'no',
            'titleShow'  : false,
        });

        $("#events_form").bind("submit", function() {
            $.fancybox.showActivity();

            $.ajax({
                type  : "POST",
                cache : false,
                url  : "/events/index.php",
                data  : $(this).serializeArray(),
                success: function(data) {
                    $.fancybox(data);
                }
            });
            return false;
        });
    });
</script>

The PHP file returns and empty array. However the Firebug post tab displays the form data.

Also, I noticed that if I do

print_r($_SERVER['REQUEST_METHOD'])

This returns GET, even though I have specified POST.

3
  • you could try to use $.post() function Commented Feb 17, 2010 at 11:58
  • I've tried using $.post(), problems remains unfortunately Commented Feb 17, 2010 at 12:02
  • Why not using jQuery form plugins? It will make your work easier. Commented Feb 17, 2010 at 13:10

3 Answers 3

1
$(this).serializeArray()

with the name of the form CSS id (#my-form-ID, in this example) like this:

$("#my-form-ID").serializeArray()

Hope that solves it. It worked for me. ;-D

Sign up to request clarification or add additional context in comments.

Comments

0

$.ajax expects the parameter data to be an object or a string.

http://api.jquery.com/jQuery.ajax/ scroll down to data.

If you wrap your data in an object e.g. data: {array:$(this).serializeArray()} it may work. I'm not 100% sure on that though.

3 Comments

Hi, thanks for your answer, unfortunately wrapping data didn't work.
What do you get if you add textStatus to your success function e.g. function(data, textStatus){alert(textStatus);}?
thats returns "Success", still an empty array though.
0

You are doing an AJAX request on a form submit.

Unless the AJAX request is synchronous (which I wouldn't recommend, anyway) there is a danger that your form will be submitted before there is any chance for the AJAX request will return.

In the line:

$(this).serializeArray()

$(this) is referring to the the form element you have selected in the bind method. I'm assuming this is intended

1 Comment

Hi, yes this is intended. I got this code from the example on fancybox.net/blog and the example works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.