Unresponsive form submit button in my CodeIgniter - 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.

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

duplicate insert after refresh page (iam new in developing with codeigniter)

i need to stop insert data when refresh page iam new in codeigniter and can't do redirect easly,please support
view:pages/home.php view which has a form
<html>
<head></head>
</body>
<?php echo validation_errors(); ?>
<?php echo form_open('speed/insert_to_db'); ?>
Branch: <input type="text" name="branch" /><br/>
Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type: <input type="text" name="deviceType" /><br/>
Brand: <input type="text" name="brand" /><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="save" />
</form>
</body>
</html>
model:add_model model which has insert_to_db function
public function insert_into_db(){
$post=$this->input->post();
$data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
$this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
}
}
controller:
<?php
class Speed extends CI_Controller {
function insert_to_db()
{
$this->load->model('add_model');
if($this->input->post('save')){
$this->add_model->insert_into_db();
$this->load->view('pages/home');//loading success view
}
}
}
I think you should try this in your controler:
function insert_to_db()
{
$this->load->model('add_model');
if( isset( $this->input->post('save') ) )
{
$this->add_model->insert_into_db();
//loading success view
$this->load->view('pages/home');
}
}

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.

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>

How to Routing joomla

I have a componenet in joomla 2.5.
I have several view, in one of then I have a combobox, when I click on it, I want to call a function for that I have this
<form class="product_filter" action="<?php echo JURI::root()?>index.php/com_productos/buscarCategoria" method="POST">
<input type="hidden" class="type" name="type" value="HEALTH_FOOD"/>
<div class="select_wrapper small first">
<?php echo JHTML::_('select.genericlist', $nameCombo,'name','onChange="this.form.submit()"','value','text'); ?>
</div>
the name of my component is com_productos so in the producto.php I have this
class ProductosController extends JController
{
function buscarCategoria(){
$jinput = JFactory::getApplication()->input;
$view = $jinput->getCmd('view', 'productos');
JFactory::getApplication()->input->set('view', $view);
$model = &$this->getModel($view);
$view = &$this->getView($view, 'html');
$view->setModel($model, true);
$view->categoria();
}
but never execute this function.
Any idea
You should pass task (and controller as part of it) in the hidden field like this:
<input type="hidden" name="task" value="productos.buscarCategoria"/>
And your action can be simply index.php.
Finally I solved the problem
<input type="hidden" name="controller" value="field" />
<input type="hidden" name="option" value="com_productos" />
<input type="hidden" name="task" value="buscarCategoria" />
</form>

Resources