2

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?

2 Answers 2

3

what you need is

jQuery('#qrgen').serialize();
Sign up to request clarification or add additional context in comments.

2 Comments

this is what I tried $ ( 'form' ).submit( function ( event ) { event.preventDefault( ); var myform = $( this ); $.post( "ajax.php" , myform.serialize(), function ( data ) { ( process data ) }); }); but when I click submit nothing happens
moreover the form is displayed in the browser window, but when I check the source of the page, there is no html for the form displayed.
1

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'
        });
    });
});​

3 Comments

steven that doesnt work either, form is displayed by ajax request that outputs the forms returned by php function. but the form displayed in the browser window doesnt have the source when I checked the page source. will that be the problem?
@user1922552 You need to bind the new submit button using the .on() method like my updated answer. It would be helpful to see your jQuery code.
@user1922552 Your welcome, glad its working for you. Please remember to click the check mark if your satisfied with my answer.

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.