Guest User

Untitled

a guest
Sep 1st, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.30 KB | None | 0 0
  1. How do I know if form submission is successful?
  2. <input type="button" tabindex="5" value="Create" id="btnS" class="btn" onClick="submitForm(this.form,'/test/upload'); return false;" />
  3.  
  4.  
  5. //1st submission
  6. form.setAttribute("action",url_action);
  7. form.setAttribute("method","post");
  8. form.submit();
  9.  
  10. //2nd submission
  11. form.setAttribute("action",url_action);
  12. form.setAttribute("method","post");
  13. form.setAttribute("enctype","multipart/form-data");
  14. form.setAttribute("encoding","multipart/form-data");
  15. form.submit();
  16.  
  17. var postString = getPostString();
  18. var client=new XMLHttpRequest();
  19. client.onreadystatechange=handler(form,url_action);
  20. client.open("POST",url_action,true);
  21. client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  22. client.setRequestHeader("Content-length", postString.length);
  23. client.setRequestHeader("Connection", "close");
  24. client.send(postString);
  25.  
  26. function handler(form,url_action)
  27. {
  28. if(this.readyState == 4 && this.status == 200) {
  29. //Here submitted is the text that I receive from the servlet If 1st submit is successful
  30. if (xmlhttp.responseText == "submitted"){
  31. secondSend(form,url_action);
  32. } else {
  33. alert("Not good!");
  34. }
  35. }
  36. }
  37. function getPostString()
  38. {
  39.  
  40. }
  41. function secondSend(form,url_action)
  42. {
  43. form.setAttribute("action",url_action);
  44. form.setAttribute("method","post");
  45. form.setAttribute("enctype","multipart/form-data");
  46. form.setAttribute("encoding","multipart/form-data");
  47. form.submit();
  48. }
  49.  
  50. public String identifyNow()throws ServletException, java.io.IOException
  51. {
  52. UploadXmlAgent uploadAgent;
  53. boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  54.  
  55. System.out.println("nn*********************************nisMultipart: "+isMultipart);
  56.  
  57. if(isMultipart)
  58. {
  59. session=request.getSession(false);
  60.  
  61. System.out.println("nThis is multipart and isNew"+session.isNew());
  62. if(session!=null)
  63. {
  64. System.out.println("ninside session");
  65. requestType=session.getAttribute("resultType").toString();
  66. //Identified based on requestType, instantiate appropriate Handler
  67. //session.invalidate();
  68. if(requestType.equals("Create"))
  69. {
  70. uploadAgent=new UploadXmlAgent(realPath,request,paramMap);
  71. uploadAgent.retrieveXml();
  72. return uploadAgent.uploadXml();
  73. }
  74. else if(requestType.equals("Update"))
  75. {
  76.  
  77. }
  78. else if(requestType.equals("Delete"))
  79. {
  80.  
  81. }
  82. }
  83. else
  84. {
  85. return "Session is null";
  86. }
  87.  
  88. }
  89. else
  90. {
  91. System.out.println("nNot a multipart");
  92. paramMap=request.getParameterMap();
  93. if (paramMap == null)
  94. throw new ServletException(
  95. "getParameterMap returned null in: " + getClass().getName());
  96.  
  97. iterator=paramMap.entrySet().iterator();
  98. System.out.println("n"+paramMap.size());
  99. while(iterator.hasNext())
  100. {
  101. Map.Entry me=(Map.Entry)iterator.next();
  102. if(me.getKey().equals("resultType"))
  103. {
  104. String[] arr=(String[])me.getValue();
  105. requestType=arr[0];
  106. System.out.println("Inside returntype: "+requestType);
  107. }
  108. }
  109. session=request.getSession(true);
  110. session.setAttribute("returntype", requestType);
  111. System.out.println("Session.isNew="+session.isNew());
  112. return "submitted";
  113. }
  114. return "noCreate";
  115. }
  116.  
  117. /* standard small functions */
  118. function $m(quem){
  119. return document.getElementById(quem)
  120. }
  121. function remove(quem){
  122. quem.parentNode.removeChild(quem);
  123. }
  124. function addEvent(obj, evType, fn){
  125. // elcio.com.br/crossbrowser
  126. if (obj.addEventListener)
  127. obj.addEventListener(evType, fn, true)
  128. if (obj.attachEvent)
  129. obj.attachEvent("on"+evType, fn)
  130. }
  131. function removeEvent( obj, type, fn ) {
  132. if ( obj.detachEvent ) {
  133. obj.detachEvent( 'on'+type, fn );
  134. } else {
  135. obj.removeEventListener( type, fn, false ); }
  136. }
  137. /* THE UPLOAD FUNCTION */
  138. function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){
  139.  
  140. /******
  141. * micoxUpload - Submit a form to hidden iframe. Can be used to upload
  142. * Use but dont remove my name. Creative Commons.
  143. * Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
  144. * Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
  145. * Parametros:
  146. * form - the form to submit or the ID
  147. * url_action - url to submit the form. like action parameter of forms.
  148. * id_element - element that will receive return of upload.
  149. * html_show_loading - Text (or image) that will be show while loading
  150. * html_error_http - Text (or image) that will be show if HTTP error.
  151. *******/
  152.  
  153. //testing if 'form' is a html object or a id string
  154. form = typeof(form)=="string"?$m(form):form;
  155.  
  156. var erro="";
  157. if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.n";}
  158. else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.n";}
  159. if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.n";}
  160. if(erro.length>0) {
  161. alert("Error in call micoxUpload:n" + erro);
  162. return;
  163. }
  164.  
  165.  
  166. //creating the iframe
  167. var iframe = document.createElement("iframe");
  168. iframe.setAttribute("id","micox-temp");
  169. iframe.setAttribute("name","micox-temp");
  170. iframe.setAttribute("width","0");
  171. iframe.setAttribute("height","0");
  172. iframe.setAttribute("border","0");
  173. iframe.setAttribute("style","width: 0; height: 0; border: none;");
  174.  
  175. //add to document
  176. form.parentNode.appendChild(iframe);
  177. window.frames['micox-temp'].name="micox-temp"; //ie sucks
  178.  
  179. //add event
  180. var carregou = function() {
  181. removeEvent( $m('micox-temp'),"load", carregou);
  182. var cross = "javascript: ";
  183. cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";
  184.  
  185. $m(id_element).innerHTML = html_error_http;
  186. $m('micox-temp').src = cross;
  187. //del the iframe
  188. setTimeout(function(){ remove($m('micox-temp'))}, 250);
  189. }
  190. addEvent( $m('micox-temp'),"load", carregou)
  191.  
  192. //properties of form
  193. /*form.setAttribute("target","micox-temp");
  194. form.setAttribute("action",url_action);
  195. form.setAttribute("method","post");*/
  196. //form.submit();
  197.  
  198.  
  199. var postString = getPostString();
  200. var client;
  201. if (window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
  202. client=new XMLHttpRequest();
  203. } else { // IE6, IE5
  204. client=new ActiveXObject("Microsoft.XMLHTTP");
  205. }
  206.  
  207. //client.onreadystatechange=handler(form,url_action);
  208. client.open("POST",url_action,true);
  209. client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  210. client.setRequestHeader("Content-length", postString.length);
  211. client.setRequestHeader("Connection", "close");
  212. client.onreadystatechange = function(){
  213.  
  214. if (client.readyState==4 && client.status==200){
  215. alert(client.responseText); //This gives back my text from servlet
  216. secondSend(form,url_action);
  217. }
  218. };
  219. client.send($postStr);
  220.  
  221. alert("1st request send");
  222. //secondSend(form,url_action);
  223.  
  224. //while loading
  225. if(html_show_loading.length > 0){
  226. $m(id_element).innerHTML = html_show_loading;
  227. }
  228. function getPostString()
  229. {
  230. $postStr=document.getElementsByTagName("confname");
  231. $postStr+=document.getElementsByTagName("returntype");
  232. return $postStr;
  233. }
  234. function secondSend(form,url_action)
  235. {
  236.  
  237. form.setAttribute("target","micox-temp");
  238. form.setAttribute("action",url_action);
  239. form.setAttribute("method","post");
  240. form.setAttribute("enctype","multipart/form-data");
  241. form.setAttribute("encoding","multipart/form-data");
  242. form.submit();
  243. if(html_show_loading.length > 0){
  244. $m(id_element).innerHTML = html_show_loading;
  245. }
  246. }
  247. }
  248.  
  249. function first_send(){
  250. // Local Variable
  251. var xmlhttp;
  252.  
  253. // Create Object
  254. if (window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
  255. xmlhttp=new XMLHttpRequest();
  256. } else { // IE6, IE5
  257. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  258. }
  259.  
  260. // Set Function
  261. xmlhttp.onreadystatechange=function(){
  262. if (xmlhttp.readyState==4 && xmlhttp.status==200){
  263. // (1) Check reply from server if request has been successfully
  264. // received
  265. // (2) Set flag / Fire-off next function to send
  266. // Example
  267. if (xmlhttp.responseText == "ReceiveSuccess"){
  268. secondSend();
  269. } else {
  270. // Error handling here
  271. }
  272. }
  273. }
  274.  
  275. // Gets the first set of Data you want to send
  276. var postString = getPostString();
  277.  
  278. // Send
  279. xmlhttp.open("POST","form1.php",true);
  280. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  281. xmlhttp.setRequestHeader("Content-length", postString.length);
  282. xmlhttp.setRequestHeader("Connection", "close");
  283. xmlhttp.send(postString);
  284. }
  285.  
  286. function getPostString(){
  287. // Collect data from your form here
  288. }
  289.  
  290. function secondSend(){
  291. // You can create this function and post like above
  292. // or just do a direct send like your code did
  293. }
  294.  
  295. <script type="text/javascript">
  296. var postString = getPostString();
  297. var client = new XMLHttpRequest(); // You shouldn't create it this way.
  298.  
  299. // Open Connection and set the necessary
  300. client.open("POST",url_action,true);
  301. client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  302. client.setRequestHeader("Content-length", postString.length);
  303. client.setRequestHeader("Connection", "close");
  304.  
  305. // Create function
  306. client.onreadystatechange = function(){
  307. if (xmlhttp.readyState==4 && xmlhttp.status==200){
  308. if (xmlhttp.responseText == "Success") {
  309. secondSend();
  310. } else {
  311. alert('In Error');
  312. }
  313. }
  314. };
  315.  
  316. client.send(postString);
  317.  
  318. function getPostString()
  319. {
  320. // Get your postString data from your form here
  321.  
  322. // Return your Data to post
  323. return $postStr;
  324. }
  325.  
  326. function secondSend()
  327. {
  328. // Make sure you fill up your form before you post
  329.  
  330. form.setAttribute("action",url_action);
  331. form.setAttribute("method","post");
  332. form.setAttribute("enctype","multipart/form-data");
  333. form.setAttribute("encoding","multipart/form-data");
  334. form.submit();
  335. }
  336. </script>
  337.  
  338. /*CALLING 1st SUBMIT*/
  339. $(function() {
  340. $("#submitButton").click(callme);
  341.  
  342. function callme() {
  343. var form=document.forms["yourFormID"];
  344.  
  345. $.ajax({
  346. type: "POST",
  347. url: "/upload",
  348. data: {resulttype: $('#resulttype').val()},
  349. async:false,
  350. complete: function(msg){
  351.  
  352. micoxUpload(form,'/upload','postUploadInformation','Loading...','Crap! something went wrong'); return false;
  353. }
  354. });
  355. }
  356.  
  357. });
  358.  
  359. /* THE UPLOAD FUNCTION */
  360. function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){
  361.  
  362. /******
  363. * micoxUpload - Submit a form to hidden iframe. Can be used to upload
  364. * Use but dont remove my name. Creative Commons.
  365. * Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
  366. * Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
  367. * Parametros:
  368. * form - the form to submit or the ID
  369. * url_action - url to submit the form. like action parameter of forms.
  370. * id_element - element that will receive return of upload.
  371. * html_show_loading - Text (or image) that will be show while loading
  372. * html_error_http - Text (or image) that will be show if HTTP error.
  373. *******/
  374.  
  375. //testing if 'form' is a html object or a id string
  376. form = typeof(form)=="string"?$m(form):form;
  377.  
  378. var erro="";
  379. if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does not exists.n";}
  380. else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.n";}
  381. if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.n";}
  382. if(erro.length>0) {
  383. alert("Error in call micoxUpload:n" + erro);
  384. return;
  385. }
  386.  
  387.  
  388. //creating the iframe
  389. var iframe = document.createElement("iframe");
  390. iframe.setAttribute("id","micox-temp");
  391. iframe.setAttribute("name","micox-temp");
  392. iframe.setAttribute("width","0");
  393. iframe.setAttribute("height","0");
  394. iframe.setAttribute("border","0");
  395. iframe.setAttribute("style","width: 0; height: 0; border: none;");
  396.  
  397. //add to document
  398. form.parentNode.appendChild(iframe);
  399. window.frames['micox-temp'].name="micox-temp"; //ie sucks
  400.  
  401. //add event
  402. var carregou = function() {
  403. removeEvent( $m('micox-temp'),"load", carregou);
  404. var cross = "javascript: ";
  405. cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";
  406.  
  407. $m(id_element).innerHTML = html_error_http;
  408. $m('micox-temp').src = cross;
  409. //del the iframe
  410. setTimeout(function(){ remove($m('micox-temp'))}, 250);
  411. }
  412. addEvent( $m('micox-temp'),"load", carregou)
  413.  
  414. secondSend(form,url_action);
  415.  
  416. //while loading
  417. if(html_show_loading.length > 0){
  418. $m(id_element).innerHTML = html_show_loading;
  419. }
  420.  
  421. function secondSend(form,url_action)
  422. {
  423.  
  424. form.setAttribute("target","micox-temp");
  425. form.setAttribute("action",url_action);
  426. form.setAttribute("method","post");
  427. form.setAttribute("enctype","multipart/form-data");
  428. form.setAttribute("encoding","multipart/form-data");
  429. form.submit();
  430.  
  431. if(html_show_loading.length > 0){
  432. $m(id_element).innerHTML = html_show_loading;
  433. }
  434. }
  435. }
Add Comment
Please, Sign In to add comment