In CodeIgniter, after submitting form, when we get POST data in controller function of form action, I am checking for form_validation. But if form_validation checking returns false, I want to redirect the user to view and show him the form's filled values. I have tried with redirect()
, but I can't get the form's posted data to view after redirect.
My View File Code =>
<div class="form-group">
<label class="control-label" for="firstname">First Name<span class="required">*</span></label>
<div class="controls">
<?php echo form_input(array('type'=>'text','class'=>'form-control','name'=>'firstname','id'=>'firstname','placeholder'=>'First Name','required'=>'required')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label" for="lastname">Last Name<span class="required">*</span></label>
<div class="controls">
<?php echo form_input(array('type'=>'text','class'=>'form-control','name'=>'lastname','id'=>'lastname','placeholder'=>'Last Name','required'=>'required')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label" for="address">Address<span class="required">*</span></label>
<div class="controls">
<?php echo form_textarea(array('class'=>'form-control','name'=>'address','id'=>'address','placeholder'=>'Address','required'=>'required')); ?>
</div>
</div>
My Controller File Code =>
//Getting value
$firstname = trim($this->input->post('firstname'));
$lastname = trim($this->input->post('lastname'));
$address = trim($this->input->post('address'));
// SET VALIDATION RULES
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
if( $this->form_validation->run() )
{
//insert into database function
}
else
{
//Here, If validation I want to pass my form's filled data to view file i.e. refill form after redirect.
//I have tried using below, but I can't get filled form in view file after redirect.
$this->data['fname'] = $firstname;
$this->data['lname'] = $lastname;
$this->data['address'] = $address;
$this->session->set_flashdata('error',validation_errors());
redirect('employee/add', 'refresh');
}
So please let me know how to get all form data refilled after redirect. Thanks.
In CodeIgniter, If you are using redirect()
function, then you can't access your form data using $this->data['anyname']
.
But, If you want to access form data you can get it as below.
redirect('employee/edit/'.$firstname.'/'.$lastname.'/'.$address, 'refresh');
then in view file, you can get data using $firstname = $this->uri->segment(3)
like that. but this method is not convenient for big forms.
So to get all posted data to view after redirect, you have to use CI's session variable set_flashdata
as below.
In Controller file,
if( $this->form_validation->run() )
{
//insert into database function
}
else
{
//here $this->input->post() is your posted form data. pass it to set_flashdata.
$this->session->set_flashdata('formdata',$this->input->post());
$this->session->set_flashdata('error',validation_errors());
redirect('employee/add', 'refresh');
}
Then in View file, get flashdata in variable as below. just put below code above your Form's starting tag.
<?php
if( $this->session->flashdata('formdata') )
{
//echo "<pre>";
//print_r($this->session->flashdata('formdata'));
//die();
$formdata = $this->session->flashdata('formdata');
}
?>
And access this $formdata in input field's value using isset as below. Set 'value' of each input field.
<?php echo form_input(array('type'=>'text','class'=>'form-control','name'=>'firstname','id'=>'firstname','placeholder'=>'First Name','required'=>'required','value'=>isset($formdata)?$formdata['firstname']:'')); ?>
So everytime, when there is an form_validation error, you will be redirected to the View, and you will get form's all posted data filled in Input fields in view file.
Google PHP Api Client - I keep getting Error 401: UNAUTHENTICATED
Getting data from nested fields using mongoDB and php driver manager
How to update photo in Active Directory using PHP ldap_modify
Turning off multiple WordPress menu items for email addresses
I have developed an external module within an existing applicationI want to make this module more or less reusable
When querying globals from the GraphiQl interface, from my frontend application, or from an API explorer like Insomnia, CraftQL returns an error Trying to get property of non-object
I have a html fieldset and a short php file for sending an email via sendgrid when the submit button is clicked