Javascript 使用jquery在页面上登录
我想创建网页登录系统 没有javascript,我可以在这种结构中完美登录:Javascript 使用jquery在页面上登录,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想创建网页登录系统 没有javascript,我可以在这种结构中完美登录: <?php require_once('login.php'); $login = new Login(); // if we are logged in here: if ($login->isUserLoggedIn() == true) { // yes we are echo "<div id=\"login\">you logged in as $_SESSION[
<?php
require_once('login.php');
$login = new Login();
// if we are logged in here:
if ($login->isUserLoggedIn() == true) {
// yes we are
echo "<div id=\"login\">you logged in as $_SESSION[user_name]. <a href=\"?logout\">Logout</a></div>";
} else {
// no
echo "
<div id=\"login\" style=\"display: none;\">
<form action=\"index_.php\" method=\"post\" name=\"loginform\" id=\"loginform\" onsubmit=\"return false;\">
<input type=\"text\" class=\"form-control\" id=\"user_name\" name=\"user_name\><br>
<input type=\"password\" class=\"form-control\" id=\"user_password\" name=\"user_password\"><br>
<label class=\"checkbox-label\" style=\"pointer-events: all;\" for=\"user_rememberme\"><input checked class=\"user_rememberme\" name=\"user_rememberme\" id=\"user_rememberme\" value=\"1\" type=\"checkbox\"/>
Remember me</label>
<a href=\"#frg\" class=\"frg float:right; display:inline;\">Forgot Password?</a>
<input class=\"btn btn-success\" type=\"submit\" name=\"login\" id=\"loginbutton\" value=\"Login\">
</form>
</div><!-- login ends -->
";
}
?>
下一步,我想在ajax的帮助下登录页面。下面是我的javascript代码:
<script type="text/javascript">
// login on page
$(function(){
$("#loginform").submit(function(){ // .click yerine .submit
if($("#loginform").valid()){
$.ajax({
type: "POST",
url: "index_.php",
data: $("#loginform").serialize(),
beforeSend: function(){
$('#message').html('Loading...');
},
success: function(data){
$('#message').html(data);
}
});
}
});
});
</script>
//登录第页
$(函数(){
$(“#loginform”).submit(函数(){//。单击yerine.submit
if($(“#loginform”).valid(){
$.ajax({
类型:“POST”,
url:“index.php”,
数据:$(“#loginform”).serialize(),
beforeSend:function(){
$('#message').html('Loading…');
},
成功:功能(数据){
$('#message').html(数据);
}
});
}
});
});
特别是javascript代码的这一部分
成功:函数(数据){
$('#message').html(数据);
我想输出我的php结果,但在数据中复制php显然不起作用
我该怎么办?您可以添加一个额外的POST字段(例如“AJAX”)
并返回其他输出(如果已设置)
$.ajax({
type: "POST",
url: "index_.php",
data: $("#loginform").serialize() + "&ajax=1",
您可以添加一个额外的POST字段(例如“AJAX”)
并返回其他输出(如果已设置)
$.ajax({
type: "POST",
url: "index_.php",
data: $("#loginform").serialize() + "&ajax=1",
您应该使用$('#message').html(数据)
…这样#消息
会加载整个页面。但我只想看到结果输出。好吧;上面的内容并不明显。所以第一段代码是index.php
?下一段代码是什么?上面所有的代码都是index.php,除了jquery、css等。它就是这样工作的。**th我帖子中的第二段php代码是我在单击login后看到的消息(您登录,失败等),但我想用jquery显示该代码。您应该使用$('#message').html(数据)
…这样#消息
会加载整个页面。但我只想看到结果输出。好吧;上面的内容并不明显。所以第一段代码是index.php
?下一段代码是什么?上面所有的代码都是index.php,除了jquery、css等。它就是这样工作的。**th我帖子中的第二段php代码是我在单击login后看到的消息(您登录了,失败了等等),但我想用jquery显示该代码。因此,我应该添加名为ajax的帖子字段,并输入值“1”然后发送表单。但是我将在哪里使用php代码?我很抱歉我的无知,但我不明白您可以在index.php中运行php代码,您需要多少。只需将其添加到那里,并在其周围创建一个if(isset($_POST['ajax']){run php code here}。因此,我应该添加名为ajax的POST字段并将值设置为“1”然后发送表单。但是我将在哪里使用php代码?我很抱歉我的无知,但我不明白你可以在index.php中运行php代码,你需要多少。只需将它添加到那里,并在它周围创建一个if(isset($_POST['ajax']){运行php代码。