Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何知道表格提交是否成功?_Java_Html_Servlets_Jquery - Fatal编程技术网

Java 我如何知道表格提交是否成功?

Java 我如何知道表格提交是否成功?,java,html,servlets,jquery,Java,Html,Servlets,Jquery,我有一个表格,基本上是上传一个文件。我提交了两次表格,一次没有多部分,第二次有多部分 <input type="button" tabindex="5" value="Create" id="btnS" class="btn" onClick="submitForm(this.form,'/test/upload'); return false;" /> //1st submission form.setAttribute("action",url_action); for

我有一个表格,基本上是上传一个文件。我提交了两次表格,一次没有多部分,第二次有多部分

<input  type="button"  tabindex="5"  value="Create" id="btnS" class="btn" onClick="submitForm(this.form,'/test/upload'); return false;" />


//1st submission
form.setAttribute("action",url_action);
form.setAttribute("method","post");
form.submit();

//2nd submission
form.setAttribute("action",url_action);
form.setAttribute("method","post");
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");
form.submit();
这是我的servlet部分。我要确定它是否是多部分的。如果未将结果类型存储到会话变量,则返回
提交的

现在,我想检查此“已提交””或类似内容,然后第二次提交表单

第二次表单提交:这里我将再次检查它的多部分,并检查会话变量和CRUD。(此IdentifyNow基本上是一种请求调制器)

下面是一个Javascript函数,用于提交表单两次,请查找micoxUpload()函数

/* standard small functions */
   function $m(quem){
 return document.getElementById(quem)
}
function remove(quem){
 quem.parentNode.removeChild(quem);
}
function addEvent(obj, evType, fn){
 // elcio.com.br/crossbrowser
    if (obj.addEventListener)
    obj.addEventListener(evType, fn, true)
if (obj.attachEvent)
    obj.attachEvent("on"+evType, fn)
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, fn );
  } else {
    obj.removeEventListener( type, fn, false ); }
} 
/* THE UPLOAD FUNCTION */
function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){

    /******
* micoxUpload - Submit a form to hidden iframe. Can be used to upload
* Use but dont remove my name. Creative Commons.
* Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
* Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
* Parametros:
* form - the form to submit or the ID
* url_action - url to submit the form. like action parameter of forms.
* id_element - element that will receive return of upload.
* html_show_loading - Text (or image) that will be show while loading
* html_error_http - Text (or image) that will be show if HTTP error.
*******/

 //testing if 'form' is a html object or a id string
 form = typeof(form)=="string"?$m(form):form;

 var erro="";
 if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does       not exists.\n";}
 else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}
 if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
 if(erro.length>0) {
  alert("Error in call micoxUpload:\n" + erro);
  return;
 }


 //creating the iframe
 var iframe = document.createElement("iframe");
 iframe.setAttribute("id","micox-temp");
 iframe.setAttribute("name","micox-temp");
 iframe.setAttribute("width","0");
 iframe.setAttribute("height","0");
 iframe.setAttribute("border","0");
 iframe.setAttribute("style","width: 0; height: 0; border: none;");

 //add to document
 form.parentNode.appendChild(iframe);
 window.frames['micox-temp'].name="micox-temp"; //ie sucks

 //add event
   var carregou = function() { 
   removeEvent( $m('micox-temp'),"load", carregou);
   var cross = "javascript: ";
   cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML; void(0); ";

   $m(id_element).innerHTML = html_error_http;
   $m('micox-temp').src = cross;
   //del the iframe
   setTimeout(function(){ remove($m('micox-temp'))}, 250);
  }
 addEvent( $m('micox-temp'),"load", carregou)

 //properties of form
 /*form.setAttribute("target","micox-temp");
 form.setAttribute("action",url_action);
 form.setAttribute("method","post");*/
 //form.submit();


 var postString = getPostString();
 var client; 
 if (window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
 client=new XMLHttpRequest();
 } else {                    // IE6, IE5
 client=new ActiveXObject("Microsoft.XMLHTTP");
 }

 //client.onreadystatechange=handler(form,url_action);
 client.open("POST",url_action,true);
 client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 client.setRequestHeader("Content-length", postString.length);
 client.setRequestHeader("Connection", "close");
 client.onreadystatechange   =   function(){

 if (client.readyState==4 && client.status==200){
     alert(client.responseText); //This gives back my text from servlet
     secondSend(form,url_action);
 }
 };
 client.send($postStr);

 alert("1st request send");
 //secondSend(form,url_action);

 //while loading
 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }
