in CodeIgniter form_validation not working - codeigniter

<form action="<?php echo base_url().'login/index'; ?>" method="post" name="loginForm" id="loginForm">
<div class="mb-3">
<input type="text" class="form-control" id="username" name="username" autocomplete="off">
</div>
<div class="mb-3">
<input type="password" class="form-control" id="password" name="password" autocomplete="off">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->load->library('form_validation');
$this->form_validation-
>set_rules('username','username','required');
$this->form_validation-
>set_rules('password','Password','required');
if($this->form_validation->run() == false){
$this->load->view('admin/login');
}
else{
echo"Form validation Successfully";
}
}
}

You are using base_url() without adding this library
// need this to use base_url() in the template.php
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
try with this line again

Related

POST method is not working in Codeigniter v4

I'm using form_open() for open form but form method is always showing get.
echo $this->request->getMethod() showing always get.
generated HTML is:
<form action="http://darkadmin.com/admin" method="post" accept-charset="utf-8">
<input type="hidden" name="_csrf" value="45435e3abd2d067883dacd6f62280fa7" />
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
View:
<?php if(isset($validation)) { ?>
<?php echo $validation->listErrors(); ?>
<?php } ?>
<?php echo form_open(current_url().'admin'); ?>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
<?php echo form_close(); ?>
Controller:
<?php
namespace App\Controllers;
class Login extends BaseController
{
public function __construct(){
helper(['form', 'url']);
}
public function index()
{
$rules = [
'fname' => 'required',
'lname' => 'required'
];
echo $this->request->getMethod();
if($this->request->getMethod() == 'post' && $this->validate($rules)){
echo 'Success';
} else {
$this->data['validation'] = $this->validator;
}
return view($this->data['config']->theme.'login_view',$this->data);
}
}
How can I solve this problem? Please help me

cant find "Class 'App\Http\Controllers\Controller' not found" on laravel 5.4

i have an issue with my laravel project
currently i been working the register page, but it stuck at class controller not found
here's my route script
Route::get('/', function() {
return view('home');
});
Route::get('/signin', function() {
return view('login');
});
Route::get('/register', function() {
return view('register.register');
});
Route::post('/register_action','RegisterController#store');
the RegisterController script
<?php
namespace App\Http\Controllers;
//use App\RegisterController;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
//
public function store(){
echo "test";
}
}
and view register blade
#extends('layout');
#section('content')
<h2>Vertical (basic) form</h2>
<form action="/register_action" method="post">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group">
<label for="name">Name:</label>
<input type="name" name="username" class="form-control" id="name" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" name="password" class="form-control" id="pwd" placeholder="Enter password">
</div>
<div class="form-group">
<label for="pwd"Confirm >Password:</label>
<input type="password" name="cpassword" class="form-control" id="pwd" placeholder="Confirm password">
</div>
<div class="checkbox">
<label><input type="checkbox" name="remember"> Remember me</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
#endsection
Check if Http/Controllers/Controller.php exists in your project.
If not, just create it, copy-paste this code and save:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Let me know if it works!
You should try this:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
//
public function store(){
echo "test";
}
}
Hello guys you need to write the below one line of code into your
protected $namespace = 'App\Http\Controllers';
app\providers\RouteServiceProvider.php file.
and then you can access controller into web.php file

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 ?

Validation rules in CodeIgniter

