JQuery event.default和php
我有一个关于事件的问题。我在客户端验证中使用它只是为了确保输入了用户名和密码。然而,当我按下提交按钮并将验证传递给php时,我什么也没有得到。我的意思是,当我检查表格是否已提交,并做了回音检查时,我什么也没有得到。但是当我做(!isset($_POST['whatever'])时,我得到了一些东西。检查字段是否为空也是如此。我做空时什么也没有得到,但做空时得到了一些东西!空 Jquery/html代码JQuery event.default和php,php,jquery,Php,Jquery,我有一个关于事件的问题。我在客户端验证中使用它只是为了确保输入了用户名和密码。然而,当我按下提交按钮并将验证传递给php时,我什么也没有得到。我的意思是,当我检查表格是否已提交,并做了回音检查时,我什么也没有得到。但是当我做(!isset($_POST['whatever'])时,我得到了一些东西。检查字段是否为空也是如此。我做空时什么也没有得到,但做空时得到了一些东西!空 Jquery/html代码 <!DOCTYPE html> <html lang="en">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/verneyCustom.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<!-- cms login form -->
<form action="login.php" method="POST" class="form-signin" id="cms-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<span class="text-center" id="jsError"></span><!-- display client side errors -->
<input type="text" class="form-control" id="username" placeholder="Username" autofocus><br/>
<input type="password" class="form-control" id="password" placeholder="Password"><br/>
<input type="submit" class="btn btn-lg btn-primary btn-block" id="loginToSystem" value="Sign In">
</form>
</div>
<!-- include jquery v1.10.2 library to validate client side -->
<script src="js/jquery.js"></script>
<script>
//if the form is submitted check if values are ok
$("#cms-signin").submit(function(event){
if($('#username').val() == "" || $('#password').val() == ""){
event.preventDefault();
$("#jsError").html('You must fill in the form.');
}else if($('#username').val() == ""){
event.preventDefault();
$("#jsError").html('Username is required.');
}else if($('#password').val() == ""){
event.preventDefault();
$("#jsError").html('Password is required.');
}
});
</script>
</body>
</html>
请登录
//如果表单已提交,请检查值是否正确
$(“#cms登录”).submit(功能(事件){
if($('#username').val()=''|$('#password').val()=''){
event.preventDefault();
$(“#jsError”).html('您必须填写表格');
}else if($('#username').val()=“”){
event.preventDefault();
$(“#jsError”).html('需要用户名');
}else if($('#password').val()=“”){
event.preventDefault();
$(“#jsError”).html('需要密码');
}
});
php代码
<?php
//include the connection to the cms database
require_once($_SERVER['DOCUMENT_ROOT'].'/vcm/_cms/resources/database_connect.php');
//check if the form was sent
if(isset($_POST['loginToSystem'])){
echo 'o';
//create an array for errors
$errors = array();
//check if the username and password fields are empty
//yes we did a check if jquery on the front but some people disable javascript
if(empty($_POST['username']) || empty($_POST['password'])){
echo 'empty';
}else{
echo 'ok';
}
}
echo'p';
?>
所以基本上,不管发生什么,它都会回显为“p”,但只有当isset更改为!isset时,它才会回显为“o”。当保持原样时,它会回显为空,当更改为!空时,它会回显为ok,正如adeneo解释的那样,您必须添加名称
<form action="login.php" method="POST" class="form-signin" id="cms-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<span class="text-center" id="jsError"></span><!-- display client side errors -->
<input type="text" class="form-control" name="username" id="username" placeholder="Username" autofocus><br/>
<input type="password" class="form-control" name="password" id="password" placeholder="Password"><br/>
<input type="submit" class="btn btn-lg btn-primary btn-block" id="loginToSystem" value="Sign In">
</form>
如果您在表单元素中添加名称,而不仅仅是ID,因为发送表单时使用的是ID,那么可能是元素的名称和值。此外,如果第一个条件为true,即用户名或密码为空,则不会执行任何“else if”条件,使其完全无效。^^^您可能是指和,因此请回答ace
|
与&
$("#cms-signin").submit(function(event){
if($('#username').val() == "" && $('#password').val() == ""){
event.preventDefault();
$("#jsError").html('You must fill all fields.');
}
});