Bootstrap dropdown not working after initial ajax form submission

Brian Powell

I have a dropdown on my page, manager.php, here. Sorry for the formatting - bootstrap.:

  <!-- Bootstrap -->
  <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="../../css/bootstrapstyle.css">
  <!-- Bootstrap Dropdown Enhancements-->
  <script type="text/javascript" src="../../js/dropdowns-enhancement.js"></script>
  <link rel="stylesheet" href="../../css/dropdowns-enhancement.css">

<div class="row">
  <div class="col-lg-6">
    <div class="input-group">
      <div class="input-group-btn">
        <button type="button" class="btn dropdown-toggle btn-primary" data-toggle="dropdown" aria-expanded="false">Search By <span class="caret"></span></button>
        <ul id="searchBy" class="dropdown-menu" role="menu">
          <li class="disabled"><a href="#">Search By...</a></li>
          <li><a href="#">cost</a></li>
          <li><a href="#">name</a></li>              
        </ul>
      </div>               
    </div>
  </div>
</div>

<div id="everything">
</div>

This code works fine when I load manager.php directly and the dropdown initializes, but this is not how I need the code to work.

The user begins on return.php; this page collects a bunch of data from the user and returns it to manager.php.

Once the user selects to do so on return.php, this code is run:

$.ajax({
      type: 'POST',
      url: 'manager.php',      
      data: {number:number, location:location, project:project, comments:comments},        
        success:function(data){
        $('#everything').html(data);                                                            
        }
  });

The ajax call works correctly, and it loads the data returned from manager.php into the everything div. It passes along the data as expected. The only thing that DOESN'T work upon loading manager.php into the DIV is the drop-down. I need to understand what I'm doing wrong to cause this functionality so I can prevent doing it in the future.

ICodeForCoffee

You need to manually reset the dropdown after you've loaded the data into the everything div. I've modified your AJAX call to show you where to put the call.

$.ajax({
      type: 'POST',
      url: 'manager.php',      
      data: {number:number, location:location, project:project, comments:comments},        
        success:function(data){
          $('#everything').html(data);
          $('#searchBy').dropdown();
        }
  });

The dynamic loading you're doing doesn't cause the DOM to reload forcing the drop down to be reinitialized. After you're AJAX call has completed, you can then reset the bootstrap drop down manually by calling .dropdown();.

EDIT

Generally functions in bootstrap for features are setup using a $( document ).ready() call. This function executes once, after the DOM has been loaded and only after the DOM has has been fully loaded. Since your manipulating the DOM, you are not causing the function to be triggered again, and you need to manually trigger the feature you need.

You also want to load your includes only once on each page. They need to on manager.php in order for the function be available when you go into your success method. I'd suggest using a template for your project so you manage all your includes in one place. Also, if your using manager.php as a page to be included in another page, it's okay if you don't have the reference to the JavaScript pieces won't work since you don't want users accessing that component on it's own.

The reload you are doing appears to be forcing the content to be re-added to the page, thus forcing the $( document ).ready() call in the included file to be re-executed. You can do this, but it's very inefficient.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bootstrap Form Validation or Required not working on Ajax submission

From Dev

Form Submission not working second time after submiting using Jquery and Ajax

From Dev

wordpress ajax form submission is not working?

From Dev

Auto form Submission not working Ajax

From Dev

wordpress ajax form submission is not working?

From Dev

Redirect after AJAX form submission

From Dev

Redirect after AJAX form submission

From Dev

Basic form submission with jquery/ajax not working

From Dev

Ajax form submission with Jquery validation plugin is not working

From Dev

AJAX form submission based on ID not working

From Dev

Bootstrap modal not closing after form submission

From Dev

Django bootstrap modal after form submission

From Dev

ajax not working .No alert shown after submission

From Dev

Form submission using ajax and page view moderation after the submission

From Dev

How to keep selected value in dropdown box after form submission?

From Dev

jQuery ajax form submission to php not working with response from server

From Dev

Bootstrap Form Submission and Modal

From Dev

Jquery form submission is not working

From Dev

Form submission is not working

From Dev

form submission not working?

From Dev

Angular submission form not working

From Dev

Ajax form submission issues

From Dev

Ajax Form Validation and Submission

From Dev

generic ajax form submission

From Dev

Rails/Ajax change form from create to update after submission

From Dev

Refresh Div's content after AJAX form submission

From Dev

How do i hide a form after submission is succesfull with Ajax/Jquery

From Dev

get redirect_to and flash notices to work after ajax form submission

From Dev

Don't reload page after form submission using ajax and jquery

Related Related

  1. 1

    Bootstrap Form Validation or Required not working on Ajax submission

  2. 2

    Form Submission not working second time after submiting using Jquery and Ajax

  3. 3

    wordpress ajax form submission is not working?

  4. 4

    Auto form Submission not working Ajax

  5. 5

    wordpress ajax form submission is not working?

  6. 6

    Redirect after AJAX form submission

  7. 7

    Redirect after AJAX form submission

  8. 8

    Basic form submission with jquery/ajax not working

  9. 9

    Ajax form submission with Jquery validation plugin is not working

  10. 10

    AJAX form submission based on ID not working

  11. 11

    Bootstrap modal not closing after form submission

  12. 12

    Django bootstrap modal after form submission

  13. 13

    ajax not working .No alert shown after submission

  14. 14

    Form submission using ajax and page view moderation after the submission

  15. 15

    How to keep selected value in dropdown box after form submission?

  16. 16

    jQuery ajax form submission to php not working with response from server

  17. 17

    Bootstrap Form Submission and Modal

  18. 18

    Jquery form submission is not working

  19. 19

    Form submission is not working

  20. 20

    form submission not working?

  21. 21

    Angular submission form not working

  22. 22

    Ajax form submission issues

  23. 23

    Ajax Form Validation and Submission

  24. 24

    generic ajax form submission

  25. 25

    Rails/Ajax change form from create to update after submission

  26. 26

    Refresh Div's content after AJAX form submission

  27. 27

    How do i hide a form after submission is succesfull with Ajax/Jquery

  28. 28

    get redirect_to and flash notices to work after ajax form submission

  29. 29

    Don't reload page after form submission using ajax and jquery

HotTag

Archive