PHP , CODEIGNITER how to add from controller with hidden view - php

I have problem that my input "type" is never post in database because I don't give input type in the view (I want value of my type is set by controller/hidden)... and this is my code
Controller :
<?php
public function addSmsCampaign() {
if (isset($_POST['addSmsCampaign'])) {
$this->form_validation->set_rules('campaign_name', 'campaign name', 'required|is_unique[campaigns.campaign_name]');
$this->form_validation->set_rules('sequence_qty', 'sequence quantity', 'required|integer');
$this->form_validation->set_rules('label_id', 'label id', 'required');
$this->form_validation->set_rules('type', '', 'required');
//if form validation true
if ($this->form_validation->run() == TRUE) {
$sms = 1;
$newcampaign = [
'campaign_name' => $_POST['campaign_name'],
'sequence_qty' => $_POST['sequence_qty'],
'label_id' => $_POST['label_id'],
'type' => $this->input->post('type'),
'created_at' => date('Y-m-d')
];
$this->db->insert('campaigns', $newcampaign);
redirect('userCont/sequenceform', 'refresh');
}
}
?>
and this is my view:
<form action="" method="POST">
<div class="form-group">
<label for="campaign_name">Input Campaign Title </label>
<input type="text" class="form-control" name="campaign_name" id="name">
</div>
<div class="form-group">
<label for="sequence_qty">Sequence qty </label>
<input type="text" class="form-control" name="sequence_qty" id="qty">
<input type="hidden" class="form-control" name="type" value="1">
</div>
<div class="form-group">
<label for="label_id">Choose Category</label>
<select class="form-control" name="label_id" id="label_id">
<?php
foreach ($label_content as $e) {
echo "<option value='$e->id;'>" . $e->label_name . "</option>";
}
?>
</select>
</div>
<div class="text-right">
<button class="btn btn-primary form-control" value="1" name="addSmsCampaign type">next</button>
</div><hr>
</form>
every time I post value of type is default = 0, and I want set value to 1...
thanks a lot

try changing name from 'type' to some other name in db, controller and view

Related

One page CRUD form operation (Laravel6)

I have a table in my database with contact details. On my form, I have a Dropdown which displays the names of people, and which, once I click on the name, fills in the fields with the data (ajax).
The problem happens when I want to perform operations on the form data.
For example, I click on "M Bram Manu" (id = 1) in the Dropdown. My Ajax will fill out my form with the details of this contact.
But when I want to update, delete or add a new contact, it doesn't work properly.
So, if I click on "delete", it not delete the contact I choose but the last contact of the dropdown list. Why???
So I think my link retrieves the last ID from my dropdown, so I decided to put an hidden Input with the value of the contact ID that appears in the form. But I don't know how to retrieve this value to define it as the ID to send to the controller.
Dropdown bar and link button :
<form class="col-md-9">
<label class="col-md-2">Rechercher : </label>
<select class="form-control select2 col-md-7" id="selInscrit" name="selInscrit" onchange="selectID()">
<option value="0" selected="selected"></option>
#foreach($inscrit as $inscrits)
<option value="{{$inscrits->INS_ID}}">{{$inscrits->INS_CIVILITE}} {{$inscrits->INS_NOM}} {{$inscrits->INS_PREN}} {{$inscrits->INS_NUM_RUE}} {{$inscrits->INS_Rue}} </option>
#endforeach
</select>
</form>
<div class="btn-group btn-group-sm col-md-3" role="group">
<form action="" method="GET">
<button type="submit" class="btn btn-secondary btn-sm">Edit</button>
</form>
<button type="submit" form="registerForm" formmethod="POST" formaction="{{ route('inscrits.store') }}" class="btn btn-secondary btn-sm">Save</button>
<button type="submit" form="registerForm" formmethod="POST" formaction="{{ route('inscrits.update') }}" class="btn btn-secondary btn-sm">Update</button>
<form action="{{ route('inscrits.destroy') }}" method="POST">
<input id="INS_ID" name="INS_ID" value="" type="hidden">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-secondary btn-sm">Delete </button>
</form>
</div>
Ajax for dropdown :
<script>
$('#selInscrit').change(function() {
var id = $(this).val();
var url = '{{ route("inscrits.show", ":id") }}';
url = url.replace(':id', id);
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function(response) {
if (response != null) {
$('#INS_ID').val(response.INS_ID);
$('#INS_CIVILITE').val(response.INS_CIVILITE);
$('#INS_NOM').val(response.INS_NOM);
$('#INS_PREN').val(response.INS_PREN);
$('#INS_NAISS').val(response.INS_NAISS);
$('#INS_AGE').val(response.INS_AGE);
$('#INS_NUM_RUE').val(response.INS_NUM_RUE);
$('#INS_Rue').val(response.INS_Rue);
$('#INS_TEL1').val(response.INS_TEL1);
$('#INS_OBS').val(response.INS_OBS);
$('#INS_DATE').val(response.INS_DATE);
$('#INS_TEL2').val(response.INS_TEL2);
}
}
});
});
Route :
// INSCRITS
/ route for index
Route::get('/inscrits', 'InscritController#index')->name('inscrits.index');
// route for dropdown bar
Route::get('/inscrits/show/{id}', 'InscritController#show')->name('inscrits.show');
// route for update
// Route::match(['put', 'patch'], '/inscrits/update','InscritController#update')->name('inscrits.update');
Route::post('/inscrits/update','InscritController#update')->name('inscrits.update');
// route for store
Route::post('/inscrits/store', 'InscritController#store')->name('inscrits.store');
//route for delete
Route::delete('/inscrits/destroy', 'InscritController#destroy')->name('inscrits.destroy');
Form :
<form id="registerForm">
#csrf
<div class="form-group row">
<div class="col-lg-1">
<label for="INS_CIVILITE">Civilité</label>
<select class="form-control form-control-sm" id="INS_CIVILITE" name="INS_CIVILITE">
<option value="" selected="selected"></option>
<option value="Mme">Mme</option>
<option value="Mlle">Mlle</option>
<option value="M.">M.</option>
</select>
</div>
<div class="col-lg-4">
<label for="INS_NOM">Nom</label>
<input class="form-control form-control-sm" id="INS_NOM" name="INS_NOM" value="" type="text">
</div>
<div class="col-lg-3">
<label for="INS_PREN">Prénom</label>
<input class="form-control form-control-sm" id="INS_PREN" name="INS_PREN" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_NAISS">Année Naiss</label>
<input class="form-control form-control-sm" id="INS_NAISS" name="INS_NAISS" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_AGE">Age</label>
<input class="form-control form-control-sm" id="INS_AGE" name="INS_AGE" value="" type="text">
</div>
</div>
<div class="form-group row">
<div class="col-lg-1">
<label for="INS_NUM_RUE"># Rue</label>
<input class="form-control form-control-sm" id="INS_NUM_RUE" name="INS_NUM_RUE" value="">
</div>
<div class="col-lg-9">
<label for="INS_Rue">Libellé voie</label>
<select class="form-control form-control-sm" id="INS_Rue" name="INS_Rue">
#foreach($rue as $rues)
<option value="{{$rues->RUE_NUM}}">{{$rues->RUE_NOM}} ({{$rues->RUE_Type}})</option>
#endforeach
</select>
</div>
<div class="col-lg-2">
<label for="INS_TEL1">Téléphone 1</label>
<input class="form-control form-control-sm" id="INS_TEL1" name="INS_TEL1" value="" type="text">
</div>
</div>
<div class="form-group row">
<div class="col-lg-8">
<label for="INS_OBS">Observation</label>
<input class="form-control form-control-sm" id="INS_OBS" name="INS_OBS" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_DATE">Date d'inscription</label>
<input class="form-control form-control-sm" id="INS_DATE" name="INS_DATE" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_TEL2">Téléphone 2</label>
<input class="form-control form-control-sm" id="INS_TEL2" name="INS_TEL2" value="" type="text">
</div>
</div>
<input id="INS_LIEU" name="INS_LIEU" value="WEB" type="hidden">
<input id="INS_EID" name="INS_EID" value="" type="hidden">
</form>
The controller :
public function index()
{
$inscrit = Inscrit::all();
return view('index', compact('inscrit'));
}
public function store(Request $request)
{
$storeData = $request->validate([
'INS_CIVILITE' => 'max:15',
'INS_NOM' => 'max:50',
'INS_PREN' => 'max:50',
'INS_NUM_RUE' => 'max:8',
'INS_TEL1' => 'max:10',
'INS_TEL2' => 'max:10',
'INS_AGE' => 'numeric',
'INS_OBS' => 'max:255',
'INS_Rue' => 'max:255',
'INS_DATE' => 'max:255',
'INS_NAISS' => 'max:255',
]);
$inscrit = Inscrit::create($storeData);
return redirect('/inscrits')->with('completed', 'Nouvel inscrit !');
}
public function show($id = 0)
{
$data = Inscrit::where('INS_ID', $id)->first();
return response()->json($data);
}
public function edit($id)
{
$inscrit = Inscrit::findOrFail($id);
return view('index', compact('inscrit'));
}
public function update(Request $request, $id)
{
$updateData = $request->validate([
'INS_CIVILITE' => 'max:15',
'INS_NOM' => 'max:50',
'INS_PREN' => 'max:50',
'INS_NUM_RUE' => 'max:8',
'INS_TEL1' => 'max:10',
'INS_TEL2' => 'max:10',
'INS_AGE' => 'numeric',
'INS_OBS' => 'max:255',
'INS_Rue' => 'max:255',
'INS_DATE' => 'max:255',
'INS_NAISS' => 'max:255',
]);
$id = request()->input('INS_EID');
Inscrit::where('INS_ID', $id)->update($updateData);
return redirect('/inscrits')->with('completed', 'inscrit mis à jour');
}
public function destroy($id)
{
$id = request()->input('INS_ID');
$inscrit = Inscrit::where('INS_ID', $id)->delete();
return redirect('/inscrits')->with('completed', 'inscrit supprimé');
}
The form
Database
Something like :
public function edit ($id) {
$inscritContent = Inscrit::where('id', $id)->firstOrFail();
return view('admin.inscrit.edit.form', [
'content' => $iscritContent
]);
}
public function update($id, Request $req) {
$inscrit = Inscrit::findOrFail($id);
$inscrit->update($req->all());
return redirect(route('admin.inscrit.index'));
}
admin.inscrit.edit.form and admin.inscrit.index are blade template

