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
Related
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
<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
for somehow my codeigniter form validation is not showing on login page
given below is my login page in view
<div class="full_w">
<p>Food4U</p>
<?php echo validation_errors(); ?>
<?php echo form_open('staff/valid_login'); ?>
<label for="city">Select City</label>
<select name="city" class="text">
<option value="">Select City</option>
<?php echo $this->front_model->get_Fcities(); ?>
</select>
<label for="login">Username:</label>
<input id="login" name="username" class="text" />
<label for="pass">Password:</label>
<input id="pass" name="password" type="password" class="text" />
<div class="sep"></div>
<button type="submit" class="ok">Login</button>
<a class="button" href="">Change Password</a>
<?php echo form_close(); ?>
</form>
</div>
given below are functions in controller
function valid_login()
{
$this->form_validation->set_rules('city','City','required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('username','Username','required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|trim');
if($this->form_validation->run())
{
$data = array(
'username' =>$this->input->post('username'),
'is_loggedin' => 1
);
$this->session->set_userdata($data);
redirect('staff/member');
}
else
{
$this->load->view("dashboard/login");
}
}
function member()
{
$row['content']=$this->load->view("dashboard/member",array(),true);
$this->load->view("dashboard/template", $row);
}
function validate_credentials()
{
if($this->staff_model->can_login())
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials','Incorrect Username or Password');
return false;
}
}
and my model function is
function can_login()
{
$this->db->where('DB_User_City',$this->input->post('city'));
$this->db->where('DB_User_Login',$this->input->post('username'));
$this->db->where('DB_User_Pass',$this->input->post('password'));
$query= $this->db->get('frp_db_user');
if($query->num_rows() == 1)
{
return true;
}
else
{
return false;
}
}
if user just click on submit button user should see a message and if user provide any single value incorrect then user must also see whats wrong with login so that user can provide correct information
inside your view for each fields you set validation blow its field or some where else
write like this codes to show your validation errors.
<div class="full_w">
<p>Food4U</p>
<?php echo validation_errors(); ?>
<?php echo form_open('staff/valid_login'); ?>
<label for="city">Select City</label>
<select name="city" class="text">
<option value="">Select City</option>
<?php echo $this->front_model->get_Fcities(); ?>
</select>
<br />
<?=form_error('city')?>
<br />
<label for="login">Username:</label>
<input id="login" name="username" class="text" />
<br />
<?=form_error('username')?>
<br />
<label for="pass">Password:</label>
<input id="pass" name="password" type="password" class="text" />
<br />
<?=form_error('password')?>
<br />
<div class="sep"></div>
<button type="submit" class="ok">Login</button>
<a class="button" href="">Change Password</a>
<?php echo form_close(); ?>
</form>
</div>
Your code seems like ok. Try this one
if($this->form_validation->run() == FALSE)
{
//redirect to your login form
}
else
{
//redirect to dashboard
}
please let me know if you face any problem.
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>
I have the problem of doing the validation of fields myself. Now there are 5-6 fields in the form. So I am checking each one in my controller, and if there is wrong i wish to load the view again and pass the error array to it.
I achieved the above functionality with this:
<html>
<head>
<title>My Form</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<?php
echo $fullname;
?>
<?
echo form_open('/membership/register');
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<h5>Mobile</h5>
<input type="text" name="mobile" value="" size="15" />
<h5>Home</h5>
<input type="text" name="home" value="" size="15" />
<h5>Full Name</h5>
<input type="text" name="fullname" value="" size="100" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
and in controller the code is:
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
$this->load->view('register', $errors);
}
Now the real problem I have is if the many fields are wrong. I want to have $errors array passed to view and accessed there for all the values it contains. so I don't have to specify $fullname or $mobile to get the value. How can this be done? as to show the user everything missing in one go
First of all I advise using codeigniter's built in form validation class
Here is how I usually process my validation in the controller:
if ($this->input->post())
{
// process the POST data and store accordingly
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
// the rest of your form fields validation can be set here
if ($this->form_validation->run() == FALSE)
{
// validation hasnt run or has errors, here just call your view
$this->load->view('same_view_file_with_the_form', $data);
}
else
{
// process the POST data upon correct validation
}
}
In my view file I call each error like this:
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<span class="error-red"><?php echo form_error("username"); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<span class="error-red"><?php echo form_error("password"); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<span class="error-red"><?php echo form_error("cpassword"); ?></span>
Do all your checks in the controller prior to binding errors into the view.
e.g.
$errors = array();
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
}
if ( do_something_to_validate(mobile) )
{
$errors['mobile'] = 'invalid mobile';
}
// after checking everything do this
$this->load->view('register', $errors);