!empty function wont work on login using codeigniter - codeigniter

This is my login form
<form action="<?php echo site_url('admin/checkAdmin')?>
<div class="form-group">
<input type="text" name="email" placeholder="Enter Your Email" class="form-control">
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Enter Your Password" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Signin</button>
</div>
</form>
And my controller
public function checkAdmin()
{
$data['aEmail'] = $this->input->post('email', true);
$data['aPassword'] = $this->input->post('password', true);
if (!empty($data['aEmail']) && !empty($data['aPassword']))
{
echo 'working';
}
else
{
echo 'not working';
}
}
Whether I key in values or not, it still echos 'not working'. Where is the problem?

Controller Code:-
class admin extends CI_Controller {
function login()
{
$email= $this->input->post("email");
$password = $this->input->post("password");
$result = $this->db->get_where('table_name',array('email'=>$email,'password'=>$password))->row_array();
if ($result >0) //active user record is present
{
$url = base_url('admin/Admin/dashboard');
redirect($url);
}
else{
redirect('admin/Admin');
}
}
}

Related

How to input multiple value with checkbox in CodeIgniter?

I have prepared the form to be inputted to the database, but specifically for multiple checkboxes. I've found a similar case with the solutionbut not with the algorithm that I use
Here it is my controller
class Ruang extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("m_ruang");
$this->load->library('form_validation');
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$data["ruang"] = $this->m_ruang->getAll();
$this->load->view('admin/ruang/index.php', $data);
}
public function add()
{
$ruang = $this->m_ruang;
$validation = $this->form_validation;
$validation->set_rules($ruang->rules());
if ($validation->run()) {
$ruang->save();
$this->session->set_flashdata('success', 'Berhasil ditambahkan');
}
$this->load->view("admin/ruang/add_ruang");
}
Here it is my models
class M_ruang extends CI_Model
{
private $_table = "ruang";
public $id_ruang;
public $ruang;
public $kapasitas_kuliah;
public $kapasitas_ujian;
public $layout;
public $fasilitas;
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_ruang" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_ruang = uniqid();
$this->ruang = $post["ruang"];
$this->kapasitas_kuliah = $post["kapasitas_kuliah"];
$this->kapasitas_ujian = $post["kapasitas_ujian"];
$this->layout = $post["layout"];
$this->fasilitas = $post["fasilitas"];
$this->db->insert($this->_table, $this);
}
and here part of form view
<form action="<?php base_url('ruang/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="ruang">Nama Ruang</label>
<input class="form-control <?php echo form_error('ruang') ? 'is-invalid':'' ?>"
type="text" name="ruang" placeholder="Masukkan nama ruangan" />
<div class="invalid-feedback">
<?php echo form_error('ruang') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_kuliah">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_kuliah') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_kuliah" min="0" placeholder="Tentukan kapasitas kuliah" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_kuliah') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_ujian">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_ujian') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_ujian" min="0" placeholder="Tentukan kapasitas ujian" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_ujian') ?>
</div>
</div>
<div class="form-group">
<label for="layout">Layout</label>
<input class="form-control"
data-inputmask="'mask': ['99 x 99']" data-mask
type="text" name="layout" placeholder="Tentukan layout ruangan" />
</div>
<div class="form-group">
<label for="fasilitas">Fasilitas Tersedia</label> <br>
<input type="checkbox" name="fasilitas[]" value="Proyektor"> Proyektor
<br>
<input type="checkbox" name="fasilitas[]" value="Papan Tulis"> Papan Tulis
<br>
<input type="checkbox" name="fasilitas[]" value="Jam Dinding"> Jam Dinding
<br>
<input type="checkbox" name="fasilitas[]" value="AC"> AC
<br>
<input type="checkbox" name="fasilitas[]" value="Kipas Angin"> Kipas Angin
<br>
<input type="checkbox" name="fasilitas[]" value="Tong Sampah"> Tong Sampah
<div class="invalid-feedback">
<?php echo form_error('fasilitas') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
This really hinders my project, I hope someone can help
You can use the following line too :
$fasilitas = implode(',', $this->input->post( 'fasilitas' , TRUE ) );
If you can save fasilitas in your database as string. Then you can implode fasilitas array with comma separated as shown below:
$this->fasilitas = implode(',',$post["fasilitas"]);
it will stored in back-end side(Database) something like that.
Proyektor,Papan Tulis
I hope this will works for you.
You Can Use This to Get fasilitas as array :
$fasilitas = $this->input->post('fasilitas'); // Like array('AC','Proyektor','Kipas Angin');
In order for you to get all the checked boxes store in database, write this code.
$values = $post['fasilitas'];
$fasilitas = "";
foreach($values as $val)
{
$fasilitas .= $val . ", ";
}
Then store $fasilitas to db.
$data = array(
'fasilitas' => $fasilitas,
);
$this->db->insert('table_name', $data);
Hope that helps :)

How to get Output it showing no any error & output?

