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>
Related
Hello everyone i'm actually stuck for hours because of this, im new to laravel and idk whats wrong with my code and there is no actual message error, please help me
This my store function
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'jenis_id' => 'required',
'detjenis_id' => 'required',
'email' => 'required',
'message' => 'required',
]);
$asep = $request->name;
dd($asep);
$order = new DataOrder();
$order->name = $request->name;
$order->jenis_id = $request->jenis_id;
$order->detjenis_id = $request->detjenis_id;
$order->email = $request->number;
$order->message = $request->message;
$order->save();
//return back()->with('alert-success', '<script> window.onload = swal("Sukses!", "Data telah terkirim!", "success")</script>');
// if ($order->save()) {
// return redirect(route('home'))->with('alert-success', '<script> window.onload = swal("Sukses!", "Data telah terkirim!", "success")</script>');
// } else {
// return redirect(route('home'))->with('alert-success', '<script> window.onload = swal("Oops !" , "Data gagal terkirim!!" , "error")</script>');
// }
}
This my blade code
<form action="{{route('order.store')}}" method="POST">
{{csrf_field()}}
<div class="tm-section-wrap bg-white">
<section id="talk" class="row tm-section">
<div class="col-xl-6 mb-5">
<div class="tm-contact-form-wrap">
<div class="tm-contact-form">
<div class="form-group">
<input type="text" id="name" name="name"
class="form-control rounded-0 border-top-0 border-right-0 border-left-0"
placeholder="Nama anda..." required="" />
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Kebutuhan</label>
<select name="jenis_id" class="form-control" id="jenis_id">
<option value="" disabled selected>Pilih dibawah ini:</option>
#foreach ($jenis as $j)
<option value="{{$j->id}}">{{$j->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Detail</label>
<select name="detjenis_id" class="form-control" id="detjenis_id">
</select>
</div>
<div class="form-group">
<input type="number" id="number" name="number"
class="form-control rounded-0 border-top-0 border-right-0 border-left-0"
placeholder="Nomor HP" required="" />
</div>
<div class="form-group">
<textarea rows="4" id="message" name="message"
class="form-control rounded-0 border-top-0 border-right-0 border-left-0"
placeholder="Message..." required=""></textarea>
</div>
<div class="form-group mb-0">
<button type="submit" class="btn rounded-0 d-block ml-auto tm-btn-primary">
SEND
</button>
</div>
</div>
</div>
</div>
</section>
</div>
and my route setup
Route::get('/testcrud', 'HomeController#testcrud')->name('testcrud');
Route::resource('SAS/order', 'DataOrderController');
Thank you if there anyone can help me with this problem, i'm really appreciate that.
As I can see, There is email field is missing in your form and you are using :
'email' => 'required'
Thats why data is not saving in database. For displaying validation error you can check here
are you sure that you already put all the field name,jenis_id,detjenis_id,email,message
in the fillable array in the model?
or if you don't want to bother doing that? you can just create a empty
protected $guarded variable on model.
And are you sure about the field? because i see you put number on email field.
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
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
I am doing an airline reservation and I have 2 radio button.
1) One way
2) Round Trip
The things that I've done is when I select the Round Trip, all the fields are there(Depart, return and number of passengers) but I when select One way radio button the return field should hide.
In my controller, I have validation that all fields are required. The problem is, whenever I tried to search in One Way(the return field is hidden) it gives me an "Return field is required" error
Question: How can I prevent the validation in return field when I choice the One Way radio button?
View
<div class="pure-u-1-1 searchcontainer center">
<div class="pure-u-1-1 findcheaptxt">
<span>Find Cheap Flights</span>
</div>
<div class="pure-u-1-1 radiobtn">
<form action="">
<input type="radio" name="flight_type" value="one_way" class="onew" style="" >One Way
<input type="radio" name="flight_type" class="roundw" style="" checked>Round Trip
</form>
</div>
<form method="post" enctype="multipart/form-data" action="<?= base_url() .'User/search'?>">
<?= validation_errors(); ?>
<div class="pure-u-1-1 fromto">
<div class="pure-u-1-1">
<label for="from" class="margin2px">From</label>
<select name="flight_from">
<option value="">-- Please select depature --</option>
<?php foreach($countries as $country):?>
<option value ="<?= $country->country_name?>" ><?= $country->country_name?></option>
<?php endforeach?>
</select>
</div>
<div class="pure-u-1-1">
<label for="to" class="tomargin">To</label>
<!-- <input type="text" class="fromto"><br> -->
<select class="fromto" name="flight_to">
<option value="">-- Please select destination --</option>
<?php foreach($countries as $country):?>
<option value ="<?= $country->country_name?>" ><?= $country->country_name?></option>
<?php endforeach?>
</select>
</div>
<div class="pure-u-1-1 dr" name ="depart">
<label for="depart" class="drr">Depart</label>
<input type="date" id="depart" name="depart" class="departreturn">
</div>
<div class="pure-u-1-1 dr" id="try">
<label for="return" class="drr">Return</label>
<input type="date" id="return" name="return" class="departreturn"><br>
</div>
</div>
<div class="pure-u-1-1 personfield">
<!-- <div class="pure-u-1-5 margin">
Adult<br>
<input type="text" name="" class="person">
</div>
<div class="pure-u-1-5 margin">
Seniors<br>
<input type="text" name="" class="person">
</div>
<div class="pure-u-1-5 margin">
Children<br>
<input type="text" name="" class="person">
</div>
<div class="pure-u-1-5">
Class<br>
<input type="text" name="" class="person">
</div> -->
<div class="pure-u-1-5 margin">
Number of Passengers<br>
<input type="text" name="no_of_passengers" class="person">
</div>
</div>
<div class="pure-u-1-1 center">
<button class="submitbtn">Search Now</button>
</form>
</div>
</div>
</div>
Controller
public function search()
{
$data['countries'] = $this->CrudModel->get('countries');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim');
$this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim');
if ($_POST['flight_type'] == 'round_trip')
{
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
$this->form_validation->set_rules('return', 'Date of return', 'required|trim');
$this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$search_result = array(
$flight_from = $_POST['flight_from'],
$flight_to = $_POST['flight_to'],
$depart = $_POST['depart'],
$return = $_POST['return'],
$no_of_passengers = $_POST['no_of_passengers']
);
$data['search_result'] = $this->CrudModel->search('flight',$flight_from,$flight_to,$depart,$return,$no_of_passengers);
$this->load->view('partials/header');
$this->load->view('partials/nav');
$this->load->view('result',$data);
}
}
else
{
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
$this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else {
$search_result = array(
$flight_from = $_POST['flight_from'],
$flight_to = $_POST['flight_to'],
$depart = $_POST['depart'],
$no_of_passengers = $_POST['no_of_passengers']
);
$data['search_result'] = $this->CrudModel->search('flight',$flight_from,$flight_to,$depart,$no_of_passengers);
$this->load->view('partials/header');
$this->load->view('partials/nav');
$this->load->view('result',$data);
}
}
}
My ajax/js when hiding the return field
<script type="text/javascript">
$(document).on('change', 'input:radio[name=flight_type]', function(){
$('div[id^="try"]').toggle(); // hide all DIVs begining with "my_radio_"
$('#' + $(this).attr('id') + '_text').show(); // show the current one
});
</script>
First of all you have to add value to round trip:
<input type="radio" name="flight_type" value="round_trip" class="roundw" style="" checked>Round Trip
Then do validation condition like below:
// Global validation for this method
if ($_POST['flight_type'] == 'round_trip')
{
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
$this->form_validation->set_rules('return', 'Date of return', 'required|trim');
}
else if($_POST['flight_type'] == 'one_way'){
// add validation for one way trip
}
You have to pass flight_type parameters in post request. Then your code will be something like this describe below.
$this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim');
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
if($this->input->post('flight_type')== 'roundw'):
$this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim');
$this->form_validation->set_rules('return', 'Date of return', 'required|trim');
endif:
Let me know if it not works for you.
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