PHP - Form Elements Resetting - php

I have made a simple form with a checkbox that echos out true or false depending on if the checkbox is set or not, however every time i click submit once the checkbox control has been ticked it seems to reset.
How can i modify my code to store the value of any form controls so if i tick the box it remains ticked after i have clicked submit?
<?php
// Setup Variables
$Result = '';
if (isset($_POST["submit"])) {
//Use Captials?
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
$Result = "true";
} else {
$Result = "false";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Contact Form</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
Checkbox
<input type="checkbox" name="formCheckbox" value="Yes"/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</form>
<div class="col-sm-10 col-sm-offset-2">
<?php echo $Result; ?>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

Add the checked property &
Change the way the check box is displayed:
<?php
// Setup Variables
$Result = '';
if (isset($_POST["submit"])) {
//Use Captials?
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
$Result = "true";
} else {
$Result = "false";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Contact Form</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Contact Form Example</h1>
<form class="form-horizontal" role="form" method="post" action="index.php">
Checkbox
<?php
if(isset($_POST['formCheckbox']) && $_POST['formCheckbox'] == 'Yes') {
echo '<input type="checkbox" name="formCheckbox" value="Yes" checked/>';
}
else
echo '<input type="checkbox" name="formCheckbox" value="Yes"/>';
?>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</form>
<div class="col-sm-10 col-sm-offset-2">
<?php echo $Result; ?>
</div>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

Related

Why PHP can't recognize when the input[type='button'] is clicked on?

I just want to simply echo 'OK!' when the input with type="button" name="calculateBtn" is clicked on, but PHP does not recognize that with the condition if (isset($_POST['calculateBtn'])) so wouldn't do anything, can somebody please tell me why is that?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="" method="post">
<div>
<input id="input" type="text">
<?php
if (isset($_POST['calculateBtn'])) {
echo 'ok';
}
?>
<input name="calculateBtn" value="change the value" type="button">
</div>
</form>
</div>
</div>
</body>
</html>
First, you don't have an action defined in the form, second the button type is set as a button instead of submit, it will not submit the form.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<form action="page.php" method="post">
<div>
<input id="input" type="text">
<?php
if (isset($_POST['calculateBtn'])) {
echo 'ok';
}
?>
<input name="calculateBtn" value="change the value" type="submit">
</div>
</form>
</div>
</div>
</body>
</html>
please set the page.php to the name of your page(file).

How to hide an option in a dropdown menu PHP?

I am learning PHP, and would like to know if there is any way to hide an option depending on what I get,
basiccaly this is my code it's simple.
<?php
require 'database.php';
session_start();
if(!isset($_POST['update'])){
$id=$_GET['id'];
$state=$_GET['state'];
$sql="SELECT * from states";
$show=$conn->prepare($sql);
$show->execute();
$result = $show->fetchAll(PDO::FETCH_ASSOC);
}else{
$id=$_POST['id'];
$state=$_POST['state'];
$sql="UPDATE pqrs SET fk_state=:fk_state WHERE idPqrs=:id";
$update=$conn->prepare($sql);
$update->bindParam(':id',$_POST['id']);
$update->bindParam(':fk_state',$_POST['state']);
$update->execute();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<body>
<div class="ml-5"><?php require 'partials/header.php' ?></div>
<div class="container col-5">
<h1 class="text-center mb-5">Extreme Techonologics</h1>
<h2 class="text-center">Ingrese los campos a cambiar</h2>
<form action="pqr_update.php" method="POST">
<div class="form-group">
<input type="hidden" value="<?php echo $id?>" class="form-control" name="id">
</div>
<div class="form-group my-5">
<label for="pqr_type">Seleccione el estado del PQR:</label>
<select class="form-control" id="state" name="state">
<?php foreach($result as $state):?>
<option value="<?php echo $state['idState'];?>"><?php echo $state['state'];?></option>
<?php endforeach ?>
</select>
</div>
<button type="submit" class="btn btn-primary" name="update">Enviar</button>
</form>
</div>
Cerrar Sesión
</body>
</html>
</html>
Basically I would like to know if when $ _GET = ['state'] = 'Nuevo', when I press the button (name = 'update') what I receive from the POST, it only shows me an option (En Ejecución), in theory there are 3 states , Nuevo, En Ejecución and Cerrado. When the status is ¿Nuevo' I don't want it to show me the option "EN Ejecucuion" and when the option received is "EN Ejecucuion" I do not want it to show me the option 'Nuevo'.
Based on the above requirement, try this:
<?php
require 'database.php';
session_start();
if(!isset($_POST['update'])){
$id=$_GET['id'];
$state=$_GET['state'];
$sql="SELECT * from states";
$show=$conn->prepare($sql);
$show->execute();
$result = $show->fetchAll(PDO::FETCH_ASSOC);
}else{
$id=$_POST['id'];
$state=$_POST['state'];
$sql="UPDATE pqrs SET fk_state=:fk_state WHERE idPqrs=:id";
$update=$conn->prepare($sql);
$update->bindParam(':id',$_POST['id']);
$update->bindParam(':fk_state',$_POST['state']);
$update->execute();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<body>
<div class="ml-5"><?php require 'partials/header.php' ?></div>
<div class="container col-5">
<h1 class="text-center mb-5">Extreme Techonologics</h1>
<h2 class="text-center">Ingrese los campos a cambiar</h2>
<form action="pqr_update.php" method="POST">
<div class="form-group">
<input type="hidden" value="<?php echo $id?>" class="form-control" name="id">
</div>
<div class="form-group my-5">
<label for="pqr_type">Seleccione el estado del PQR:</label>
<select class="form-control" id="state" name="state">
<?php
foreach($result as $state):
if (
($_REQUEST['state'] == '¿Nuevo\'' && $state['state'] != 'EN Ejecucuion')
&&
($_REQUEST['state'] == 'EN Ejecucuion' && $state['state'] != '¿Nuevo\'')
):
?>
<option value="<?php echo $state['idState'];?>"><?php echo $state['state'];?></option>
<?php endif; endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary" name="update">Enviar</button>
</form>
</div>
Cerrar Sesión
</body>
</html>
</html>

Login form not validating successfully

I want login/ to be successful
This is my connect File for starting session
<?php
$connect_error='Error...'
session_start();
$con=mysql_connect('localhost','root','')or die(connect_error);
mysql_select_db('lothlorien',$con) or die(connect_error);
$errors=array();
require 'Functions/users.php';
require 'Login/L1.php';
require 'Functions/General.php';
This is My Login Widget File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lothlorien</title>
<!-- CSS -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/form-elements.css">
<link rel="stylesheet" href="assets/css/style.css">
<style>
.row{
margin:auto;
width:700px;
}
</style>
</head>
<body>
<?php
include 'Core/Database/connect.php';
?>
<div class="top-content">
<div class="inner-bg">
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2 text">
<h1>Lothlorien Login Form</h1>
</div>
</div>
</div>
<div class="content">
<div class="row">
<div class="form-box">
<div class="form-top">
<div class="form-top-left">
<h3>Login to our site</h3>
<p>Enter username and password to log in:</p>
</div>
<div class="form-top-right">
<i class="fa fa-lock"></i>
</div>
</div>
<div class="form-bottom">
<form role="form" action="" method="post" class="login-form">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn">Sign in!</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="assets/js/jquery-1.11.1.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/jquery.backstretch.min.js"></script>
<script src="assets/js/scripts.js"></script>
<!--[if lt IE 10]>
<script src="assets/js/placeholder.js"></script>
<![endif]-->
</body>
</html>
?>
this is my Login for login validation
<?php
include 'Core/Database/connect.php';
include 'Core/Functions/Users.php';
if(empty($_POST)===false)
{
$form_username = $_POST['form-username'];
$form_password = $_POST['form-password'];
}
if(user_exists($form_username)===false)
{
$errors[]="Register before Logging in";
}
else if(user_active($form_username)===false)
{
$errors[]="Not activated Your account";
}
This is where i want the validation to happen
else
{
$login=login($form_username,$form_password);
if($login=== false)
{
$errors[]="Wrong Username and password combination";
}
This is where the redirection takes place
else
{
$_SESSION['user_id']=$login;
header('Location:index.php');
exit();
}
}
print_r($errors);
?>
And finally these are my functions...
<?php
function user_exists($form_username)
{
$form_username=sanitize($form_username);
$query=mysql_query("SELECT COUNT('User_id') FROM 'user' WHERE 'Username'='$form_username'");
return (mysql_result($query,0) == 1) ? true :false;
}
function user_active($form_username)
{
$form_username=sanitize($form_username);
$query=mysql_query("SELECT COUNT('User_id') FROM 'user' WHERE 'Username'='$form_username' AND 'active'= 1");
return (mysql_result($query,0) == 1) ? true :false;
}
function user_id_from_username($username)
{
$form_username=sanitize($username);
$query=mysql_query("SELECT 'user_id' FROM 'user' WHERE 'Username'='$form_username' ");
return (mysql_result($query,0,'user_id'));
}
function login($form_username,$form_password)
{
$user_id=user_id_from_username($form_username);
$form_password=md5($form_password);
$query=mysql_query("SELECT COUNT('User_id') FROM 'user' WHERE 'Username'='$form_username' AND 'Password'= '$form_password'");
return (mysql_result($query)==1)? $user_id:false;
}
?>
I don't know the root of my problem so I kinda posted all up
Problems I encounter
1)No error messages showing up..
2)want to redirect to index.php after successful login
3)Successful Message should be displayed once login is done
Database entries are correct.

PHP Session variable not carrying over

I am working with PHP session variables, but the problem is that the session variables are not carrying over. I checked using Firebug, and found that the session variables are not being created at all.
print_r($_SESSION); returns an empty array after the page returns to index.php, but displays it correctly before the refresh.
logincheck.php:
<?php
session_start();
error_reporting(-1);
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
{
$username=$_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Smart Shopping Cart System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<style type="text/css">
.show-grid{
background:#E0E0E0;
}</style>
</head>
<body>
<?php
include "connection.php"
?>
// ** Some Content**
</body>
</html>
<?php
}
else
{
?>
<html>
<head>
<title>Log In | SmartCart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-brand">
SmartCart
</div>
</div>
</div>
<div class="container">
<br />
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<form role="form" method="post" name="login" action="test.php">
<div class="form-group">
<label for="username">Operator Username</label>
<input type="username" class="form-control" name="username" placeholder="Enter your username">
</div>
<div class="form-group">
<label for="pass">Password</label>
<input type="password" class="form-control" name="pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-default" name="sub">Submit</button>
</form>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body></html>
<?php
}
?>
test.php
<?php
session_start();
error_reporting(-1);
?>
<html><head><title>Redirecting...</title>
</head>
<body>
Redirecting....
<?php
include "connection.php";
if(isset($_POST['sub']))
{
$u_name=$_POST['username'];
$password=mysql_escape_string($_POST['pass']);
$password=mysql_escape_string($password);
$w="select * from operators where operator_username = '$u_name' AND operator_pass = '$password'";
$b=mysql_query($w) or die(mysql_error()."in query $w");
$num_rows = mysql_num_rows($b);
if($num_rows > 0)
{
$_SESSION['logged_in']=true;
$_SESSION['username']=$_POST['username'];
print_r($_SESSION);
echo '<meta http-equiv="refresh" content="0; url=index.php">';
}
else
{
echo "<script>
alert('Your username or password is incorrect. Please try again'); window.location = 'index.php'; </script>";
}
}
?>
</body></html>
Please help.
Spot the difference:
logincheck:
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
^^^^^^^^^
test:
$_SESSION["loggedin"]=true;
^^^^^^^^
One of these things is not like the other...
I think the under score is missing in on test.php page
in $_SESSION['loggedin']=true;
change this to $_SESSION['logged_in']=true;
As you are using this in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true)
there is no error with you session part the error is may be in you db access part check your database connection. you can check you session by commenting the query part, it is working fine.

Bind a PHP form button to a javascript method

Hi guy I'm new to php and I have a php form as shown below, it used to work before I applied bootstrap to it.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Login Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/bootstrap/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<!-- Main Container -->
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4 col-md-offset-4">
<h1 class="text-center login-title">Sign in to HRMS</h1>
<div class="account-wall">
<img class="profile-img" src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120"
alt="">
<form class="form-signin" method="post" action="">
<input type="text" id="txtEmpID" class="form-control" placeholder="Employee ID" required autofocus>
<input type="password" id="txtEmpPass" class="form-control" placeholder="Password" required>
<button id="btnLogin" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var login_url = "<?php echo Util::convertLink("Login") ; ?>" ;
<?php include (PATH_CODE . "js/general/login.min.js") ; ?>
</script>
</body>
</html>
with the button Login bind to a method in login.js
$(document).ready(function()
{
$('#btnLogin').button().bind('click',this,onLogin) ;
}) ;
function onLogin(obj) {
alert( $('#txtEmpID').val());
}
it there something i'm missing?
$(document).ready(function()
{
$('#btnLogin').bind({
click: function() {
onLogin(this);
}
});
});
function onLogin(obj) {
alert( $('#txtEmpID').val());
}
DEMO

Categories