The contoller shows output error1 it does not shoe even row(),and does not give any error in developer tool. it get the file from ajax users_controller but does not work & issue is contant.
This is the form:
<form class="form-signin" >
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="name" class="sr-only">Username</label>
<input type="text" id="name" class="form-control" placeholder="Username" autofocus><br/>
<label for="Password" class="sr-only">Password</label>
<input type="text" id="password" class="form-control" placeholder="Password">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<span class="error"></span>
</form>
These are the scripts:
<script>
$(document).ready(function(){
$('form').on('submit',function(){
var username = $('#name').val();
var password = $('#password').val();
$.ajax({
type:'post',
url:'users_controller/login_check',
data: {
username:'username',
password:'password'
},
success:function(result)
{
if(result !== 'error')
{
window.location.href=result;
}
else
{
$('.error').html('');
$('.error').html('<p class="alert alert-danger">Invalid username or password</p>');
$('.error').show();
}
}
});
return false;
});
});
This is controller file:
public function login_check()
{
$username = $this->input->post('username',TRUE);
$password = $this->input->post('password',TRUE);
$this->load->model('users_model');
//$row = $this->load->users_model('getUsers');
$row = $this->users_model->getUsers($username,$password);
if($row)
{
if($this->username === $username && $this->password === $password)
{
$this->session->set_userdata('user_id',$row->user_id);
$this->session->set_userdata('user_name',$row->username);
echo base_url('users_controller');
}
else
{
echo "error";
}
}
else{
echo "error1";
print_r($row);
}
}
The contoller shows output error1 it does not shoe even row(),and does not give any error in developer tool.

I want to display data in textfield. when search data have

