Search code examples
javascriptjspservletstreejqtree

Submitting JSP form data to servlet


I currently have this tree node which was created from http://mbraak.github.io/jqTree/. I am trying to implement this tree, so that when a user clicks on a node in the tree, it will send the data to my servlet. Is there any way to do this? I am currently using JSP. My initial solution was to add a button so that the button (with form tag) will do post action when it is being clicked(after selecting my node), but i would like to know if there is any solution without using a button. I also thought of using ajax but im new to this and am not sure if it works. Really need some help. Thanks My Tree:

$('#tree1').tree({data: data});
    $('#tree1').bind(
             'tree.click',
            function(event) {
                if (event.node) {
                    // node was selected
                        node = event.node.name;
                    alert(node);
                    // send node value to servlet
                }
                else {
                }});

HTML

<div id="tree1"></div>

My Initial idea

  $('#saveCat').click(function(){
        document.getElementById('mainCat').value = node;
        document.getElementById('action').value = "savecategory";
     });


  <form action="TopicCloudServlet">
    <button id="saveCat" class=" catbtn btn-primary">Save</button>
    <input type="hidden" id="mainCat" name="mainCat" value="" />
    <input type="hidden" id="action" name="action" value="" />
  </form>

Solution

  • Ok i found 1 solution which is very simple( why didn't i thought of it)

    1. Add an id to the form that is going to be submitted.
    2. At the javascript where u want your form to be submitted,

      document.forms["FormID"].submit();