Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery - .submit() function not working inside ajax() function
    primarykey
    data
    text
    <p>I am new to ajax. The requirement is fairly simple. When the submit button is pressed, the javascript would do a asynchronous request, which the sever-side would validate the user input. If the input is alright, the form would do standard submit, otherwise have a error message.</p> <p>So, here is my form:</p> <pre><code>&lt;form id="form" action="dosomething.jsp" method="post"&gt; &lt;input type="text" name="name"&gt; &lt;input type="text" name="email"&gt; &lt;input id="submit" type="submit" value="Go"&gt; &lt;/form&gt; </code></pre> <p>here is my javascript:</p> <pre><code>$(document).ready(function() { $("#form").submit(function(event) { event.preventDefault(); var form = $(this); var button = $("#submit"); button.attr("disabled", "disabled"); $.ajax({ url: "ValidateServlet", type: "post", data: form.serialize(), success: function(msg) { if(msg== ""){ form.unbind("submit"); form.submit(); } else{ alert(msg); button.removeAttr("disabled"); } } }); }); }); </code></pre> <p>The code works fine, except one function <code>form.submit();</code> not working. I try this outside the ajax() function, it would submit, so I think there is problem about the scope, but I can't fix that.</p> <p>Let me clarify the <code>form.unbind();</code>. Let say I delete the line with <code>form.submit();</code>, in other words, there is only <code>form.unbind();</code> for the success case <code>(if(msg = ""))</code>. When I press the submit button first time, the form would do a asynchronous request to validate as normal. If the input is correct, (namely <code>msg = ""</code>), when I press the submit button again, it would do the standard submit, since I have unbinded the submit event. Therefore, the problem is not on that <code>form.unbind();</code>, at least in my concern.</p> <p>The servlet code:</p> <pre><code>public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ PrintWriter out = response.getWriter(); String msg = ""; String name = request.getParameter("name"); String email = request.getParameter("email"); if("".equals(name)){ msg += "&lt;li&gt;Please enter name&lt;/li&gt;" ; } if("".equals(email)){ msg += "&lt;li&gt;Please enter email&lt;/li&gt;" ; } if(title.length() &gt; 20 ){ msg += "&lt;li&gt;Name should be lesser than 20 character.&lt;/li&gt;" ; } if(email.length() &gt; 30){ msg += "&lt;li&gt;Title should be lesser than 30 character.&lt;/li&gt;" ; } if(! "".equals(msg)){ msg = "&lt;p&gt;Error :&lt;/p&gt;&lt;ul&gt;" + msg; msg += "&lt;/ul&gt;"; out.print(msg); } out.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload