pelister
pelister

Reputation: 79

jQuery serialize on forms created by php output

How do I serialize a form in jquery that is generated by php output?

    $qrform = '<form action="" method="post" id="qrgen">
    <p><strong>Bookmark a Website</strong></p>
    Title<input type="text" name="title"><br />
    Url<input type="text" name="url"><br />
    <input type="submit" value="Generate"></form>';

    echo $qrform ;

Now this data is displayed by an AJAX request. A php function outputs the data and then shows the form in the browser window.

How do I serialize this form in jquery, when the form is submitted?

Upvotes: 2

Views: 154

Answers (2)

PhearOfRayne
PhearOfRayne

Reputation: 5050

Updated code if your getting the form via ajax you will need to bind the submit button like this:

​$(function () {
    $(document).on('submit', '#qrgen', function (e) {
        e.preventDefault(); //Prevent normal form submittion
        var $form = $(this);

        $.ajax({
            url: $form.attr('action'),
            data : $form.serialize(),
            success: function(data) {
                alert('Ajax was successful');
            },
            type : 'POST' //'POST' or 'GET'
        });
    });
});​

Upvotes: 1

DiverseAndRemote.com
DiverseAndRemote.com

Reputation: 19888

what you need is

jQuery('#qrgen').serialize();

Upvotes: 3

Related Questions