验证Ajax加载的表单

时间:2014-07-13 23:33:15

标签: php jquery ajax validation

我试过到处搜索(过去2小时),我尝试了所有这些不同的解决方案,到目前为止我没有任何工作。所以我想我会来这里问天才。因为我的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>

我很确定它是可能的,但我很遗憾如何让它实际验证信息。 如果有人会这么善良,我将不胜感激。

0 个答案:

没有答案