Php 使用Jquery隐藏登录表单
我创建了一个登录名,当我点击提交按钮时,它会将变量发送到login_success.php页面。但我想在我点击提交按钮时,登录名就会关闭。我可以使用Jquery关闭表单Php 使用Jquery隐藏登录表单,php,jquery,forms,hide,Php,Jquery,Forms,Hide,我创建了一个登录名,当我点击提交按钮时,它会将变量发送到login_success.php页面。但我想在我点击提交按钮时,登录名就会关闭。我可以使用Jquery关闭表单 <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $(".loginform").hide(); }); }); </script> $(文档).ready(
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".loginform").hide();
});
});
</script>
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
$(“.loginform”).hide();
});
});
但这个时间表单不会向.php文件发送请求。我把它做成了一个类似于addin脚本的.php文件,然后重定向到index.html站点。它也不错,但我可以看到反射。我如何将它们结合起来
这是我的表格
<div class="loginform">
<form action="php/login.php" method="post" id="login">
<fieldset class="loginfield">
<div>
<label for="username">User Name</label> <input type="text" id="username" name="username">
</div>
<div>
<label for="password">Password</label> <input type="password" id="password" name="password">
</div>
</fieldset>
<button type="submit" id="submit-go" ></button>
</form>
</div>
用户名
密码
编辑
我将函数用作NAVEED sad。我在firefox中安装了FireBug,我可以看到我的表单验证工作正常。它发送并请求login.php,但我无法对表单进行任何更改。它不会关闭,也不会在div标记上显示$arr值 试试看
PS您知道将onclick
处理程序应用于页面上的所有
元素是个坏主意,对吗?试试看
$(document).ready(function(){
$("button").click(function(){
$(".loginform").hide();
return false;
});
});
PS您知道将onclick
处理程序应用于页面上的所有
元素是个坏主意,对吗?您应该使用/AJAX组合:
$(document).ready(function(){
$("button").click(function(){
$(".loginform").hide();
return false;
});
});
如果您的表单如下所示:
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<div id='errors'></div>
<div class='loginform' id='loginform'>
<form action="php/login.php" method="post" id="login">
Username:<input type="text" id="username" name="username">
Password:<input type="password" id="password" name="password">
<button type="submit" id="submit-go" value='Login'></button>
</form>
</div>
您的login.php文件如表单操作属性中所述:
$username = $_POST['username'];
$password = $_POST['password'];
if( $username and $password found in database ) {
// It will replace only id='loginform' DIV content
// and login form will disappear
$arr = array ( "loginform" => "you are logged in" );
} else {
// It will replace only id='errors' DIV content
$arr = array ( "errors" => "You are not authenticated. Please try again" );
}
echo json_encode( $arr );
更多详细信息:
- :
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<div id='errors'></div>
<div class='loginform' id='loginform'>
<form action="php/login.php" method="post" id="login">
Username:<input type="text" id="username" name="username">
Password:<input type="password" id="password" name="password">
<button type="submit" id="submit-go" value='Login'></button>
</form>
</div>
您的login.php文件如表单操作属性中所述:
$username = $_POST['username'];
$password = $_POST['password'];
if( $username and $password found in database ) {
// It will replace only id='loginform' DIV content
// and login form will disappear
$arr = array ( "loginform" => "you are logged in" );
} else {
// It will replace only id='errors' DIV content
$arr = array ( "errors" => "You are not authenticated. Please try again" );
}
echo json_encode( $arr );
更多详细信息:
- :
action=“php/login”
,则可以在$.ajax
函数中使用url:$(this).attr('action')
。看看答案的变化。可能是你遗漏了什么。确保jquery文件包含在表单文件中,路径正确。若jquery代码位于单独的文件中,那个么请确保它包含在表单文件中。检查jquery中用于捕获事件的表单的id。我尝试了这个方法,但当我单击submit时,什么也没有发生。对于使用ajax,我应该使用一些插件还是Jquery有它自己的功能?在表单标签中,我是否应该删除action=“php/login”?您必须下载jQuery并将其包含在表单文件中。如果表单标记中有action=“php/login”
,则可以在$.ajax
函数中使用url:$(this).attr('action')
。看看答案的变化。可能是你遗漏了什么。确保jquery文件包含在表单文件中,路径正确。若jquery代码位于单独的文件中,那个么请确保它包含在表单文件中。检查jquery中用于捕获事件的表单的id。