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>


