1

I have an form and, when I submit it, I want to remain on the same page. I just want to treat the informations sent to the server and remain on the same page.

For this I have the form:

<form action='adicionaLoja' method='post'>
<input type='text' autocomplete='off' name='nome'/>
<button type='submit' id='saveStore' class='btn' value='Save'>Save</button>
<input type=hidden name='xValue'/><input type=hidden name='yValue'/></form>

And here's where I'm treating the data:

@RequestMapping("adicionaLoja")
public void adiciona(Loja loja) {
        System.out.println("adicionou - " + loja.getNome() + " X: " + loja.getxValue() + " Y: " + loja.getyValue());
}

With this code, when I hit the submit button, the server tries to redirect to adicionaLoja.jsp. But I do not have this file created, I just want to hit submit button and stays on the same page. I've tried to return the name of the page where the form is created, but this way the page is reloaded, I also don't wanna to reload it, just wanna to send the request to the server.

How can I do it?

EDIT: My java script file:

var form = $('#submitLoja');

      form.find('submit:first').click( function() {
        $.ajax( {
          type: "POST",
          url: "adicionaLoja",
          data: form.serialize(),
          success: function( response ) {
            console.log( response );
          }
        } );
      } );

My form (Note that this form is declared in the same .js file I've declared the submit click event):

<form action='adicionaLoja' id='submitLoja' method='post'><input type='text' autocomplete='off' name='nome'/>
<button type='submit' id='saveStore' class='btn' value='Save'>Save</button>
<input type=hidden name='xValue'/><input type=hidden name='yValue'/></form>

Here's the server side:

@RequestMapping("adicionaLoja")
    @ResponseBody
    public void adiciona(Loja loja) {
        System.out.println("adicionou - " + loja.getNome() + " X: " + loja.getxValue() + " Y: " + loja.getyValue());
    }

Complete JavaScript file:

$(document).ready(function(){


    $('.ImgMapa').popover({
        html: true,
        trigger: 'manual',
        title: "<form id='submitLoja' method='post'><input type='text' autocomplete='off' data-provide='typeahead' data-items='4' name='nome' data-source='[&quot;Alabama&quot;,&quot;Alaska&quot;,&quot;Arizona&quot;,&quot;Arkansas&quot;,&quot;California&quot;,&quot;Colorado&quot;,&quot;Connecticut&quot;,&quot;Delaware&quot;,&quot;Florida&quot;,&quot;Georgia&quot;,&quot;Hawaii&quot;,&quot;Idaho&quot;,&quot;Illinois&quot;,&quot;Indiana&quot;,&quot;Iowa&quot;,&quot;Kansas&quot;,&quot;Kentucky&quot;,&quot;Louisiana&quot;,&quot;Maine&quot;,&quot;Maryland&quot;,&quot;Massachusetts&quot;,&quot;Michigan&quot;,&quot;Minnesota&quot;,&quot;Mississippi&quot;,&quot;Missouri&quot;,&quot;Montana&quot;,&quot;Nebraska&quot;,&quot;Nevada&quot;,&quot;New Hampshire&quot;,&quot;New Jersey&quot;,&quot;New Mexico&quot;,&quot;New York&quot;,&quot;North Dakota&quot;,&quot;North Carolina&quot;,&quot;Ohio&quot;,&quot;Oklahoma&quot;,&quot;Oregon&quot;,&quot;Pennsylvania&quot;,&quot;Rhode Island&quot;,&quot;South Carolina&quot;,&quot;South Dakota&quot;,&quot;Tennessee&quot;,&quot;Texas&quot;,&quot;Utah&quot;,&quot;Vermont&quot;,&quot;Virginia&quot;,&quot;Washington&quot;,&quot;West Virginia&quot;,&quot;Wisconsin&quot;,&quot;Wyoming&quot;]'/>"
            + "<button type='submit' id='saveStore' class='btn' value='Save'>Save</button><input type=hidden name='xValue'/><input type=hidden name='yValue'/></form>"
    });

      $("#submitLoja").submit(function(event) { 
          alert("oie");

          return false;
      });


});

// Function to show popover on the image mouse click
$(function() {
    var xMousePos = 0;
    var yMousePos = 0;
    var lastScrolledLeft = 0;
    var lastScrolledTop = 0;

    // Manages the page scrolling and add X and Y when the page is scrolled
    $(window).scroll(function(event) {
        if (lastScrolledLeft != $(document).scrollLeft()) {
            xMousePos -= lastScrolledLeft;
            lastScrolledLeft = $(document).scrollLeft();
            xMousePos += lastScrolledLeft;
        }
        if (lastScrolledTop != $(document).scrollTop()) {
            yMousePos -= lastScrolledTop;
            lastScrolledTop = $(document).scrollTop();
            yMousePos += lastScrolledTop;
        }
    });

    function captureMousePosition(event) {
        xMousePos = event.pageX;
        yMousePos = event.pageY;
    }

    $('.ImgMapa').click(function(e) {
        $(this).popover('show');//this is used to correct the popover location on the first time click

        captureMousePosition(e);
        var offset = $(this).offset();
        var left = xMousePos;
        var top = yMousePos;
        var theHeight = $('.popover').height();

        //http://jsfiddle.net/2EBGE/1/
        $('.popover').css('cssText', 'left: ' + (left+10) + 'px !important; top: ' + (top-(theHeight/2)-10) + 'px !important');

        $(this).popover('show');

        $("input[name='xValue']").val(xMousePos);
        $("input[name='yValue']").val(yMousePos);
    });

    // Hide popover when ESCAPE key is pressed
    $(document).keydown(function(e) {
        if (e.keyCode === 27)
            $('.ImgMapa').popover('hide');
    });
});
Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61

1 Answers1

2

When you submit a form, the browser expects the server to send back an HTML page and displays the returned HTML. If you want to stay on the same page, you should submit the form using an AJAX request. See Submit form using AJAX and jQuery for example for how to do that.

You'll still have to tell Spring not to forward to adicionaLola.jsp. You can do that using the @ResponseBody annotation.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I could not do what you said.. I've edited my question with what I'm trying.. could you please check it? – Felipe Mosso Mar 10 '13 at 16:18
  • So, what happens? Is your Java method being called? Is there an error in the JavaScript console of your browser? Have you tried debugging the JS code to see if it's executed? – JB Nizet Mar 10 '13 at 16:20
  • Yeah, it seems the click event is not called. And my java method is not called either.. and don't know why, maybe because I've declared the form in the same .js file? – Felipe Mosso Mar 10 '13 at 16:21
  • 1
    The Javascript code above should only be executed when the document has been loaded. See http://api.jquery.com/ready/ – JB Nizet Mar 10 '13 at 16:27
  • hmm.. I'm afraid I'm not finding the form, because it's declared in another function outside the document.ready(function() {}) – Felipe Mosso Mar 10 '13 at 16:35
  • `$('#submitLoja')` returns the form. – JB Nizet Mar 10 '13 at 16:38
  • Yes.. it does.. I don't know what's happening, I tried to put an alert when the form is submited just to debug, and it doesn't work.. – Felipe Mosso Mar 10 '13 at 17:09
  • $("#submitLoja").submit(function(event) { alert("testing"); }); – Felipe Mosso Mar 10 '13 at 17:09
  • I can only guess. Please paste the complete JavaScript code in your question. – JB Nizet Mar 10 '13 at 17:11
  • 1
    OK. Now I understand. You're registering a submit event listener when the DOM is ready. But at this time, the form doesn't exist yet, since it's dynamically generated when a popover is displayed. Use `$(document).on('submit', '#submitLoja', function(event) {...})` to register a listener to current and future elements with the `'#submitLoja'` CSS selector. – JB Nizet Mar 10 '13 at 17:30
  • Now it works, when submit button is hit, my alert is shown! Thanks! But I'm still not receiving the data in the server side.. :( – Felipe Mosso Mar 10 '13 at 17:43
  • Now it works! I had to return false on the submit event! Thank you very much your help and your patience! I appreciate it! :)) – Felipe Mosso Mar 10 '13 at 18:04
  • You're welcome. Glad it helps. And congrats for finding the problems by yourself. – JB Nizet Mar 10 '13 at 18:06