I am trying to validate email and password using the CodeIgniter's form_validation library. But when I typed wrong email or password the error message of validation_rules is not displayed.
The controller:
public function anuncios()
{
$usr=$this->input->post('Usuario');
$this->input->post('Contrasenya');
$this->form_validation->set_rules('drcorreo','Nombre de usuario',
'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('contrasena','Contraseña',
'trim|required|min_length[8]|md5|xss_clean');
if($this->form_validation->run())
{
$this->load->model('modelo_usuarios');
if($this->modelo_usuarios->puede_entrar())
{
echo "Credenciales correctos";
$this->load->model("modelo_bd");
$data['vanc']=$this->modelo_bd->datos();
$this->load->view('vancios',$data);
return true;
}
else { //this one is never displayed
echo "Credenciales incorrectos";
echo "Usuario o contrasenya incorrectos<br /><br />";
$this->load->view('indice');
return false;
}
}
else { //this one is displayed but not the rules specified in
//form_validation_lang.php file
echo "Las reglas no son validas";
$this->load->view('indice');
}
}
View:
<div id="acformulario">
<form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
<label for="correo" id="dcorreo">Dirección de correo</label>
<input type="text" name="drcorreo" id="dcc"/><br /><br />
<label for="contrasenya" id="cont">Contraseña</label>
<input type="password" name="contrasena" id="cmcont"/><br /><br />
<!--<label for="enviar"></label>-->
<input type="submit" name="envia" id="bentrar" value="Entrar" />
</form>
</div>
Why error messages are not displayed?
Thanks.
i assume you call "form" helper in your autoload.php file,
use your view like this;
<div class="errors"><?php echo validation_errors(); ?></div>
<div id="acformulario">
<form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
<label for="correo" id="dcorreo">Dirección de correo</label>
<input type="text" name="drcorreo" id="dcc"/><br /><br />
<label for="contrasenya" id="cont">Contraseña</label>
<input type="password" name="contrasena" id="cmcont"/><br /><br />
<!--<label for="enviar"></label>-->
<input type="submit" name="envia" id="bentrar" value="Entrar" />
</form>
</div>
the point is "validation_errors()" function in here.
The 'can not enter' message is passed back after the rules have been passed. It is inside of the if statement when the form validation is true.
If you want this message to be displayed either load the message into the view, or set it as flash data.
Why not make the puede_entrar() model call into a custom callback form validation rule, then it can be added to the rules that are set.
Look here for information on custom callbacks.
This is how I would code your controller;
<?php
class Foo extends CI_Controller {
public function anuncios() {
// xxs_clean set globally in config
$this->form_validation->set_rules('drcorreo','Nombre de usuario', 'trim|required|min_length[5]|callback_puede_entrar');
// should use sha1 at least for hashing, see http://www.freerainbowtables.com/tables/
$this->form_validation->set_rules('contrasena','Contraseña', 'trim|required|min_length[8]|md5');
if($this->form_validation->run()) {
$this->load->model('modelo_bd');
$data['vanc']=$this->modelo_bd->datos();
$this->load->view('vancios',$data);
} else {
// redisplay form with validation errors
$this->load->view('indice');
}
}
public function puede_entrar($val) {
$this->load->model('modelo_usuarios');
if($this->modelo_usuarios->puede_entrar()) {
return TRUE;
} else {
$this->form_validation->set_message('puede_entrar', 'Las reglas no son validas.');
return FALSE;
}
}
}
The view (assuming you have autoloaded or have loaded $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); elsewhere);
<div id="acformulario">
<form action="http://localhost/Pruebas/index.php/cindice/anuncios" method="post">
<?php echo form_error('drcorreo'); ?>
<label for="correo" id="dcorreo">Dirección de correo</label>
<input type="text" name="drcorreo" id="dcc"/><br /><br />
<label for="contrasenya" id="cont">Contraseña</label>
<?php echo form_error('contrasena'); ?>
<input type="password" name="contrasena" id="cmcont"/><br /><br />
<!--<label for="enviar"></label>-->
<input type="submit" name="envia" id="bentrar" value="Entrar" />
</form>
</div>

codeigniter submits to unknown form

I have this form:
<html>
<head>
<title>التسجيل</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body dir="rtl">
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>الإسم الكامل:</h5>
<input type="text" name="fullname" value="" size="50" />
<h5>الجوال:</h5>
<input type="text" name="mobile" value="" size="50" />
<h5>هاتف المنزل:</h5>
<input type="text" name="home" value="" size="50" />
<h5>اسم المستخدم:</h5>
<input type="text" name="username" value="" size="50" />
<h5>كلمة السر:</h5>
<input type="text" name="password" value="" size="50" />
<h5>اعادة كلمة السر:</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>الايميل:</h5>
<input type="text" name="email" value="" size="50" />
<br><br>
<div><input type="submit" value="التسجيل" /></div>
</form>
</body>
</html>
Please don't mind the arabic writings.The controller as follows:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Membership extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
public function index()
{
//Loads the login page which has a registration option
$this->load->view('login');
}
public function register()
{
$post = $this->input->post();
if($post)
{
//handle posted data
$fullname = $this->input->post('fullname');
$email = $this->input->post('email');
$mobile = $this->input->post('mobile');
$home = $this->input->post('home');
$username = $this->input->post('username');
$password = $this->input->post('password');
$confirmPassword = $this->input->post('cpassword');
$memberType = 'silver';
if ($this->form_validation->run() == FALSE)
{
$this->load->view('register');
}
else
{
$this->load->view('welcome_message');
}
}else{
$this->load->view('register');
}
}
}
Now when I click submit button on the form, it shows 404 and link in address bar is:
http://domain.com/index.php/form
Why is this happening? It should check if posted, then load x. if not load y.
Am a beginner in codeigniter so apologies.
As you have copied code from CI user guide :
form_open('form'); // here is a form action which is form class index function
just change it to :
form_open('membership/register');
which means :
form_open('location/to/sumbit/to');
that's it!
Change
<?php echo form_open('form'); ?>
To
<?php echo form_open('membership/register'); ?> // class/function

Resources