My problem is when I click submit the form_validation->run() returns false
Here is my controller
public function change_pass()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('new_pass', 'New Password', 'required|matches[cnew_pass]');
$this->form_validation->set_rules('cnew_pass','Confirm Password','required|matches[new_pass]');
if($this->form_validation->run()==FALSE)
{
die(print_r("Always go here"));
}
}
Here is my view.................
<?php echo form_open('tickets/change_pass'); ?>
<center>
<table>
<tr>
<td<label for="new_pass">New Password:</label>
<input type="password" name="new_pass" class="form-control"><?php echo form_error('new_pass');?>
</td>
</tr>
<tr>
<td<label for="cnew_pass">Confirm New Password:</label>
<input type="password" name="cnew_pass" class="form-control"><?php echo form_error('cnew_pass');?>
</td>
</tr>
</table>
<input type="submit" name="change" class="btn btn-success"> <input type="reset" class="btn btn-danger">
</center>
hi you have to first load view in separate function.this will be your code.
public function index()
{
$this->load->helper('form');
$this->load->view('your view name');
}
i think you need to check that you click to the submit button then the validation
here is the code you need to change in your controller
public function change_pass() {
$this->load->helper('form');
$this->load->library('form_validation');
if ($this->input->post('submit')) {
$this->form_validation->set_rules('new_pass', 'New Password', 'required|matches[cnew_pass]');
$this->form_validation->set_rules('cnew_pass', 'Confirm Password', 'required|matches[new_pass]');
if ($this->form_validation->run() == FALSE) {
die(print_r("Always go here"));
}
}
$this->load->view('change_pass');
}
because of not checking is always false in validation
i think it should help you
Related
I am trying to add the form error messages in my html form. Problem is there in first view. In user function html is disappear from where I start to use form_error(). My Original form design is like this: my original form
but after adding the form_error under the first input: my form error image
Here is my html code
<div class="form-group">
<label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
<input type="text" name="username" id="name" placeholder="Your Name" />
<?php echo form_error('username','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
<div class="form-group">
<label for="email"><i class="zmdi zmdi-email"></i></label>
<input type="text" name="email" id="email" placeholder="Your Email"/>
<?php echo form_error('email','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
Here is my controller
<?php
class Register extends CI_Controller
{
public function user()
{
$this->load->view('files/registration/signup');
}
public function login()
{
$this->load->view('files/registration/login');
}
public function description()
{
$this->load->view('files/registration/description');
}
public function registerMe()
{
$this->load->helper('form','url');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|alpha|max_length[15]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('re_pass', 'Confirm Password', 'required|matches[pass]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run()===FALSE) {
$this->load->view('files/registration/signup');
}
else
{
$this->load->model('RegisterUser');
$this->RegisterUser->registerme($_POST);
}
}
}
?>
you need to load the form helper in signup function too. form_error is a function in form_helper. (this is required for other functions with a form view too)
public function user()
{
$this->load->helper('form');
$this->load->view('files/registration/signup');
}
html is disappeared because there was php error. check your error log when this happens or change codeigniter environment to development in index.php
after add form validation in my controllers
public function save($id_kecamatan='')
{
$this->form_validation->set_rules('nama_kecamatan','nama_kecamatan', 'required');
if ($this->form_validation->run() != FALSE){
$data = [
'nama_kecamatan' => $this->input->post('nama_kecamatan')
];
}
$simpan = $this->KecamatanModel->saveKecamatan($data);
redirect('admin/kecamatan/add');
}
this is my model, on the line $this->db->insert
public function saveKecamatan($data)
{
$this->db->insert('tbl_kecamatan', $data);
$id_kecamatan = $this->db->insert_id();
return true;
}
my view, when submit, appear You must use the "set" method to update an entry.
<?= validation_errors(); ?>
<form action="<?= site_url('admin/kecamatan/save');?>" method="POST">
<div class="box-body">
<div class="form-group">
<label class="control-label">Kecamatan</label>
<input type="text" class="form-control" name="nama_kecamatan" id="" placeholder="Nama Kecamatan">
</div>
<div class="form-actions">
<input type="hidden" name="id_kecamatan" value="">
<button type="submit" class="btn btn-success"><i class="fa fa-check"></i> Simpan</button>
<i class="fa fa-undo"></i> Batal
</div>
</form>
The validation is useless because it will not prevent empty value to be passed to
$this->db->insert('tbl_kecamatan', $data).
Try to prevent empty value by moving $simpan variable one line up :
public function save($id_kecamatan='')
{
$this->form_validation->set_rules('nama_kecamatan','nama_kecamatan', 'required');
if ($this->form_validation->run() != FALSE){
$data = [
'nama_kecamatan' => $this->input->post('nama_kecamatan')
];
$simpan = $this->KecamatanModel->saveKecamatan($data);
}
redirect('admin/kecamatan/add');
}
I am working with Codeigniter framework these days. I am trying to validate my form data using codeigniter's form_validation library. The problem is I cant get it to work. I dont know where the problem is no error is being shown.
Here is the view page I am calling controller core and function postad in form action
<div class="container table-responsive" id="con1">
<div class="row" id="h4_subad">
<div class="col-md-4"><h4>Submit an Ad</h4></div>
</div>
<form class="form-horizontal" role="form" method="get" action="/core/postad">
<table id="form_table" class="table-responsive table table-striped" id="table_form">
<td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>
<tr> <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
<option>Electronics</option>
<option>Mobiles & Accessories</option>
<option>Computers & Accessories</option>
<option>Kitchen Applicances</option>
<option>Clothing</option>
</select></td></tr>
<tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>
<tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
</table>
</form>
</div>
Controller for this view
class Core extends CI_Controller {
public function index() {
$this->load->view('header1');
$this->load->view('body');
$this->load->view('footer');
}
public function product() {
$this->load->view('header1');
$this->load->view('product');
$this->load->view('footer');
}
public function contact() {
$this->load->view('header1');
$this->load->view('contact');
$this->load->view('footer');
}
public function aboutus() {
$this->load->view('header1');
$this->load->view('aboutus');
$this->load->view('footer');
}
public function reg() {
$this->load->view('header1');
$this->load->view('reg');
$this->load->view('footer');
$this->input->get('username');
}
public function postad() {
$this->load->view('header1');
$this->load->view('postad');
$this->load->view('footer');
$this->load->model('insert_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('txt_area', 'Text Area', 'required|min_length[5]|max_length[15]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('postad');
}
else {
//Setting values for tabel columns
$data = array(
'description' => $this->input->post('txt_area'),
);
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('insert_view', $data);
}
}
}
Model for database connectivity
<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
$this->db->insert('post_ad', $data);
}
}
?>
Only trying it for just one field txt_area
I dont want to use codeigniter's form helper as I am using bootstrap form.
You should echo the validation_errors(); as below in your html . For more information you can vist this
<?php echo validation_errors(); ?>
<div class="row" id="h4_subad">
<div class="col-md-4"><h4>Submit an Ad</h4></div>
</div>
<form class="form-horizontal" role="form" method="get" action="/core/postad">
<table id="form_table" class="table-responsive table table-striped" id="table_form">
<td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>
<tr> <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
<option>Electronics</option>
<option>Mobiles & Accessories</option>
<option>Computers & Accessories</option>
<option>Kitchen Applicances</option>
<option>Clothing</option>
</select></td></tr>
<tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>
<tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
</table>
</form>
</div>
You need to use validation_errors(); in posted.php file (HTML) for display error message which you validate.
<?php echo validation_errors(); ?>
Form Validation User Manual
Your button to submit the form is incorrect. When you try to submit a form make sure to set the type to submit too.
<button type="submit">Submit form</button>
or
<submit>Submit form</submit>
I have tried everything I can think of but whenever I click submit the form passes on a null value, I dont know if it is the problem with the form or the controller or even the view. I changed this->input->post to posted data and i get an error of undefined variable posted data, please help.
Controller:
public function addmenu(){
$this->load->model('organizer_model');
$data = array(
'menu_name' => $this->input->post('menu name'),
'price' => $this->input->post('price'),
'email' => $this->session->userdata('email')
);
if($this->organizer_model->insertmenu($data)) {
$this->session->set_flashdata('message', 'Your menu has been added');
redirect('/menu/index', 'refresh');
} else {
$this->session->set_flashdata('message', 'Your menu was not added, please try again');
redirect('/menu/index', 'refresh');
}
View:
<form action="<?php echo site_url('Organizer/addmenu'); ?>" method="post" class="form-horizontal no-margin">
<div class="control-group">
<label class="control-label" for="menuname">
Menu Name
</label>
<div class="controls controls-row">
<input class="span3" name="data[menuname]" type="text" placeholder="Enter menu Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="price">
Price
</label>
<div class="controls controls-row">
<input class="span3" name="data[price]" type="text" placeholder="">
</div>
</div>
<div class="form-actions no-margin">
<button type="submit" name="submit" class="btn btn-info pull-right">
Add menu
</button>
<div class="clearfix">
</div>
</div>
</form>
Model:
public function insertmenu($data) {
$condition = "email = '" . $data['email'] . "'";
$this->db->select('organizer_id');
$this->db->from('organizer');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0){
array_pop($data); //will remove email from data
$row = $query->row();
$data['organizer_id'] = $row->organizer_id;
$this->db->insert('menu', $data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
I notice same question here codeigniter- insert data into db not working
Checks
Make sure you load your form helper and url helper.
Make sure you use form validation when submitting form in codeigniter on controller.
From this php user guide here http://php.net/manual/en/reserved.variables.post.php
Example on your input would be like person[0][first_name]
<form action="" method="">
<input type="text" name="data_posts[0][menu_name]" placeholder="Enter menu Name">
<input type="text" name="data_posts[0][price]" placeholder="">
</form>
Model
<?php
class Model_something extends CI_Model {
public function add_menu() {
$data_posts = $this->input->post('data_posts');
foreach ($data_posts as $data_post) {
$data = array(
'email' => $this->session->userdata('email'),
'menu_name' => $data_post['menu_name'],
'price' => $data_post['price']
);
$this->db->insert('tablename', $data);
}
}
}
Controller
<?php
class Add_menu extends CI_Controller {
public function index() {
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$data_posts = $this->input->post('data_posts');
foreach ($data_posts as $data_post) {
$this->form_validation->set_rules('data_posts['.$data_post.'][menu_name]', 'Menu Name', 'required');
$this->form_validation->set_rules('data_posts['.$data_post.'][price]', 'Price', 'required');
}
if ($this->form_validation->run() == FALSE) {
$this->load->view('some_view');
} else {
$this->load->model('model_something');
$this->model_something->add_menu();
redirect('to_success_page');
}
}
}
You could also check if has been inserted by using callback function
Codeigniter 3 user guide form validation http://www.codeigniter.com/user_guide/libraries/form_validation.html
Codeigniter 2 user guide form validation http://www.codeigniter.com/userguide2/libraries/form_validation.html
Also you should upgrade to the new bootstrap I see your using old version.
I try to set up a password in a codeigniter form...
Everything seems ok to my eyes but no matter which password I use the form is still submitted...
here is the code in the controler:
class MyBlog extends Controller{
function MyBlog(){
parent::Controller();
$this->load->helper(array('url','form','html')); //here we load some classes that we use
$this->load->scaffolding('entries'); //scaffolfing is a feature that lets you add or remove elements from the database
$this->load->scaffolding('comments');
$this->load->library('form_validation');//load validation class used to validate our forms...
}
function index(){
$data['title'] = "My Blog Title"; //the title of my blog
$data['query'] = $this->db->get('entries'); //here we make a small query to entries table
$this->load->view('myBlog_view', $data); ///load all data variables on myBlog_view.php
//this is also for the form validation
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('author', 'Author', 'required');
$this->form_validation->set_rules('pass', 'Pass', 'callback_pass_check');
function pass_check($str) {
if ($str == 'baywatch'){
return TRUE;
}
else{
return FALSE;
}
}
if ($this->form_validation->run() == TRUE)
{
$this->myBlog_insert();
//$this->load->view('formSuccess_view');
}
}
function myBlog_insert(){
$insert = array( 'title' => $_POST['title'],
'body' => $_POST['body'],
'author' => $_POST['author']
);
$this->db->insert('entries',$insert);
redirect('myBlog/');
}
}
and this is my form:
<div class="theForm">
<?php echo $this->form_validation->error_string; ?>
<?php echo validation_errors(); ?>
<?php echo form_open('myBlog'); ?>
<label for="title">Title:</label>
<input type='text' name="title" size="40" id="title" />
<p>
<label for="body">Body:</label>
<textarea name="body" rows = "10" cols="60" id="body"></textarea>
</p>
<p>
<label for="author">Author:</label>
<input type="text" name="author" size="40" id="author"/>
</p>
<p>
<label for="pass">Password:</label>
<input type="password" name="pass" size="38" id="pass"/>
</p>
<p><input type="submit" value="Submit New Post"/></p>
</form>
</div>
</body>
</html>
any ideas?
thanks in advance
<label for="pass">Password:</label>
<input type="text" name="pass" size="38" id="author"/>
The input type is text no password, the id='pass'.
Ok, a couple of things first:
1) id's should be unique. ie your author field and your password field shouldn't have the same id.
2) password fileds should use the type "password" not "text".
I think the reason you're having problems is with your callback function pass_check(). Try changing your function to:
function pass_check($pass)
{
if($pass !== 'baywatch')
{
return FALSE;
}
By the way, scaffolding has now been deprecated. Can I suggest you look into using models and the active record class as a way of interacting with your db? Also, this really isn't a very secure way of handling passwords. Have a look at some of the CI authentication libraries and see if you can implement one of them.
Ok guys...I found what the problem was...function pass_check was declared inside index()...and for some reason it needs to be outside as a method of the class...Hope this will help others... I give some ups for all the suggestions...