CodeIgniter -> Submitting a form into database

Creating my own application to learn CodeIgniter, by following CI documentation, i can't post my data into db.
I'm using create.php:
<p class="h4 mb-4 text-center"><?= $title ?></p>
<label>Username</label>
<input type="text" id="textInput" class="form-control mb-4" placeholder="Your username" name="username">
<label for="textInput">Title</label>
<input class="form-control mb-4" placeholder="Your title" name="title">
<label>Your message</label>
<textarea class="form-control mb-4" placeholder="Your message content" name="body"></textarea>
<div class="row">
<div class="col-8">
<select class="browser-default custom-select mb-4" id="select" name="genre">
<option selected="">Choose your Genre</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
<div class="col-2">
<input class="form-control mb-4" placeholder="Your age" name="age">
</div>
</div>
<select class="browser-default custom-select mb-4" name="platform">
<option value="" disabled="" selected="">Choose your Platform</option>
<option value="1">Snapchat</option>
<option value="2">Kik</option>
<option value="3">Instagram</option>
<option value="4">Telegram</option>
</select>
<button class="btn btn-info my-4 btn-block" type="submit">Sign up</button>
<div class="text-center">
<p>By clicking
<em>Sign up</em> you agree to our
terms of use.
</p>
</div>
This is the controller:
public function create(){
$data['title'] = 'Add your Username';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('genre', 'Genre', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('age', 'Age', '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');
}
}
This is the model:
public function create_post(){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'username' => $this->input->post('username'),
'genre' => $this->input->post('genre'),
'age' => $this->input->post('age'),
'platform' => $this->input->post('platform'),
);
return $this->db->insert('posts', $data);
}
I've added as well in autoload (libraries->form_validation) and (helper->form).
I've already tested everything before those codes, and it was working fine. The problem is that those data in HTML i want to send them into db, but even validator works on blank forms (submitting without content) or inserting content, it's just realod the page, as it should redirecting it to posts url.
Someone can help me?
where your post method in view ?
Ex :
<form method="post" name="from_submit" action="">
You forgot to add form
<form method="post" name="myform" action="">
//Your form code---
</form>

