Hi when I use required in my rules in ci form_validation, I know the value of this required input post and don't show any message .but this if ($this->form_validation->run()) is always false.
thanks for your help.
my view:
<form>
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
</form>
my controller:
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
}
else redirect();
}
In your view file, you had wrote nmae except name
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
correct it.
please load again view file after false form_validation
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
$this->load->view('view_file','',true);
}
else redirect();
}
Related
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:
<?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 $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 $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 $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 $author->bio; ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
It's corresponding controller logic is this:
public function edit($id) {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$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->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
}
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>');
if($this->form_validation->run() === FALSE)
{
$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 problem I have not been able to solve is related to the validation of this form: if I leave a required filed empty, and try to submit the form, the form is loaded, with the proper validation error message, only the invalid field is populated with the data from the database and so, valid data is displayed along with the error message, as seen in the form below:
However, as long as the field's current value is not replaced with an invalid one, I want it (the field value) to come from the database.
How can I solve this issue?
If you want to keep the submitted data on an input value, you could modify the email codes like this :
<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>
This will set the submitted data as the email value (if available), or email data from the database if there is no submitted email data.
I trying to create a basic blog application. I have auto-loaded all helpers needed but every the form_validation check always returns false and does not display any errors.
Controller
class Posts extends CI_Controller {
public function create()
{
$data['title'] = 'Create Post';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
$this->Post_model->create_post();
redirect('posts');
}
}
}
View
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('posts/create'); ?>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title" placeholder="Add Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea class="form-control" name="body" placeholder="Add Body</textarea>
</div>
<input type="submit" value="Add" class="btn btn-secondary">
<?php echo form_close(); ?>
It works for me with one minor change: check your textarea tag. It's not closed properly.
I have email form in my codeigniter, it is sending e-mails successfully i just cannot make it refresh page when the e-mail will be sent, if I send email and then refresh page manually it writes your e-mail has been sent successfully which is written in flashdata in my language, which means that everything is okay I just can not refresh page automatically, please help.
This is my contact Controller :
class Contact extends CI_Controller {
public function index()
{
$this->load->library('session');
$this->load->library('email');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['title'] = "Contact";
$this->load->view('templates/header', $data);
$this->load->view('contact', $data);
$this->load->view('templates/footer', $data);
}
public function sendmail1(){
$this->load->library('email');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$name = $this->input->post('contact-name');
$email = $this->input->post('contact-email');
$subject = $this->input->post('contact-subject');
$phone = $this->input->post('contact-phone');
$message = $this->input->post('contact-message');
$this->form_validation->set_rules('contact-name', 'სახელი', 'trim|required');
$this->form_validation->set_rules('contact-email', 'ელ-ფოსტა', 'trim|required');
$this->form_validation->set_rules('contact-phone', 'ტელეფონი', 'trim|required');
$this->form_validation->set_rules('contact-subject', 'წერილის თემა', 'trim');
$this->form_validation->set_rules('contact-email', 'ელ-ფოსტა', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('warmatebulia', '<h5 style="color: red;">თქვენი წერილის გაგზავნა ვერ მოხერხდა.</h5>');
// after storing i redirect it to the controller
redirect(base_url().'contact', 'refresh');
return FALSE;
}
else {
$this->email->from($email, $name);
$this->email->to('info#mymail.com');
$this->email->subject($subject);
$this->email->message('ტელეფონის ნომერი:'.$phone.'<br />'.$message);
$this->email->send();
$this->session->set_flashdata('warmatebulia', '<h5 style="color: green;">თქვენი წერილი წარმატებით გაიგზავნა, მადლობა.</h5>');
// after storing i redirect it to the controller
redirect('', 'refresh');
}
}
}
This is my form on contact view page :
<?php echo $this->session->flashdata('warmatebulia'); ?>
<?php
$attributes = array('class' => 'form-message', 'id' => 'quote-contact-request');
echo form_open('contact/sendmail1', $attributes);
?>
<div class="form-results"></div>
<div class="form-group row">
<div class="form-field col-md-6 form-m-bttm">
<input name="contact-name" type="text" placeholder="სახელი *" class="form-control required">
</div>
<div class="form-field col-md-6">
<input name="contact-email" type="email" placeholder="ელ-ფოსტა *" class="form-control required">
</div>
</div>
<div class="form-group row">
<div class="form-field col-md-6 form-m-bttm">
<input name="contact-phone" type="text" placeholder="ტელეფონის ნომერი*" class="form-control required">
</div>
<div class="form-field col-md-6">
<input name="contact-service" type="text" placeholder="წერილის თემა" class="form-control">
</div>
</div>
<div class="form-group row">
<div class="form-field col-md-12">
<textarea name="contact-message" placeholder="წერილი *" class="txtarea form-control required"></textarea>
</div>
</div>
<input type="submit" name="submit" value="გაგზავნა" class="btn solid-btn sb-h">
<?php echo form_close(); ?>
If you need to flash a success message in your contact page itself, you just redirect to index() function of your 'Contact' controller. So please do like this
$this->session->set_flashdata('warmatebulia', '<h5 style="color: green;">თქვენი წერილი წარმატებით გაიგზავნა, მადლობა.</h5>');
// after storing i redirect it to the controller
redirect('contact', 'refresh');
I know most of you guys will realize this is a dumb question but I don't have any idea what's wrong with my code. I'm trying to create a contact form that will be sent into the database. But when I try to go to the contact_form_view.php it says Fatal error: Call to undefined function form_open() on Line 104
Here's the form code
<?php $attributes = array("name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('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 set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-success">Submit</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
Here's the php code
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
if ($this->db->insert('contacts', $data))
{
// success
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
if you please try to load form helper in your autoload.php like this
$autoload['helper'] = array('url', 'form');
i think you load the helper in the controller which have the action of your form not the controller that call the form.
so just add the helper to autoload.php and it will work
My code so far is working and changing the user password. I would now like to display the user that their password was changed successfully else say try again but i cant make it happen. Any help would be appreciated, thanks.
Here is the controller:
public function change_password($arg = "admin")
{
if(isset($_POST['data']) && !empty($_POST['data']))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('data[oldpassword]', 'old password', 'required');
$this->form_validation->set_rules('data[password]', 'password', 'trim|required');
$this->form_validation->set_rules('data[passconf]', 'password Confirmation', 'trim|required|md5|matches[data[password]]');
if ($this->form_validation->run() == FALSE)
{
$data['requestee'] = $arg;
$this->load->view('change_password', $data);
}
else
{
$data = $_POST['data'];
array_pop($data);
$data['requestee'] = $arg;
$data['email'] = $this->session->userdata('email');
//print_r($data); exit;
if($data['requestee'] == "admin")
{
$this->load->model('login_model');
if($this->login_model->updatepassword($data))
{
redirect('/admin/show', 'refresh');
}
else
{
redirect('login/change_password/');
}
}
elseif($data['requestee'] == "org")
{
$this->load->model('login_model');
if($this->login_model->updatepassword($data))
{
redirect('/organizer/show', 'refresh');
}
else
{
redirect('login/change_password/org');
}
}
else
{
echo "could not change password";
}
}
}
else
{
$data = array('requestee' => $arg);
$this->load->view('change_password', $data);
}
}
Here is the view:
<form action="<?php echo site_url('login/change_password'); ?>" class="login-wrapper" method="post">
<div class="header">
<div class="row-fluid">
<div class="span12">
<h3>Change password<img src="<?php echo asset_url();?>img/logo1.png" alt="Logo" class="pull-right"></h3>
<p>Fill out the form below to change your password.</p>
</div>
</div>
</div>
<div class="content">
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[oldpassword]" placeholder="Old password" required="required" type="password" value="<?php echo set_value('oldpassword'); ?>">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[password]" placeholder="New password" required="required" type="password" value="<?php echo set_value('password'); ?>">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[passconf]" placeholder="Confirm new password" required="required" type="password" value="<?php echo set_value('passconf'); ?>" >
</div>
</div>
</div>
<div class="actions">
<input class="btn btn-danger" name="change_password" type="submit" value="Change" >
<div class="clearfix"></div>
</div>
</form>
It appears that after changing the users password, you are using a redirect to return the user to another page.
In your controller, just before doing the redirect, You can use CodeIgniters session data to set a message in flashdata like so:
$this->session->set_flashdata('message', 'Your Password Was Successfully Changed');
or
$this->session->set_flashdata('message', 'Your Password Was Not Changed');
Then in your view, you can add:
<?php echo $this->session->flashdata('message'); ?>
Please note that in order for this to work, if you have not done so already, you will need to either manually load the session library on every page where it is used or add it to your autoload.php file.