function getPostString()
 {
 $postStr=document.getElementsByTagName("confname");
 $postStr+=document.getElementsByTagName("returntype");
 return $postStr;
 }
     function secondSend(form,url_action)
 {

 form.setAttribute("target","micox-temp");
 form.setAttribute("action",url_action);
 form.setAttribute("method","post");
 form.setAttribute("enctype","multipart/form-data");
 form.setAttribute("encoding","multipart/form-data");
 form.submit();
 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }
 }
}
submit()没有返回值,因此您无法仅根据上面的代码检查提交结果

但是,通常的做法是使用Ajax并使用函数设置标志。这样,您就可以检查表单是否成功提交。更不用说,通过服务器回复,您可以进一步验证表单是否已正确传输到服务器:)

希望有帮助。干杯


下面的代码应该让您了解如何执行此操作:

function first_send(){
    // Local Variable
    var xmlhttp;            

        // Create Object
    if (window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {                    // IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

        // Set Function
    xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4 && xmlhttp.status==200){
        // (1) Check reply from server if request has been successfully 
        //     received
        // (2) Set flag / Fire-off next function to send
        // Example
        if (xmlhttp.responseText == "ReceiveSuccess"){
          secondSend();
        } else {
          // Error handling here
      } 
      } 
    }

        // Gets the first set of Data you want to send
    var postString = getPostString();  

          // Send
    xmlhttp.open("POST","form1.php",true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       xmlhttp.setRequestHeader("Content-length", postString.length);
       xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(postString);
}
您需要:

function getPostString(){
          // Collect data from your form here
}

function secondSend(){
      // You can create this function and post like above
      // or just do a direct send like your code did
}
希望有帮助(:


这段代码应该可以做到这一点,但一定要填写您正在使用的HTML表单!此外,如果需要,请将第一个表单放入提交中:

<script type="text/javascript">
  var postString = getPostString();
  var client     = new XMLHttpRequest();  // You shouldn't create it this way.

            // Open Connection and set the necessary
        client.open("POST",url_action,true);
        client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        client.setRequestHeader("Content-length", postString.length);
        client.setRequestHeader("Connection", "close");

            // Create function
        client.onreadystatechange   =   function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                if (xmlhttp.responseText == "Success") {
                    secondSend();
                } else {
                    alert('In Error');
                }
            }
        };

        client.send(postString);

  function getPostString()
  {
    // Get your postString data from your form here

        // Return your Data to post
        return $postStr;
  }

  function secondSend()
  {
        // Make sure you fill up your form before you post

    form.setAttribute("action",url_action);
    form.setAttribute("method","post");
    form.setAttribute("enctype","multipart/form-data");
    form.setAttribute("encoding","multipart/form-data");
    form.submit();
  }    
</script>

var postString=getPostString();
var client=new XMLHttpRequest();//您不应该这样创建它。
//打开连接并设置必要的
client.open(“POST”,url\u action,true);
setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
setRequestHeader(“内容长度”,postString.length);
setRequestHeader(“连接”、“关闭”);
//创建函数
client.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
if(xmlhttp.responseText==“成功”){
secondSend();
}否则{
警报(“错误”);
}
}
};
client.send(postString);
函数getPostString()
{
//从这里的表单获取postString数据
//将您的数据返回到post
返回$postStr;
}
函数secondSend()
{
//在你发帖之前一定要填好你的表格
form.setAttribute(“action”,url\u action);
form.setAttribute(“方法”、“帖子”);
setAttribute(“enctype”、“多部分/表单数据”);
setAttribute(“编码”、“多部分/表单数据”);
表单提交();
}    

