POST method is not working in Codeigniter v4 - codeigniter

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

Related

What is the cause of this wrong image upload error message in my Codeigniter 3 application?

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
There is, among others an "Edit account information" form, which has an image upload field. In the controller, the update() method contains the logic for the image upload action:
public function update() {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
// Upload avatar
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '100';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$uerrors = array('uerrors' => $this->upload->display_errors());
$data['uerrors'] = $uerrors;
}
if(!$this->form_validation->run() || !empty($uerrors))
{
print_r($data['uerrors']);
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else
{
$this->Usermodel->update_user($id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
The (surprising) problem I have is that, even though I am uploading an image, print_r($data['uerrors']); returns You did not select a file to upload. in the browser.
In the view, the same error is returned:
<?php echo form_open(base_url('dashboard/users/update')); ?>
<input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo set_value('first_name', $author->first_name); ?>" placeholder="First name">
<?php if(form_error('first_name')) echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo set_value('last_name', $author->last_name); ?>" placeholder="Last name">
<?php if(form_error('last_name')) echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
<textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo set_value('bio', $author->bio); ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<input type="hidden" name="avatar" id="avatar" value="<?php echo $author->avatar; ?>">
<label for="avatar">Upload avatar</label>
<div class="form-group">
<input type="file" name="userfile" id="uavatar" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
<div class="form-group">
<div class="w-50 pull-left pr-1">
<input type="submit" value="Update" class="btn btn-block btn-md btn-success">
</div>
<div class="w-50 pull-right pl-1">
Cancel
</div>
</div>
<?php echo form_close(); ?>
The error message I was expecting, considering that the image I was trying to upload is larger then the specified limit (100KB) is: The file you are attempting to upload is larger than the permitted size.
What am I doing wrong?
Try this
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
<div class="form-group">
<input type="file" name="userfile" id="userfile" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
Try to change your form opening, from :
<?php echo form_open(base_url('dashboard/users/update')); ?>
to
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
To change the form encoding type from text/plain to multipart/form-data to support image data upload. Here is the difference between each encoding type.

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 :)

Validate with codeigniter

I dont know what am i doing wrong with the validation.
here is my Controller
function update_user() {
$this->load->library('form_validation');
$this->form_validation->set_rules('sirname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('name', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('update_view');
}
else
{
$data = array(
'surname' => $this->input->post('sirname'),
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
);
$this->Account_model->update_user(31,$data);
$this->show_user();
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Updated!</div>');
redirect('home');
}
}
here is my view
<form method="post" action="<?php echo base_url() . "account/update_user"?>" >
<div class="form-group">
<label for="name">First Name</label>
<input class="form-control" name="sirname" placeholder="Your First Name" type="text" value="<?php echo $user->surname; ?>" />
<span class="text-danger"><?php echo form_error('sirname'); ?></span>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input class="form-control" name="name" placeholder="Last Name" type="text" value="<?php echo $user->name; ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo $user->email; ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-default" onclick="account.php">Update</button>
<button name="cancel" type="reset" class="btn btn-default">Cancel</button>
</div>
<?php echo form_close(); ?>
<?php endforeach; ?>
<?php echo $this->session->flashdata('msg'); ?>
i need to display the value already in the database as im doing an update info for user.. so how do i implement the set_value('name') as well.
First preference is for form_error(),
so in value for each input fields,
value="<?php form_error("surname") ? echo set_value("surname") : echo $user->surname; ?> ?>"
Which means, if form error, then echo set_value() else echo database value.
in home controller where you redirect to success load :
$this->load->library('form_validation');
it will be ok!
You can use CI's built-in set_value function, which lets you set the values of a form field. It has a second (optional) parameter, to set the default value for the field.
<? echo form_label('Last Name', 'name'); ?>
<input class="form-control" name="name" id="name" type="text" value="<? echo set_value('name', $user->name) ?>">
<? echo form_error('name', '<span class="text-danger">','</span>');?>
In the code above, when loaded for the first time, the form will show the value from the database. But when returned after failing form validation, it will show the user input.

Unresponsive form submit button in my CodeIgniter

I am trying to get to know CodeIgniter. I just couldn’t figure out what the problem with the code below is. However, when I click on the ‘submit’ button, nothing happens. I mean no record is inserted in the database table.
The php in the view is as follows.
<?php echo form_open('site/create'); ?>
<?php echo form_close();?>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
Down goes the controller
<?php
class site extends CI_Controller
{
function index()
{
$this->load->view('options_view');
}
function create()
{
$rec=array(
'title' => $this->input->post('title'),
'content'=>$this->input->post('content')
);
$this->site_model->add_record($rec);
$this->index();
}
}
?>
And the model is as follows.
<?php
class site_model extends CI_Model
{
function get_records()
{
$query=$this->db->get('other');
return $query->result();
}
function add_record($rec)
{
$this->db->insert('other',$rec);
return;
}
function update_record($rec)
{
$this->db->where('id',2);
$this->db->update('other',$rec);
}
function delete_rec()
{
$this->db-delete();
}
}
?>
The form close has to be at the end:
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
<?php echo form_close();?>
It's better and easier to use the form library from codeigniter.
I believe you must be familiar with Html already to start using codeIgniter so this should be easy... Like all html forms you need to have an opening tag and a closing tag, what you have done is open and close the form at the top before the other elements. So as expected, the submit button wont do what you need it to do. The solution is to move
<?php echo form_close();?>
to the bottom so your code appears as such
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
<?php echo form_close();?>
All the best.

Codeigniter form validation is not showing

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.

Resources