栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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-urlenpred");       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 pre 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-urlenpred");        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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号