upload more files to a folder with laravel

I try to upload multiple files to an specific folder using laravel. I was able to do this with just one file. But after I tried to upload multiple files it didn't work. First of all I tried to make an foreach. But it didn't work I tried to find solutions on the internet but unfortunately I didn't find one. Can anybody help me?
public function store(Request $request) {
// dd(request()->all());
$this->validate(request(), [
'name' => 'required|unique:lessons|max:20',
'type' => 'required',
'description' => 'required',
'price' => 'required',
'main_picture' => 'required',
'pictures' => 'required'
]);
$input = $request->all();
$images= array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('lesson images',$name);
$images[]=$name;
}
}
$lesson = Lesson::create( [
'name' =>$input['name'],
'type' => $input['type'],
'description' =>$input['description'],
'price' => $input['price'],
'main_picture' =>$input['main_picture'],
'pictures'=> implode("|",$images),
]);
$image_name = $request->file('main_picture')->getClientOriginalName();
$id = $lesson->id;
if($request->hasFile('main_picture')) {
$file = $request->file('main_picture');
$file->move('lesson images', $id . "-main-" . $image_name);
}
return redirect('/lessenoverzicht');
}
HTML:
<form method="post" enctype="multipart/form-data" action="/lessenoverzicht/toevoegen">
{{ csrf_field() }}
<div class="form-group">
<label for="name"><span class='required'>*</span>Naam les:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Voer naam les in..">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1"><span class='required'>*</span>Type</label>
<select name='type' class="form-control" id="type">
<option value="" disabled selected>Select your option</option>
<option value='les'>Les</option>
<option value='workshop'>Workshop</option>
</select>
</div>
<div class="form-group">
<label for="description"><span class='required'>*</span>Omschrijving:</label>
<textarea class="form-control" id="description" name="description" rows="10" placeholder="Voer omschrijving in..."></textarea>
</div>
<label for="description"><span class='required'>*</span>Prijs:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">€</span>
</div>
<input type="number" value="0.00" min="0" step="1.00" class="form-control" id='price' name='price'>
</div>
<div class="form-group">
<label for="main_picture"><span class='required'>*</span>Main Image:</label>
<input type="file" class='form-control' id='main_picture' name="main_picture">
</div>
<div class="form-group">
<label for="picture">Afbeelding(en) omschrijving:</label>
<input type="file" class='form-control' id='picture' name="pictures" multiple>
</div>
<div class="form-group">
#include('layouts.errors')
</div>
<button type="submit" class="btn btn-primary">Toevoegen</button>
</form>
Can you try the below change in your code? I have only added Array type name property name="pictures[]"
<input type="file" class='form-control' id='picture' name="pictures[]" multiple>
Sample Code:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection:
<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>
PHP logic
public function store(request $request) {
$input=$request->all();
$images=array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
/*Insert your data*/
Detail::insert( [
'images'=> implode("|",$images),
'description' =>$input['description'],
//you can put other insertion here
]);
return redirect('redirecting page');
}

