Fancybox sends form data to other php page

2020-05-02 03:57发布

问题:

This is the original script of fancybox

<script>
$(document).ready(function() {
    $("#fancybox-manual-b").click(function() {
        $.fancybox.open({
            href : 'go.php',
            type : 'iframe',
            padding : 5
        });
    });
});
</script>

I want to send the data from the following form to POST them to "go.php" page via fancybox iframe script
I want to open "go.php" in the same/current window via fancybox

<form name="fancy" action="go.php" method="POST">
    <input name="text1" type="text">
    <input name="text2" type="text">

    **<input type="button" value="Submit" id="fancybox-manual-b"/>
    **<input type="submit" value="Submit" id="fancybox-manual-b"/>
</form>

If I used type="button", fancybox iframe works but POST don't send anything to go.php
If I used type="submit", fancybox iframe doesn't work but POST sends the form data to go.php correctly

回答1:

When you're clicking on a 'Submit' button, the browser doesn't know that you want to send your form.

Just use type="button" and change your javascript:

<script>
$(document).ready(function() {
    $("#fancybox-manual-b").click(function() {
        $.post('go.php', $(this).parent().serialize())
            .done(function() {
                $.fancybox.open({
                    href : 'go.php',
                    type : 'iframe',
                    padding : 5
                });
            });
    });
});
</script>