Neden AJAX doğrulama kodu düzgün çalışmıyor?

2 Cevap php

I made a website that depends on PHP, AJAX and Javascript.
The problem here is that the website is unstable which means sometimes the validation is working and some times it is not working at all.
The validation code is written in JavaScript. Does this mean that we need more special conditions?

Doğrulama için kod:

<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>

2 Cevap

Doğrulama sunucu tarafında yapılmalıdır. Bir kullanım onların JavaScript devre dışı ne olur?

Sayfa yükler değil, her kullanıcının doğrular Ayrıca, JavaScript bu bloğu sadece PHP çalıştıracaktır.

Very hard to answer because I don't see a question. What exactly does not work?

Ama bazı sorunları düşüneceğiniz:

  • Emin misiniz, bu body.onload çalışır? Bazen değil ...
  • betik crossbrowser değil
  • Orada bir çıkış öncesi ve başlıkları gönderilebilir olamaz çünkü session_start () başarısız.
  • Doğrulama başarısız olduğunu söyledi, ama nereye denir ediliyor?

, daha kesin soru ve çalışmıyor kod daha net örnek verin (yani emin değilim, submitform o kaynağı () yani nessecary olduğunu).