I am new to Codeigniter can any one help me how to insert data into mysql? [duplicate]

This question already has answers here:
insert data into database with codeigniter
(9 answers)
Closed 6 years ago.
I am learning Codeigniter i want to insert data into database. How can i perform such task?
This is my Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class PostJobs extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Post_Model');
}
public function index(){
$this->load->helper(array('form', 'url', 'email'));
$this->load->view('postjobs');
}
public function post(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email',
array(
'required' => 'enter email id'));
$this->form_validation->set_rules('JTitle', 'Job Title', 'trim|required|callback__check_length[6]',
array(
'required' => 'enter JOB TITLE'));
$this->form_validation->set_rules('JType', 'Job Type', 'trim|required',
array(
'required' => 'select job type'));
$this->form_validation->set_rules('Salary', 'Salary', 'trim|required',
array(
'required' => 'enter salary'));
$this->form_validation->set_rules('Exp', 'Experience', 'trim|required',
array(
'required' => 'select Experience'));
$this->form_validation->set_rules('Skills', 'Skills', 'trim|required',
array(
'required' => 'enter skills'));
$this->form_validation->set_rules('EduReq', 'Education required', 'trim|required',
array(
'required' => 'enter EDUCATION'));
$this->form_validation->set_rules('JLoc', 'Location', 'trim|required',
array(
'required' => 'enter LOCATION'));
$this->form_validation->set_rules('CName', 'Company Name', 'trim|required',
array(
'required' => 'enter COMPANY NAME'));
$this->form_validation->set_rules('CAdd', 'Company Address', 'trim|required',
array(
'required' => 'enter COMPANY ADDRESS'));
$this->form_validation->set_rules('JDesc', 'Job Description', 'trim|required',
array(
'required' => 'enter JOB DESCRIPTION'));
if($this->form_validation->run() ==FALSE){
$this->load->view('postjobs');
}
else{
$data = array(
'email' => $this->input->post('email'),
'JTitle' => $this->input->post('JTitle'),
'JType' => $this->input->post('JType'),
'Salary' => $this->input->post('Salary'),
'Exp' => $this->input->post('Exp'),
'Skills' => $this->input->post('Skills'),
'EduReq' => $this->input->post('EduReq'),
'JLoc' => $this->input->post('JLoc'),
'CName' => $this->input->post('CName'),
'CWeb' => $this->input->post('CWeb'),
'CAdd' => $this->input->post('CAdd'),
'JDesc' => $this->input->post('JDesc')
);
$this->Post_Model->insert_postjob($data);
$this->load->view('postjobs');
}
}
function _check_length($input, $min){
$length = strlen($input);
if ($length >= $min){
return TRUE;
}
elseif ($length < $min){
$this->form_validation->set_message('_check_length', 'Minimum ' . $min. ' character required');
return FALSE;
}
}
}
This is my Model:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Post_Model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function insert_postjob($data){
$this->db->insert('jobs', $data);
}
}
jobs is table name
This my view
<div class="postjob">
<div class="container">
<h3>Post a Job</h3>
<form class="form-horizontal" role="form" id="postjobs" role="form" method="POST" action="<?=site_url('PostJobs/post');?>">
<div class="form-group">
<div class="col-lg-12">
<label for="jobemail">Email</label>
<span class="text-danger" style="display:inline-block; text-transform:uppercase;"><?php echo form_error('email');?></span>
<input type="email" id="emailjob" name="email" class="input-job form-control" value="<?php echo set_value('email'); ?>" autofocus>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="jobtitle">Job Title</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JTitle');?></span>
<input type="text" id="titlejob" name="JTitle" class="input-job form-control" value="<?php echo set_value('JTitle'); ?>">
</div>
<div class="col-lg-6">
<label for="jobtype">Job Type</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JType');?></span>
<select name="JType" class="input-job form-control" id="select" >
<option value="">SELECT</option>
<option value="Full Time">Full Time</option>
<option value="Part Time">Part Time</option>
<option value="Temporary">Temporary</option>
<option value="Internship">Internship</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="jobsalary">Salary</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Salary');?></span>
<input type="text" name="Salary" id="salaryjob" class="input-job form-control" value="<?php echo set_value('Salary');?>">
</div>
<div class="col-lg-6">
<label for="exp">Experience</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Exp');?></span>
<select name="Exp" class="input-job form-control" id="exp" >
<option value="">SELECT</option>
<option value="Fresher">Fresher</option>
<option value="< 1"> < 1</option>
<option value="1-2">1-2</option>
<option value="2-5">2-5</option>
<option value="5-8">5-8</option>
<option value="8-10">8-10</option>
<option value="10-12">10-12</option>
<option value="> 12">> 12</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="skills">Skills(Seperate by Comma)</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Skills');?></span>
<input type="text" name="Skills" id="skill" class="input-job form-control" value="<?php echo set_value('Skills');?>">
</div>
<div class="col-lg-6">
<label for="education">Education Requirement</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('EduReq');?></span>
<input type="text" name="EduReq" id="EduReq" class="input-job form-control" value="<?php echo set_value('EduReq');?>">
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="joblocation">Job Location</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JLocation');?></span>
<input type="text" name="JLocation" id="locationjob" value="<?php echo set_value('JLocation');?>" class="input-job form-control">
</div>
<div class="col-lg-6">
<label for="cname">Company Name</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('CName');?></span>
<input type="text" name="CName" id="cname" value="<?php echo set_value('CName');?>" class="input-job form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="cweb">Company Website(Optional)</label>
<input type="url" name="CWeb" id="cweb" value="<?php echo set_value('CWeb');?>" class="input-job form-control">
</div>
<div class="col-lg-6">
<label for="cadd">Company Address</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('CAdd');?></span>
<input type="text" class="input-job form-control" name="CAdd" value="<?php echo set_value('CAdd');?>">
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<label for="jobdesc">Job Description</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JDesc');?></span>
<textarea class="input-job form-control textarea" name="JDesc" rows="10" id="jdesc" placeholder="Description" style="resize:none;"><?php echo set_value('JDesc');?></textarea>
</div>
</div>
<div class="form-group text-center">
<input type="submit" name="btnpost" class="btn btn-green" value="POST JOBS"/>
</div>
</form>
</div>
</div>
Can anyone help me where i am failing to insert data??
Try to make your model without public:
$query = $this->db->insert('jobs', $data);
if ($query) {
return true;
} else {
return false;
}
Check if the table column names match to what you are trying to insert to, they might be wrong...
Use $this->db->last_query(); to see your generate query, and try to insert it manually and run it, if it fails you will see why :)
In your controller replace :
$this->Post_Model->insert_postjob($data);
with
$this->post_model->insert_postjob($data); // lowercase
In your model add following to your construct function
$this->load->database(); // if not added in autoload

