【问题标题】:Why does my AJAX validation code not work properly?为什么我的 AJAX 验证码不能正常工作?
【发布时间】:2009-05-21 15:34:40
【问题描述】:

我创建了一个依赖于 PHP、AJAX 和 Javascript 的网站。
这里的问题是网站不稳定,这意味着有时验证工作正常,有时根本不工作。
验证代码是用 JavaScript 编写的。这是否意味着我们需要更多的特殊条件?

验证代码:

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

【问题讨论】:

  • 祝你好运。你最好重用一个框架。这段代码对于简单的表单验证来说太复杂了。

标签: php javascript ajax


【解决方案1】:

很难回答,因为我没有看到任何问题。 究竟什么不起作用?

但我可以假设一些问题:

  • 您确定 body.onload 运行了吗?有时不会...
  • 脚本不能跨浏览器
  • session_start() 应该会失败,因为之前有输出,并且无法发送标头。
  • 您说验证失败,但它在哪里被调用?

请提供更准确的问题和更清晰的无效代码示例(即我不确定,submitform() 的来源是否如此重要)。

【讨论】:

    【解决方案2】:

    验证应该在服务器端完成。当用户禁用他们的 JavaScript 时会发生什么?

    此外,该 JavaScript 块只会在页面加载时执行 PHP,而不是在每次用户验证时执行。

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多