Php 验证Ajax加载的表单

Php 验证Ajax加载的表单,php,jquery,ajax,validation,Php,Jquery,Ajax,Validation,在过去的两个小时里,我尝试了搜索所有地方,我尝试了所有这些不同的解决方案,但到目前为止,没有任何效果。所以我想我会来这里问问那些天才。因为我的Jquery不是很好。我知道它与id有关,并且在脚本启动时没有加载,但我不知道如何着手修复此问题。 我将要展示的代码肯定不是最好的。我做这件事才几个月。所以请对我耐心点 下面是main.php文件: echo "<div class=\"main_menu_buttons\">"; echo "<table id=\"nav\">"

在过去的两个小时里,我尝试了搜索所有地方,我尝试了所有这些不同的解决方案,但到目前为止,没有任何效果。所以我想我会来这里问问那些天才。因为我的Jquery不是很好。我知道它与id有关,并且在脚本启动时没有加载,但我不知道如何着手修复此问题。 我将要展示的代码肯定不是最好的。我做这件事才几个月。所以请对我耐心点

下面是main.php文件:

echo "<div class=\"main_menu_buttons\">";
echo "<table id=\"nav\">";
echo "<tr><td>";
echo "<ul id=\"nav\">";
echo "<li><button class=\"pm_button\" onclick=\"masspm('new', 'menu')\">New</button></li>";
echo "<li><button class=\"pm_button\" onclick=\"masspm('edit', 'menu')\">Edit</button></li>";
echo "<li><button class=\"pm_button\" onclick=\"masspm('view', 'menu')\">View</button></li>";
echo "</ul>";
echo "</td></tr>";
echo "</table>";
echo "</div>";
echo "<span id=\"wait_1\" style=\"display: none;\">";
echo "<img alt=\"Please Wait\" src=\"/opserv/regimental/ajax-loader.gif\"/>";
echo "</span>";
echo "<span id=\"result_1\">";
// This is the default load screen. This will change every time we click a button.  
echo "<div class=\"editor\">";
echo "<br><span class=\"header\">List of your PM groups:</span><br>";
$resultVB = $mysqliVB->query("my query");
if ($resultVB->num_rows > 0){
    while ($rowVB = $resultVB->fetch_assoc()){
        echo " - ".$rowVB['name']."<br>";
    }
}else{
    echo " - You have no groups.<br>";
}
echo "</div>";
// End of Default Load screen   
echo "</span>";
-------
//Here is the script for loading ajax
<script type="text/javascript">
function masspm(type, arg, global) {
$('#wait_1').hide();
$('#wait_1').show();
$('#result_1').hide();
    $.get("/forum/opserv/ajaxselect.php", {
    func: type,
    drop_var: arg
    }, function(response){
    $('#result_1').fadeOut();
    setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
});
    return false;
}

function finishAjax(id, response) {
$('#wait_1').hide();
$('#result_1').show();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
</script>
我以前在这个文件中有验证文件。但是被告知不要把它放在这个文件中,并在我的名为ajax的文件中使用它,这个文件是ajaxselect.php 现在是ajaxselect.php

echo "<div class=\"editor\">";
echo "<br><span class=\"header\">Create New PM Group:</span><br>";
echo "<form name=\"new\" action=\"/forum/opserv/addon_pmgroup.php?op=addpmgroup\" method=\"post\" onSubmit=\"return validate();\">";
echo "Group Name: <input type=\"text\" name=\"name\"><br><br>";
echo "<table border=1 class=\"pmTable\"><tbody><tr>";
$i = 0;
while ($rowVB = $resultVB->fetch_assoc()){
    if ($i > $rows){
        echo "</td><td>";
        $i = 0;
    }elseif ($i == 0){
        echo "<td>";
    }
echo "<input type=\"checkbox\" id=\"member".$rowVB['userid']."\" name=\"members[]\" class=\"aar-checkbox\" value=\"".$rowVB['userid']."\"/><label for=\"member".$rowVB['userid']."\" class=\"aar-label\"></label> ".$rowVB['username']."<br>";
    $i++;
}
echo "</tbody></table>";
echo "<br><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\"/>";
echo "</form>";
echo "</div>";
然后这里有验证脚本,它也在ajaxselect.php中

<script type="text/javascript">
function validate(){
msg = ""
if(document.new.name.value.length < 3){
    msg += "Please enter a valid name\n";
}

if ($("#new input:checkbox:checked").length == 0) {
    msg += "Please select a member\n";
}

if ($("#new input:checkbox:checked").length > 30 ) {
    msg += "You many only have 30 members per group\n";
}

if(msg != ""){
    alert(msg);
    return false;
}else{
    return true;
}
}
</script>
我很确定这是可能的,但我不知道如何让它真正验证信息。
如果有人愿意帮忙,我将不胜感激。

您能解释一下问题所在吗?验证不是在开火吗?您是否检查了浏览器控制台的Javascript错误?没有错误:。我已经读到,更改live上的submit按钮调用验证的方式与此有关,但我不知道如何操作。当我单击submit按钮时。表单执行时就像在没有验证的情况下呼喊一样,然后继续。除了我试图阻止用户不输入任何名称外,其他一切都正常。它仍然只是提交。我认为问题在于验证函数中的第一个if条件。document.new.name.value.length。您可以更改它并使用jQuery选择元素吗,例如:$IdOfElement.val.length好的,我算出了。。最后我不得不将上面的代码从onsubmit=returnvalidate改为onsubmit;to onsubmit=返回validatethis;在验证函数中。我必须将函数validate{更改为函数validateform{,并且ifdocument.new.name.value.length<3{行必须更改为ifform.name.value.length<3{我还必须将验证脚本移回main.php,这与我被告知的不同。