Ineed to send the array of html elements into different rows in database
given below is the html code.
<form action="#" method="post">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="submit" value="submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST['user'] as $key => $value)
{
echo $key." has the value = ". $value."<br>";
}
}
?>
You can achieve like this....
<?php
$this->load->database();
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST['user'] as $key => $value)
{
$data = array('user'=>$value);
$this->db->insert('table_name',$data); //Generated query >> INSERT INTO table_name(user) VALUES ('$value');
}
}
?>
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
i have a problem when i want to login , i get always that the password and email are not corrects even if they are correct ..
this is the controller :
public function authentification (Request $request)
{
$this->validate($request, [
'email' => 'required',
'mdp'=> 'required'
]);
$logmedecin=doc::orderBy('created_at','desc')->get() ;
$rdv=rendezvous::get();
foreach ($logmedecin as $log )
{
if (( $lo->Login== 'email') && ($log->Password=='mdp' ))
{
return view ('/Welcome',['med'=>$log] ,['pat'=>$rdv] );
}
}
return ("Password or mail not correct") ;
}
}
And this is the form :
<form class="form-signin" method="post" action="{{route('aziz')}}">
#csrf
<input type="text" class="form-control" name="email" placeholder=" mail" data-rule="email" autofocus="" /> </br>
<input type="password" class="form-control" name="mdp" placeholder="password" data-rule="required"/>
<label class="checkbox">
<input type="checkbox" value="remember-me" id="rememberMe" name="rememberMe"> Remember me
</label>
<button class="btn btn-primary btn-block" type="submit"> Se connecter </button> <br>
<center> Inscrivez-vous </center>
</form>
You've got a typo in your code. Change $lo->Login to $log->Login
foreach ($logmedecin as $log )
{
if (( $log->Login== 'email') && ($log->Password=='mdp' ))
{
return view ('/Welcome',['med'=>$log] ,['pat'=>$rdv] );
}
}
And I'm guessing you don't want to be checking whether there exists a doc with Login == "email" and Password == "mdp". You'll want to grab the relevant values from the user's request:
foreach ($logmedecin as $log )
{
if (( $log->Login== $request->input('email') ) && ( $log->Password== $request->input('mdp') ))
{
return view ('/Welcome',['med'=>$log] ,['pat'=>$rdv] );
}
}
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 :)
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 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