Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 619
None Avatar asked Dec 19 '12 13:12

None


1 Answers

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.

like image 124
Explosion Pills Avatar answered Oct 20 '22 04:10

Explosion Pills