除了@Vern的常规XMLHttpRequest之外,我还分享了ajax的实现方法

/*CALLING 1st SUBMIT*/
$(function() {
$("#submitButton").click(callme);

function callme() {      
    var form=document.forms["yourFormID"];

    $.ajax({
        type: "POST",
        url: "/upload",
        data: {resulttype: $('#resulttype').val()},
        async:false,
        complete: function(msg){

            micoxUpload(form,'/upload','postUploadInformation','Loading...','Crap! something went wrong'); return false;
        }
     });
}

});

/* THE UPLOAD FUNCTION */
function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){

/******
* micoxUpload - Submit a form to hidden iframe. Can be used to upload
* Use but dont remove my name. Creative Commons.
* Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
* Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
* Parametros:
* form - the form to submit or the ID
* url_action - url to submit the form. like action parameter of forms.
* id_element - element that will receive return of upload.
* html_show_loading - Text (or image) that will be show while loading
* html_error_http - Text (or image) that will be show if HTTP error.
*******/

 //testing if 'form' is a html object or a id string
 form = typeof(form)=="string"?$m(form):form;

 var erro="";
 if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does   not exists.\n";}
 else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}
 if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
 if(erro.length>0) {
  alert("Error in call micoxUpload:\n" + erro);
  return;
 }


 //creating the iframe
 var iframe = document.createElement("iframe");
 iframe.setAttribute("id","micox-temp");
 iframe.setAttribute("name","micox-temp");
 iframe.setAttribute("width","0");
 iframe.setAttribute("height","0");
 iframe.setAttribute("border","0");
 iframe.setAttribute("style","width: 0; height: 0; border: none;");

 //add to document
 form.parentNode.appendChild(iframe);
 window.frames['micox-temp'].name="micox-temp"; //ie sucks

 //add event
 var carregou = function() { 
   removeEvent( $m('micox-temp'),"load", carregou);
   var cross = "javascript: ";
   cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML;  void(0); ";

   $m(id_element).innerHTML = html_error_http;
$m('micox-temp').src = cross;
//del the iframe
setTimeout(function(){ remove($m('micox-temp'))}, 250);
}
 addEvent( $m('micox-temp'),"load", carregou)

 secondSend(form,url_action);

 //while loading
 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }

 function secondSend(form,url_action)
 {

 form.setAttribute("target","micox-temp");
 form.setAttribute("action",url_action);
 form.setAttribute("method","post");
 form.setAttribute("enctype","multipart/form-data");
 form.setAttribute("encoding","multipart/form-data");
 form.submit();

 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }
 }
}

@维姆:谢谢,这对我来说是一个很好的信息。你能指导我如何使用ajax来做到这一点吗?谢谢,我无法将所有代码压缩到注释中,所以请查看水平分隔线后的代码(:干杯!不用担心,是的,你是对的,你需要用从表格中收集的数据填写$postStr。当你这样做时,也许你可以回显它,这样你就可以查看你从表格中收集的数据是否正确。嗨,当你说“不起作用”时,这是否意味着xmlhttp.responseText将返回一个空字符串?您不应该使用from.submit()对于第一个表单,如果您想使用XMLHttp对象发送。另外,您使用的浏览器是什么?我发布的第二个代码是从您的原始代码修改而来的。XMLHttp对象不应该以这种方式创建。您好,您不发送任何内容,因为您的“client.send();”有一个空括号。它应该更改为“client.send”(postString);”。这样就可以了:)这完全没有意义。为什么要提交两次不同编码的表单?从一开始就使用正确的编码。@BalusC:我第一次发出两个请求来发送所有表单数据,因为在多部分中,整个内容都发生了更改,我想使用任何
请求.getParameter…
函数。所以第二次请求时,表单是由多部分提交的,这样我就可以从会话中获取表单数据并继续。您的建议是什么?我已经回答了您的另一个问题@BalusC:Ya我参考了您对
请求的答案。getParameterMap();
谢谢:)
<script type="text/javascript">
  var postString = getPostString();
  var client     = new XMLHttpRequest();  // You shouldn't create it this way.

            // Open Connection and set the necessary
        client.open("POST",url_action,true);
        client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        client.setRequestHeader("Content-length", postString.length);
        client.setRequestHeader("Connection", "close");

            // Create function
        client.onreadystatechange   =   function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                if (xmlhttp.responseText == "Success") {
                    secondSend();
                } else {
                    alert('In Error');
                }
            }
        };

        client.send(postString);

  function getPostString()
  {
    // Get your postString data from your form here

        // Return your Data to post
        return $postStr;
  }

  function secondSend()
  {
        // Make sure you fill up your form before you post

    form.setAttribute("action",url_action);
    form.setAttribute("method","post");
    form.setAttribute("enctype","multipart/form-data");
    form.setAttribute("encoding","multipart/form-data");
    form.submit();
  }    
