basic php mysql login script without page reload
insert.php
<?php
define('HOST','localhost');
define('USERNAME', 'root');
define('PASSWORD','');
define('DB','mydba');
session_start();
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$username = $_POST['username'];
$pass = $_POST['password'];
//$_SESSION["username"] = $username;
//$sql = "select * from users where username='$username' && password='$pass'";
//if(mysqli_query($con, $sql)){
//echo 'success';
//echo " username is " . $_SESSION["username"] . ".";
//}
$sql="SELECT * FROM users WHERE username='$username' and password='$pass'";
$result=mysqli_query($con, $sql);
$count=mysqli_num_rows($result);
if($count==1){
//session_register("username");
//session_register("pass");
$_SESSION = mysqli_fetch_array($result, MYSQLI_ASSOC);
header("Location: page5.php");
}else
{
echo 'Either Your Account is inactive or Email address /Password is Incorrect';
}
?>
<?php
//session_start();
$_SESSION['id']="";
?>
<div id="loginbutton">login button</div>
<div id="menu">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
</ul>
</div>
<?php
if (!isset($_SESSION['id']) || empty($_SESSION['id'])){ ?>
<script>
document.getElementById("menu").style.display = "none";
document.getElementById("loginbutton").style.display = "show";
</script>
<?php }
else { ?>
<script>
document.getElementById("loginbutton").style.display = "none";
document.getElementById("menu").style.display = "show";
</script>
<?php
}
?>
insert.js
$('#myform').submit(function(){
return false;
});
$('#insert').click(function(){
$.post(
$('#myform').attr('action'),
$('#myform :input').serializeArray(),
function(result){
$('#result').html(result);
}
);
});
<?php
define('HOST','localhost');
define('USERNAME', 'root');
define('PASSWORD','');
define('DB','mydba');
session_start();
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$username = $_POST['username'];
$pass = $_POST['password'];
//$_SESSION["username"] = $username;
//$sql = "select * from users where username='$username' && password='$pass'";
//if(mysqli_query($con, $sql)){
//echo 'success';
//echo " username is " . $_SESSION["username"] . ".";
//}
$sql="SELECT * FROM users WHERE username='$username' and password='$pass'";
$result=mysqli_query($con, $sql);
$count=mysqli_num_rows($result);
if($count==1){
//session_register("username");
//session_register("pass");
$_SESSION = mysqli_fetch_array($result, MYSQLI_ASSOC);
header("Location: page5.php");
}else
{
echo 'Either Your Account is inactive or Email address /Password is Incorrect';
}
?>
<?php
//session_start();
$_SESSION['id']="";
?>
<div id="loginbutton">login button</div>
<div id="menu">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
</ul>
</div>
<?php
if (!isset($_SESSION['id']) || empty($_SESSION['id'])){ ?>
<script>
document.getElementById("menu").style.display = "none";
document.getElementById("loginbutton").style.display = "show";
</script>
<?php }
else { ?>
<script>
document.getElementById("loginbutton").style.display = "none";
document.getElementById("menu").style.display = "show";
</script>
<?php
}
?>
insert.js
$('#myform').submit(function(){
return false;
});
$('#insert').click(function(){
$.post(
$('#myform').attr('action'),
$('#myform :input').serializeArray(),
function(result){
$('#result').html(result);
}
);
});
Comments
Post a Comment