None
None

Reputation: 5670

Form within a form and a JSON call

In my page I have a form:

   <form method="post" id="confirm-order-form" name="confirm-order-form">

Inside the form I have written some scripts to make a JSON call:

<script type="text/javascript"><xsl:text disable-output-escaping="yes"><![CDATA[
    $(function() {
        $('#submit').click(function() {
             if ($('#nlapproved').attr('checked')) {
                newsletter();
            }
        });

        function newsletter()
        {
            $form = $('<form action="http://mydomain.createsend.com/t/j/s/jtes/" method="post" id="subForm" />');
            $form.append('<input type="hidden" name="cm-name" id="hidName" />');
            $form.append('<input type="hidden" name="cm-jtes-jtes" id="hidEmail" />');
            $form.append('<input type="hidden" name="cm-fo-pikty" id="hidPrivateBusiness" />');

            $form
                .find("#hidName")
                .val(']]></xsl:text><xsl:value-of select="$context//checkoutinformation/info[key='name']/value" disable-output-escaping="yes"/><xsl:text disable-output-escaping="yes"><![CDATA[');

            $form
                .find("#hidEmail")
                .val(']]></xsl:text><xsl:value-of select="$context//checkoutinformation/info[key='email']/value" disable-output-escaping="yes"/><xsl:text disable-output-escaping="yes"><![CDATA[');

            $form
                .find("#hidPrivateBusiness")
                .val(']]></xsl:text><xsl:value-of select="$acctype"/><xsl:text disable-output-escaping="yes"><![CDATA[');

            $.getJSON(
                $($form).get(0).action + "?callback=?",
                $($form).serialize(),
                function (data) {
                    if (data.Status === 400) {
                        alert("Error: " + data.Message);
                    } else { 
                        // 200
                        alert("Success: " + data.Message);
                    }
                }
            );
        }
    });
    ]]>
    </xsl:text>
</script>

My problem is that this thing does not work when the outer form is there--the code works fine otherwise. Note: I am redirecting this page to another physical server in the post back of my outer form and i have a lot of other controls in my first form so i cant simply avoid that. Can anyone help?

Upvotes: 12

Views: 384

Answers (2)

Tieson T.
Tieson T.

Reputation: 21191

1) Add a div outside the form that has the runat="server" attribute.

<form runat="server">

    <!-- stuff here -->

</form>

<div id="target"></div>

2) Write your jQuery selector to target that div.

$('#target').append('<form id="newform"></form>');

// add your controls...

$.post(
    'your_action', 
    $('#newform').serialize(), 
    function(result){ 
        // handle result... 
    }, 
    'json');

3) Do whatever you need to do with the server form...

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

The actual form is being submitted. Stop that:

$("#confirm-order-form").on('submit', function (e) { e.preventDefault(); });

EDIT: to submit ajax, then normal form:

...ajax.done(function () {
   $("#confirm-order-form").off('submit').trigger('submit');
});

After successful ajax completion, unbind the prevention of the form submission and trigger a submission.

Upvotes: 2

Related Questions