I am working with codeigniter. I have created one function to add university form.
function add_uni_admin()
{
$user_id = $this->session->userdata("user_id");
if (!empty($user_id)) {
$uni_name = $this->input->post("uni_name");
$uni_image = $this->input->post("uni_image");
$uni_email = $this->input->post("uni_email");
$phn_no = $this->input->post("phn_no");
$m_no = $this->input->post("m_no");
$address = $this->input->post("address");
$password = $this->input->post("pass");
$this->form_validation->set_rules('uni_name', 'University Name', 'required');
$this->form_validation->set_rules('uni_email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('phn_no', 'Phone Number', 'required');
$this->form_validation->set_rules('m_no', 'Mobile Number', 'required');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('pass', 'Password', 'required|matches[conf_pass]');
$this->form_validation->set_rules('conf_pass', 'Confirm Password', 'required');
if ($this->form_validation->run() == false) {
$this->load->view("admin/add_uni_admin");
} else {
echo "successfull";exit;
}
} else {
redirect(base_url());
}
}
When I submit the form without filling any field then it does not give any error.Here I have added form_validation library into autoload.php file and added form helper in it.
So what code should I have to write to display errors?
use echo validation_errors() in your view to display errors.
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
**your form here.**
</body>
</html>
Add $this->load->library('form_validation');
in your controller function..
And <?php echo validation_errors(); ?>
in your view file to display errors.
Related
Here is my login method inside the controller, Here I am setting a flash message for the validation errors -
public function login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[3]');
if($this->form_validation->run() == FALSE){
$data = array(
'errors' => validation_errors()
);
$this->session->set_flashdata($data);
redirect('home');
}
}
Here id the code to display those errors -
<?php if($this->session->flashdata('errors')): ?>
<?php echo $this->session->flashdata('errors');?>
<?php endif; ?>
I don't know what is wrong, Error message is not displaying.
I hope work this process
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('name','Your message');
redirect('home');
}
<?php if($this->session->flashdata('name')): ?>
<?php echo $this->session->flashdata('name');?>
<?php endif; ?>
Its placed in controller
if ($this->form_validation->run() == FALSE) {
$errors = validation_errors();
$this->session->set_flashdata('form_error', $errors);
$this->load->view('Your View page');
}
Its placed in your view page
<?php if($this->session->flashdata('form_error')): ?>
<?php echo $this->session->flashdata('form_error');?>
<?php endif; ?>
I hope this it will help to you..!
I need to redirect the url with hashtag in Codeigniter3. There is no error but its not redirect with hashtag. Please help me to fix it.
http://localhost:8888/CodeIgniter/index/aboutus#aboutusredirect
Controller.
public function aboutus()
{
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('include/header.php');
$this->load->view('about');
$this->load->view('include/footer.php');
}
else
{
$this->load->view('profilecreate');
}
}
Inside the if condition change
$this->load->view('include/header.php');
$this->load->view('about');
$this->load->view('include/footer.php');
use redirect function like below:
redirect('index/aboutus#aboutusredirect');
you can use required_once() function in 'about' view and then use redirect() for go another page.
you change about view like this :
required_once('include/header.php')
//html tag in about view
required_once('include/footer.php')
and then you're code must be like this :
public function aboutus()
{
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('about');
}
else
{
$this->load->view('profilecreate');
}
}
I had this form working and to be honest I can not figure out what I might have done. The form will not submit now and I don't see any form validation errors.
controller:
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="ncerror" >', '</div>');
$this->form_validation->set_rules('media_consent', 'Media Consent', 'required');
$this->form_validation->set_rules('aic_name', 'Name', 'required');
$this->form_validation->set_rules('aic_phone', 'Cell phone', 'required');
$this->form_validation->set_rules('aic_email', 'Email', 'required');
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
I have
<?php echo "errors" . validation_errors();
at the top of my form. After submitting form, the form reloads with "errors" displayed but no other output from the validation_errors function. Any help with troubleshooting?
should this line
$this->load->view('ncform', $this->ncformdata());
be this?
$this->load->view('ncform', $data);
You have a condition when
$this->form_validation->run() == FALSE
but I can not see any condition for when the form validation returns TRUE. Try adding a condition when
$this->form_validation->run() == TRUE
Example:
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
else
{
echo 'There are no form validation errors!.';
}
This is the controller code for signing up:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index(){
$data = array(
'title' => 'SignUp Page'
);
$this->load->view('header', $data);
$this->load->view('signup');
}
public function signup_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('salutation', 'Salutation', 'required');
$this->form_validation->set_rules('fName', 'First Name', 'required|trim|alpha|xss_clean|strip_tags');
$this->form_validation->set_rules('lName', 'Last Name', 'required|trim|alpha|xss_clean|strip_tags');
if($this->form_validation->run()){
$month = $this->input->post('months');
$day = $this->input->post('days');
$year = $this->input->post('years');
$birthday = date("d-m-Y",mktime(0,0,0,$month,$day,$year));
}
$this->form_validation->set_rules('address', 'Address', 'required|trim|xss_clean|strip_tags|min_length[10]');
$this->form_validation->set_rules('contact', 'Contact No', 'required|trim|xss_clean|strip_tags|numeric|min_length[8]|max_length[8]');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]|xss_clean|strip_tags');
$this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|xss_clean|strip_tags|min_length[3]|max_length[20],is_unique[users.username');
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|xss_clean|strip_tags|alpha_numeric');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]|xss_clean|strip_tags|alpha_numeric');
if ($this->form_validation->run()){
$key = md5(uniqid());
$this->load->library('email', array('mailtype'=>'html'));
$this->load->model('model_users');
$this->email->from('admin#snt.website', "SWAP");
$this->email->to($this->input->post('email'));
$this->email->subject("Confirm your account.");
$message = "<p>Thank you for signing up!</p>";
$message .= "<p><a href='".base_url()."main/register_user/$key'>Click Here</a> to confirm your account</p>";
$this->email->message($message);
if ($this->model_users->add_temp_users($key, $birthday)){
if ($this->email->send()){
echo "The email has been sent!";
} else echo "Could not send the mail";
} else echo "problem adding to database";
} else {
$this->index;
}
}
}
I have checked through my codes and have no idea why does this error keep showing up:
If i am to remove all the validations it will work perfectly fine.Can someone help me with this problem please?
I think you wanted to write:
$this->index();
//^^ So you actually call the function index and not the property
instead of:
$this->index;
I have function like this:
function gi_insert()
{
if(!IS_AJAX){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags');
$this->form_validation->set_rules('body', 'Tekst', 'trim|required|strip_tags');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|strip_tags|valid_email');
$this->form_validation->set_rules('tag', 'Tagovi', 'trim|required|strip_tags');
if(isset($_POST['category_new'])){
$this->form_validation->set_rules('category_new', 'Kategorija', 'trim|strip_tags');
}
$p = $this->uri->segment(3);
if ($this->form_validation->run() == FALSE)
{
$errors = validation_errors();
redirect("admin/create/$p", 'location');
}
else
{
$this->gi->gi_insert();
redirect('admin/pregled/' . $this->uri->segment(3));
}
} else
{
$this->gi->gi_insert();
}
}
How can I send validation errors to admin/create controller? At the moment, this is working, but I don't get error report (page where errors should appear is containig
<?php echo validation_errors(); ?>
)
validation_errors() only works if the $_POST data is still set, since you redirect to admin/create, $_POST is empty and the function no longer works, returning nothing.
You could send the errors with a piece of flashdata to the page.