Refreshing ajax div with form data

2019-09-02 10:01发布

问题:

I've googled a lot but could not find the solution to this problem:

I have a div in my main page that loads another page with the JQuery function '.load'. This works well. In this loaded page I have a search option. When the form is submitted I want this ajax div to reload and the submitted data to be send along so I can use that in this ajax div.

How can I make such a construction? I have heard .load uses GET and my form sends POST. But using the .post function after the submit does not work.

Anyone?

Code sofar:

In my main page I have just a div, nothing important

The loaded page:

 <form id="formpie" name="zoek" action="" method="POST">
  <input type="hidden" name="zoek" value="zoek" id="zoek"/>
  <input type="text" name="tags" size="31" id="tags"/>
  <select name="categorie" id="categorie">
    <?php $imagelib = Imagelib::find_all();?>
        <option value="">alle</option>
    <?php  foreach($imagelib as $imagelib_id): ?>
        <option value="<?php echo $imagelib_id->id;?>"><?php echo $imagelib_id->naam; ?></option>
        <?php  endforeach;?> 
  </select>
  <input type="submit" name="zoek" id="search" value="Zoek" />
</form>

in my JS file I have:

The loading part:

jQuery("#select_portal").bind('click', function () {
            $("#dialog-form").css("display", "block");
            $("#dialog-form").css("top", "100px");
            $("#dialog-form").css("left", "auto");
            $("#dialog-form").css("right", "auto");
            $("#dialog-form").css("backgroundColor", "white");
            document.getElementById('dialog-form').style.visibility = 'visible';
            $("#dialog-form").load("imglib.php");
        });
    }

After the submit button has been clicked:

$('#formpie').submit(function (e) {
    e.preventDefault();
    var tags = $("#tags").val();
    var categorie = $("#categorie").val();
    $.ajax({
        type: "POST",
        url: "imglib.php",
        async: false,
        dataType: 'html',
        success: function () {
            $('#dialog-form').load("imglib.php", {tags : tags, categorie : categorie});
        }
    });
});

回答1:

Something like this should work:

$('#formpie').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: "imglib.php",
        async: false,
        data: $(this).serializeArray(),
        success: function(data) {
            $('#dialog-form').html(data);
        }
    });
});

You should send the data with the post. It won't automaticly send the form data along.