简体   繁体   English

Ajax表单验证问题

[英]Ajax Form Validation Problem

I've got my form validation almost working but I can't seem to figure this last problem out. 我已经进行了表单验证,但似乎无法解决最后一个问题。

I'm trying to send back error messages and position them in their own div next to their relevant form fields. 我正在尝试发回错误消息,并将它们放置在其自己的div中相关表单字段旁边。

I've got an error message coming back in its own div, but when I try to send multiple messages back nothing happens, any thoughts? 我在自己的div中返回了一条错误消息,但是当我尝试向后发送多条消息时,没有任何反应,有什么想法吗?

Here's most of my ajax 这是我大部分的ajax

function regForm(thisform) { //Reg user form check
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
 alert ("Browser does not support HTTP Request");
 return;
 }
 var formdata = "";
 formdata = "lname=" + thisform.elements['lname'].value + "&fname=" + thisform.elements['fname'].value + "&email=" + thisform.elements['email'].value + "&username=" + thisform.elements['username'].value + "&pass=" + thisform.elements['pass'].value + "&pass2=" + thisform.elements['pass2'].value; //send the data through the url - frist is the name i want to call it... second grad the content from the form using its id
    xmlHttp.onreadystatechange=formSubmitted;
    xmlHttp.open("POST", "adduser.php",true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", formdata.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(formdata);
    return false;
}

function formSubmitted() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
    xmlDoc=xmlHttp.responseXML;
//document.getElementById("feedback").innerHTML = xmlHttp.responseText;
document.getElementById("feedback1").innerHTML= xmlDoc.getElementsByTagName("lname")[0].childNodes[0].nodeValue;
document.getElementById("feedback2").innerHTML= xmlDoc.getElementsByTagName("fname")[0].childNodes[0].nodeValue;

    }
}

and here is my simple adduser.php page so far 这是到目前为止我简单的adduser.php页面

<?php
header('Content-Type: text/xml');

$lname = mysql_real_escape_string($_POST['lname']);
$fname = mysql_real_escape_string($_POST['fname']);

if($lname == NULL) {
echo "<lname>NEED TO FILL</lname>";
}

//if($fname == NULL) {
//echo "<fname>NEED TO FILL</fname>";
//}
else {
    echo "<lname> </lname>";
    //echo "<fname> </fname>";
}
?>

As you can see I've got the fname information commented out right now and my messaging is working for lname but as soon as I uncomment the fname stuff in hopes to send a message for both lname and fname nothing happens I don't understand why. 如您所见,我现在已经注释掉了fname信息,并且我的消息传递适用于lname,但是一旦我取消注释fname东西,希望同时发送lname和fname消息,我什么都不知道,我不明白为什么。

Any insight would be a big help! 任何见识都会有很大帮助! Thanks. 谢谢。

I don't entirely understand what you mean by "coming back in its own div" but you are aware that an element ID must be unique in the document? 我不完全理解“返回自己的div”的意思,但是您知道元素ID在文档中必须唯一吗? There doesn't happen to happen that you get two DIVs of the same ID if the error DIV comes back? 如果错误DIV再次出现,您不会得到两个具有相同ID的DIV吗?

Try having only a single echo statement at the very end of the script (so have a variable to concatenate all your error messages and only echo it at the very end). 尝试在脚本的末尾仅包含一个echo语句(因此,请使用一个变量来连接所有错误消息,并仅在末尾对其进行回显)。

Also, I would very strongly recommend using a JS library (like jQuery) for all your ajax needs - will make your life a whole lot easier 另外,我强烈建议您使用JS库(如jQuery)来满足您的所有ajax需求-这样会使您的生活更加轻松

ok I've got it figured out and like always it was stupid why it wasn't working. 好的,我已经弄清楚了,就像往常一样,为什么它不起作用是愚蠢的。

in my adduser.php page I just need to wrap the error message in a xml tag. 在我的adduser.php页面中,我只需要将错误消息包装在xml标记中。

echo "<errors>";

//error content

echo "</errors>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM