2009-05-21 91 views
-2

我製作了一個依賴於PHP,AJAX和Javascript的網站。
這裏的問題是,網站是不穩定的,這意味着有時驗證工作,有時它根本不工作。
驗證碼是用JavaScript編寫的。這是否意味着我們需要更多特殊條件?爲什麼我的AJAX驗證代碼無法正常工作?

用於驗證的代碼:

<script language=Javascript> 
function Inint_AJAX() { 
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE 
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE 
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript 
alert("XMLHttpRequest not supported"); 
return null; 
}; 

function dochange(src, val) 
{ 
var req = Inint_AJAX(); 
req.onreadystatechange = function() 
{ 
     if (req.readyState==4) 
     { 
      if (req.status==200) 
      {  
       document.getElementById(src).innerHTML=req.responseText; 
      } 
     } 
}; 
req.open("GET", "traditional_locate.php?data="+src+"&val="+val); 
    req.send(null); 
} 

window.onLoad=dochange('cities', -1); 


function submitform() 
{ 
if(document.form1.onsubmit()) 
{ 
    document.form1.submit(); 
} 
} 

function validate() 
{ 
    var p_o_box=(document.form1.p_o_box.value); 
    var owner_name=(document.form1.owner_name.value); 
    var cities=(document.form1.cities.value); 
    var post_office=(document.form1.post_office.value); 


     if(cities == 0) 
     { 
      alert("Please Choose city"); 
      document.form1.cities.focus(); 
      return false; 
     } 

     else if(post_office == 0) 
      { 
      alert("Please Choose post office"); 
      document.form1.post_office.focus(); 

      return false; 
      } 

      else if (p_o_box=="") 
       { 
        alert ("Please Write P.O.Box"); 
        document.form1.p_o_box.focus(); 
         return false;      
       } 

       else if(owner_name=="") 
         { 
         alert("Please Write Owner Name"); 
         return false; 
         } 

        else if(p_o_box!="") 
         { 
           var a=new Array; 

          <?php 
           session_start();          
           $zipinfo=$_SESSION[zip_code];         
           $conn=mysql_connect('localhost','root',''); 
           mysql_select_db('post_db',$conn); 
           $query="select p_o_box from p_o_box where zip_code='$zipinfo' "; 
           $result = mysql_query ($query , $conn) or die (mysql_error()); 
           $rows=mysql_num_rows($result); 
           $n = array(); 
           for($i=0;$i<$rows;$i++) 
           { 
            $data=mysql_fetch_row($result); 
            $n[] = $data[0]; 
           } 

           for($i=0;$i<count($n); $i++) 
           { 
            echo "a[$i]='".$n[$i]."';\n"; 
           }   
           ?> 

           var ss=0; 
           for(i=0;i<a.length;i++) 

           if(a[i]==p_o_box) 
            { 
             var ss=1; 
            } 

           if (ss==0) 
            { 
             alert('not correct p.o. box'); 
             document.form1.p_o_box.focus(); 
              return false; 
            } 
           else 
            { return true;} 

         }      


    return true;   
} 

</script> 
+0

祝你好運得到答案。你最好重新使用一個框架。這段代碼對於簡單的表單驗證來說太複雜了。 – 2009-05-21 19:19:25

回答

0

驗證應該在服務器端完成。使用禁用JavaScript時會發生什麼?

此外,該JavaScript塊只會在頁面加載時執行PHP,而不是每次用戶驗證。

1

很難回答,因爲我沒有看到問題。 什麼不工作?

但我可以想一些問題:

  • 確定,即body.onload運行?有時候不會...
  • 腳本不是交叉瀏覽器
  • session_start()應該失敗,因爲之前有一個輸出並且不能發送標題。
  • 你說驗證失敗了,但它在哪裏被調用?

請提供更準確的問題和更清晰的代碼示例,它不起作用(即我不確定,submitform()的源是如此詭祕)。

相關問題