</script>
/*CALLING 1st SUBMIT*/
$(function() {
$("#submitButton").click(callme);

function callme() {      
    var form=document.forms["yourFormID"];

    $.ajax({
        type: "POST",
        url: "/upload",
        data: {resulttype: $('#resulttype').val()},
        async:false,
        complete: function(msg){

            micoxUpload(form,'/upload','postUploadInformation','Loading...','Crap! something went wrong'); return false;
        }
     });
}

});

/* THE UPLOAD FUNCTION */
function micoxUpload(form,url_action,id_element,html_show_loading,html_error_http){

/******
* micoxUpload - Submit a form to hidden iframe. Can be used to upload
* Use but dont remove my name. Creative Commons.
* Versão: 1.0 - 03/03/2007 - Tested no FF2.0 IE6.0 e OP9.1
* Author: Micox - Náiron JCG - elmicoxcodes.blogspot.com - micoxjcg@yahoo.com.br
* Parametros:
* form - the form to submit or the ID
* url_action - url to submit the form. like action parameter of forms.
* id_element - element that will receive return of upload.
* html_show_loading - Text (or image) that will be show while loading
* html_error_http - Text (or image) that will be show if HTTP error.
*******/

 //testing if 'form' is a html object or a id string
 form = typeof(form)=="string"?$m(form):form;

 var erro="";
 if(form==null || typeof(form)=="undefined"){ erro += "The form of 1st parameter does   not exists.\n";}
 else if(form.nodeName.toLowerCase()!="form"){ erro += "The form of 1st parameter its not a form.\n";}
 if($m(id_element)==null){ erro += "The element of 3rd parameter does not exists.\n";}
 if(erro.length>0) {
  alert("Error in call micoxUpload:\n" + erro);
  return;
 }


 //creating the iframe
 var iframe = document.createElement("iframe");
 iframe.setAttribute("id","micox-temp");
 iframe.setAttribute("name","micox-temp");
 iframe.setAttribute("width","0");
 iframe.setAttribute("height","0");
 iframe.setAttribute("border","0");
 iframe.setAttribute("style","width: 0; height: 0; border: none;");

 //add to document
 form.parentNode.appendChild(iframe);
 window.frames['micox-temp'].name="micox-temp"; //ie sucks

 //add event
 var carregou = function() { 
   removeEvent( $m('micox-temp'),"load", carregou);
   var cross = "javascript: ";
   cross += "window.parent.$m('" + id_element + "').innerHTML = document.body.innerHTML;  void(0); ";

   $m(id_element).innerHTML = html_error_http;
$m('micox-temp').src = cross;
//del the iframe
setTimeout(function(){ remove($m('micox-temp'))}, 250);
}
 addEvent( $m('micox-temp'),"load", carregou)

 secondSend(form,url_action);

 //while loading
 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }

 function secondSend(form,url_action)
 {

 form.setAttribute("target","micox-temp");
 form.setAttribute("action",url_action);
 form.setAttribute("method","post");
 form.setAttribute("enctype","multipart/form-data");
 form.setAttribute("encoding","multipart/form-data");
 form.submit();

 if(html_show_loading.length > 0){
  $m(id_element).innerHTML = html_show_loading;
 }
 }
}