input value returns when form validation runs false

View
<div class="box-body">
<div class="form-group">
<label for="FNAME" class="col-sm-2 control-label col-sm-offset-2"> <span>*</span>First Name:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="FNAME" name="FNAME">
</div>
</div>
<div class="form-group has-error col-sm-offset-7">
<label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label>
</div>
</div>
Controller
public function create_id() {
$this->form_validation->set_rules('FNAME', 'First Name' ,'trim|required|max_length[30]');
$this->form_validation->set_rules('MNAME', 'Middle Name' ,'trim|max_length[30]');
$this->form_validation->set_rules('SURNAME', 'Last Name' ,'trim|required|max_length[30]');
if($this->form_validation->run($this) == FALSE) {
$this->add_view();
} else {
if($query = $this->Employees_Model->insert()) {
$this->autoid();
redirect('Employees/index');
} else {
$this->add_view();
}
}
}
What I want is if($this->form_validation->run($this) == FALSE){as you see it will redirect back to view. $this->add_view(); all I want is when it redirects back, the data that I input will remain. so that I will not input it again when the validation fails.
As per CodeIgniter documentation, you need to alter your view file a little bit, in the means of, printing those input element values with <?= set_value("FNAME"); ?> in the input element _value_ parameter. So your
<input type="text" class="form-control" id="FNAME" name="FNAME">
would become
<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= set_value("FNAME"); ?>">
and so on. This way you will tell CodeIgniter to re-populate the form after an error.
You can use set_value function.
view file
<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?php echo set_value('FNAME'); ?>">
Get the posted value like this
public function create_id() {
$this->form_validation->set_rules('FNAME', 'First Name' ,'trim|required|max_length[30]');
$this->form_validation->set_rules('MNAME', 'Middle Name' ,'trim|max_length[30]');
$this->form_validation->set_rules('SURNAME', 'Last Name' ,'trim|required|max_length[30]');
$data = array(
'FNAME' => $this->input->post('FNAME'),
'MNAME' => $this->input->post('MNAME'),
'SURNAME' => $this->input->post('SURNAME')
);
if($this->form_validation->run($this) == FALSE) {
$this->add_view($data);
} else {
if($query = $this->Employees_Model->insert()) {
$this->autoid();
redirect('Employees/index');
} else {
$this->add_view();
}
}
}
In your function
function add_view($data){
.............................
.............................
$this->data['values'] = $data;
.............................
.............................
}
Then
<div class="box-body">
<div class="form-group">
<label for="FNAME" class="col-sm-2 control-label col-sm-offset-2"><span>*</span>First Name:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= $values['FNAME'] ?>">
</div>
</div>
<div class="form-group has-error col-sm-offset-7">
<label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label>
</div>
</div>

Categories