I want to display data in textfield . Not all data. When search by name and it have data then went to display data in textfield. And search form and want to display form is same form.
<form action="postAuth" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" name="productname" placeholder="Search Product"> <span class="input-group-btn">
<button type="submit" class="btn btn-default" name="search">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div class="form-group">
<label for="ProductName" >Product Name :</label>
<input type="text" class="form-control" name="ProductName">
</div>
</form>
route
Route::post("postAuth", ['as' => 'search' , 'uses'=> 'ProductController#postAuth']);
That's my controller
public function postAuth(Request $request)
{
//check submit
$update = $request->get('update',false);
if($update){
return $this->update($request);
}
$productname = $request->input('productname');
$product = DB::table('products')
->where('product_name','LIKE','%'.$productname.'%')
->get();
if($product->count() > 0)
return redirect()->to('/update')->withDetails($product)->withQuery($productname);
else
$request->session()->flash('alert-danger','No Data Found!');
return redirect()->to('/update');
}
can anyone help me please
Here what you exactly want : Just for example
<div class="form-group">
{{Form::label('name', trans('admin.venue.fields.name'),['class' => 'col-md-4 control-label'])}}
<div class="col-md-6">
{{Form::text('name',old('name', isset($venue) ? $venue->name : ''),['class' => 'form-control'])}}
</div>
</div>
Model function example :
public function save(Request $request) {
try {
$this->validate($request, Venue::rules(), Venue::messages());
$venue = Venue::saveOrUpdate($request);
if($venue !== false) {
if($request->get('continue', false)) {
return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
} else {
return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
}
} else {
return back()->with('error', "Unable to save venue")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save venue")->withInput();
}
}
Hope it is useful.
use session() in the value field of ProductName input tag and with some logic, for example
view file:
#if(session()->has('data'))
#if(count(session('data')))
#foreach(session('data') as $data)
<input type="text" class="form-control" name="ProductName" value="{{ $data->prodcut_name }}">
#endforeach
#else
<input type="text" class="form-control" name="ProductName" value="No Data Available">
#endif
#else
<input type="text" class="form-control" name="ProductName">
#endif
and in Controller:
public function postAuth(Request $request) {
$productname = $request->input(productname);
$product_search = Products::where('product_name', $productname)->get();
if($product_search) {
return redirect()->back()->with('data', $product_search);
}
}

Login using username or email in codeigniter

I am unable to login with email address. login is done through username only, when i try to login with email it redirect me to login page.
Please help me, so i can login with email also.
I checked many online sources but not helpful for me.
My Controllers:
STORE_ACCOUNTS_CONTROLLER
function get_with_double_condition($col1, $value1, $col2, $value2)
{
$this->load->model('mdl_store_accounts');
$query = $this->mdl_store_accounts->get_with_double_condition($col1, $value1, $col2, $value2);
return $query;
}
YOURACCOUNTS_CONTROLLER
function submit_login()
{
$this->load->module('site_security');
$this->load->module('store_accounts');
$submit = $this->input->post('submit',TRUE);
if ($submit=="Submit") {
//process the form
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Username','required|min_length[5]|max_length[60]|callback_username_check');
$this->form_validation->set_rules('pword','Password','required|min_length[7]|max_length[35]');
if ($this->form_validation->run()==TRUE) {
//figure out user_id
$col1 = 'username';
$value1 = $this->input->post('username', TRUE);
$col2 = 'email';
$value2 = $this->input->post('email', TRUE);
$query = $this->store_accounts->get_with_double_condition($col1, $value1, $col2, $value2);
foreach ($query->result() as $row) {
$user_id = $row->id;
}
//send them to the private page
$this->_in_you_go($user_id, $login_type);
$remember = $this->input->post('remember', TRUE);
if ($remember=="remember-me") {
$login_type = "longterm";
}
else
{
$login_type = "shortterm";
}
}
else
{
echo validation_errors();
}
}
}
function username_check($str)
{
$this->load->module('site_security');
$this->load->module('store_accounts');
$error_msg = "You did not enter a correct username and/or password.";
$col1 = 'username';
$value1 = $str;
$col2 = 'email';
$value2 = $str;
$query = $this->store_accounts->get_with_double_condition($col1, $value1, $col2, $value2);
$num_rows = $query->num_rows();
if ($num_rows<1) {
$this->form_validation->set_message('username_check', $error_msg);
return FALSE;
}
foreach ($query->result() as $row) {
$pword_on_table = $row->pword;
}
$pword = $this->input->post('pword', TRUE);
$result = $this->site_security->_verify_hash($pword, $pword_on_table);
if ($result==TRUE) {
return TRUE;
}
else
{
$this->form_validation->set_message('username_check', $error_msg);
return FALSE;
}
}
My Model:
STORE_ACCOUNTS_MODEL
function get_with_double_condition($col1, $value1, $col2, $value2)
{
$table = $this->get_table();
$this->db->where($col1, $value1);
$this->db->or_where($col2, $value2);
$query=$this->db->get($table);
return $query;
}
My View:
LOGIN_PAGE
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<form class="form-signin" action="<?= $form_location ?>" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputText" class="sr-only">Username or Email address</label>
<input type="text" name="username" value="<?= $username?>" id="inputEmail" class="form-control" placeholder="Username or Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="pword" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" name="remember" value="remember-me"> Remember me
</label>
</div>
<button name="submit" value="Submit" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
</div> <!-- /container -->
Your form is only POST username.
In case of email this don't validate in code
$this->form_validation->set_rules('username','Username','required|min_length[5]|max_length[60]|callback_username_check');
and $value2 will be empty
$value2 = $this->input->post('email', TRUE);
I didnt read the whole code ill provide you with a simple code that will be effective :
View File:
<form action='controller.php' method='POST'>
<input type='email' name='email'>
<input type='password' name='pass'>
<input type='submit' value='login' name='login'>
</form>
Controller.php
(define the model in constructor first in order to execute the statement $this->login_model->login();)
if(isset($_POST['login'])){
$condition=$this->login_model->login();
if(!empty($condition) && isset($condition)){
redirect('');//redirect to where ever you want or you can set the session as well
}}
login_model.php
public function login(){
$email=$_POST['email'];
$password=$_POST['pass'];
$this->db->select('*');
$this->db->from('table_name');
$this->db->where('email',$email);
$this->db->where('pass',$password);
$q= $this->db->get()-results();
return $q;
}

google recaptcha are not working?

controller: test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Fetch_data');
}
public function login()
{
$this->load->view('login');
}
public function signin()
{
$firstName = trim($this->input->post('fname'));
$email = trim($this->input->post('email'));
$phone = trim($this->input->post('mobile'));
$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
$userIp=$this->input->ip_address();
$secret='******************************';
$url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response;=".$recaptchaResponse."&remoteip;=".$userIp;
$response = $this->curl->simple_get($url);
$status= json_decode($response, true);
if($status['success'])
{
$this->session->set_flashdata('flashSuccess', 'Google Recaptcha Successful');
}
else
{
$this->session->set_flashdata('flashSuccess', 'Sorry Google Recaptcha Unsuccessful!!');
}
redirect(base_url('index.php/test/login'));
}
}
view: login.php
<?php echo $this->session->flashdata('message');?>
<div class="error"><strong><?=$this->session->flashdata('flashSuccess')?></strong></div>
<hr/>
<form method="post">
<div class="form-group">
<input type="text" name="fname" id="fname" placeholder="Your First Name" class="text-line"/>
</div>
<div class="form-group">
<input type="text" name="email" id="email" placeholder="Your Email" class="text-line"/>
</div>
<div class="form-group">
<input type="text" name="mobile" id="mobile" placeholder="Your Mobile" class="text-line"/>
</div>
<div class="g-recaptcha" data-sitekey="*********************************"></div>
<div class="form-group">
<input type="submit" name="signup" id="signup" value="Sign Up" class="btn btn-warning"/>
</div>
</form>
In this code, I am using google recaptcha. when I have checked recaptcha checkbox
then it could not show message i.e. Google Recaptcha Successful or Sorry Google Recaptcha Unsuccessful. I don't know why ? can any body tell me where I am doing wrong ?

Resources