简体   繁体   中英

AJAX Page with Boostrap Modal Form - Submit don't work

i searched a lot about this problem, but I didn't find a solution, yet.

At first a short description about my setup to make my problem clearer.

  • Settings.php Page with a Menu, where you can select different settings categories
  • By clicking on one menu point the corresponding loads by ajax and is displayed.

$('#content').load("http://"+ document.domain + "/domainhere/settings/menupoint1.php");

  • On the menupont1.php page I got a list with mysql data.
  • I implemented a "edit" button for each row - while clicking on the edit button, a boostrap modal appears with a form and the corresponding data filled in and ready to edit.
  • When i now click on "Save changes", the POST-Request is always empty.

To realize the form submit, I already tried several codes:

eg:

   $.ajax({
        type: "POST",
        url: "php/form-process.php",
        data: "name=" + name + "&email=" + email + "&message=" + message,
        success : function(text){
            if (text == "success"){
                formSuccess();
            }
        }
    });

or

$(function(){
    $('#editform').on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: url, //this is the submit URL
            type: 'GET', //or POST
            data: $('#editform').serialize(),
            success: function(data){
                 alert('successfully submitted')
            }
        });
    });
});

At the moment:

while($xy= $xysql->fetch_assoc()) {
<div class="modal fade" id="edit-<?php echo $xy["id"] ?>" [..]>
<button id="submit>" class="btn btn-default">Save</button>
</div>

<script>
                     $(function() {
                        $('button#submit').click(function(){
                                $.ajax({
                                type: 'POST',
                            url: './test2.php',
                            data: $('form#modal-form').serialize(),
                                success: function(msg){
                                        $('#test').html(msg)
                                    $('#form-content').modal('hide');   
                                    },
                            error: function(){
                                alert('failure');
                                }
                                });
                        });
                    });
                    </script>

Maybe someone here could help me out with this problem? thank you very much :)

I've set up a minimal example of how this would work:

example html of two modals, which are produced in a loop in your case. I've now done it without a unique id, but with selecting via class.

<div class="modal">
          <!-- // this classname is new and important -->
   <form class="editform">
     <input name="test" value="value1">
     <button class="btn btn-default">Save</button>   
   </form>
</div>

<div class="modal">
   <form class="editform">
     <input name="test" value="value2">
     <button class="btn btn-default">Save</button>   
   </form>
</div>

Your javascript would be something like this:

$(function() {
   var formsAll = $('.editform');
                          // changed this to onSubmit, because it's easier to implement the preventDefault!
   formsAll.on('submit',function(e){
       e.preventDefault();
       var formData = $(this).serialize();
       console.log(formData);
       // add your ajax call here.
       // note, that we already have the formData, it would be "data: formData," in ajax.

   });
});

Note, that I don't have your real html structure, so details might vary. But you get the idea.

also available here: https://jsfiddle.net/a0qhgmsb/16/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM