Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单上的jQuery验证无效_Php_Jquery_Html_Forms_Validation - Fatal编程技术网

Php 表单上的jQuery验证无效

Php 表单上的jQuery验证无效,php,jquery,html,forms,validation,Php,Jquery,Html,Forms,Validation,我是jQuery新手,我正在尝试使用它来验证登录表单。但是,验证脚本不会激活:它只是坐在那里不做任何事情,同时禁用提交按钮。我认为这会干扰在同一表单上运行的另一个脚本,该脚本允许用户在同一div中的不同表单之间切换 以下是html: <div class="box"> <?php if (isset($_SESSION['login'])){ ?> <h2>Welcome back, <?php echo $_SESSION['u

我是jQuery新手,我正在尝试使用它来验证登录表单。但是,验证脚本不会激活:它只是坐在那里不做任何事情,同时禁用提交按钮。我认为这会干扰在同一表单上运行的另一个脚本,该脚本允许用户在同一div中的不同表单之间切换

以下是html:

<div class="box">
    <?php if (isset($_SESSION['login'])){ ?>
        <h2>Welcome back, <?php echo $_SESSION['username']; ?></h2>
        <div><p><a href="logout.php">Click here to log outt</a></p></div>
    <?php } else { ?>
        <div id="form_wrapper" class="form_wrapper">
            <div class="register"> <!-- First form -->
            <form id="registrationform">
        <h2>Register</h2>
        <div class="box">
           <div>
               <label>Name:</label>
               <input name="nomeagenzia" type="text" required />
               </div>
           <!-- Some other input fields -->
               <input type="submit" value="Register" />
           <a href="#" rel="login" class="linkform">Already a user? Login here</a>
        </div>
        </form>
        </div>

        <div class="login active"> <!-- Second form, the one I'm validating-->
        <form id="loginform" action="index.php" method="POST">
        <h2>Area Agenzie</h2>
        <div class="box">
        <div>
            <label>Username:</label>
        <input name="username" type="text" />
        </div>
        <div style="position:relative;">
            <label>Password:</label>
                <a href="#" rel="forgot_password" class="linkform" style="right:0; position:absolute; margin-right:3px;">Forgot your password?</a>
            <input name="password" type="password" />
        </div>
    <input name="submit" type="submit" value="Login" />
        <a href="#" rel="register" class="linkform">Register here!</a>
        </div>
        </form>
        </div>
        <!-- There's a third form I omitted -->                 
        </div>
    <?php } ?>
</div>
以下是验证脚本:

$(document).ready(function()
{  
    $("#loginform").validate(  
    {  
        rules:{  
        'username':{  
            required: true,
            remote:{  
                url: "php/validatorAJAX.php",  
                type: "post"  
                }  
            },  
        'password':{  
            required: true  
            }  
        },  
        messages:{  
        'username':{  
            required: "Il campo username è obbligatorio!",
            remote: "L'username non esiste!" 
            },  
        'password':{  
            required: "Il campo password è obbligatorio!"  
            }  
        },
        submitHandler: function(form){
        if($(form).valid())
            form.submit();  
        return false;
        }           
    });
});  
<?php
$mysqli = new mysqlc();
function usernameExists($username){
    $username = trim($username);
    $stmt = $mysqli->prepare("SELECT COUNT(*) AS num FROM utenti WHERE username= ?");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $stmt->bind_result($result);
    $result = (bool)$stmt->fetch();
    $stmt->close();
    return $result;
}

if(isset($_POST['username'])){
    if(usernameExists($_POST['username'])){
        echo 'true';
    }else{
        echo 'false';
    }
}
?>
最后,这是验证脚本中包含的validatorAJAX.php:

$(document).ready(function()
{  
    $("#loginform").validate(  
    {  
        rules:{  
        'username':{  
            required: true,
            remote:{  
                url: "php/validatorAJAX.php",  
                type: "post"  
                }  
            },  
        'password':{  
            required: true  
            }  
        },  
        messages:{  
        'username':{  
            required: "Il campo username è obbligatorio!",
            remote: "L'username non esiste!" 
            },  
        'password':{  
            required: "Il campo password è obbligatorio!"  
            }  
        },
        submitHandler: function(form){
        if($(form).valid())
            form.submit();  
        return false;
        }           
    });
});  
<?php
$mysqli = new mysqlc();
function usernameExists($username){
    $username = trim($username);
    $stmt = $mysqli->prepare("SELECT COUNT(*) AS num FROM utenti WHERE username= ?");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $stmt->bind_result($result);
    $result = (bool)$stmt->fetch();
    $stmt->close();
    return $result;
}

if(isset($_POST['username'])){
    if(usernameExists($_POST['username'])){
        echo 'true';
    }else{
        echo 'false';
    }
}
?>

您可以在上测试脚本,当您在登录表单上单击Submit时,您将看到什么也没有发生。此外,没有进行任何验证。我在这里发疯了:

我修复了它:validatorAJAX.php出现问题,导致整个表单崩溃。基本上,mysqli对象是在函数外部初始化的,这导致验证失败。

在我尝试时,它似乎可以工作?如果我不填写任何内容,然后单击“登录”,它会弹出两条错误消息。如果我输入用户名和密码,它会抱怨用户不存在。似乎有用!