Update the select box after adding a new entry

I have two jsp pages add.jsp and view.jsp. There is a dropdown list with all userids on view page which will be automatically fetched from database when the page loads (I'm using document.ready event to retrieve the values).

When i add a new user on add.jsp page and click submit, it will redirect to view.jsp page with a success message. However, the dropdown will not show the new userid added. I have to close and re-run the project so that it appears next time.

How to append the select box with newly added user ?

    view.jsp

<script>

$(document).ready(function() {
    $.get('MyController', function(response){
        var $select = $(#myselect);
        $.each(response,function(key,value){
            $('<option>').val(key).text(value).appendTo($select);
        });
    });
});
</script>
..
..
..
<body><form>
<select id=myselect></select>
</form>
</body></html>

Mycontroller.java

doGet (HttpServletRequest req, HttpServletResponse res){
//construct json object
//write to response

}

doPost (HttpServletRequest req, HttpServletResponse res){
//adduser to DB
//return userId generated
/*send the generated id to view.jsp page*/

RequestDispatcher rd = req.getRequestDispatcher("view.jsp");
req.setattribute("generatedId",id);
rd.forward(req,res);
}


The generated Id is sent to view.jsp page. I want to append this new id to the select box in view page along with the existing id's
(which were already loaded using javascript).

Answers


Since you are fetching the vales from database on page load your dropdown will not be getupdated untill page reloads.. if want to update with out refresh what you need to do is capture userid from textbox on submit click you need to append for drop down of if userid generated by database return updated data from your controller on sucess of ajax bind it to dropdown

var newOption = $('<option value="'+yournewvalue+'">'+yournewvalue+'</option>');
 $('#yourdropdownid').append(newOption);

Need Your Help

android- Replacing fragment in ViewPager from NavigationBar

android android-fragments android-viewpager android-cardview fragmentmanager

I'm developing an Android application that allow to view the results of hockey matches.

Devise + Legacy DB: "password" column

ruby-on-rails ruby devise ruby-on-rails-4 legacy-database

I'm trying to slowly migrate an old CakePHP site to Rails 4. Because of the massive effort required, the site needs to be moved over gradually, only migrating a single piece at a time, and starting...