Codeigniter: how to check if username already exists from mysql database - php

I'm currently working on codeigniter. I am not so expert using framework. I have a registration form of an employee and I want it to display "username already exists" beside the Username input after I submit a button.
Here is the image of tables from mysql database:
Image of tables from mysql database
Here is the controller (home.php):
public function viewAddEmployeeForm() {
$this->load->model('Model_home');
$data = array();
$data['dropdown'] = $this->Model_home->get_dropdown();
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('emp_add', $data);
}
public function saveEmployee() {
$this->load->model('Model_home');
$p = new Model_home();
$p->date_employed = $this->input->post('date_emp');
$p->designation_id = $this->input->post('emp_desi');
$p->username = $this->input->post('username');
$p->password = $this->input->post('pswrd');
$p->name = $this->input->post('emp_name');
$p->midname = $this->input->post('emp_mname');
$p->lastname = $this->input->post('emp_lname');
$p->CityAddress = $this->input->post('emp_cadd');
$p->license_num = $this->input->post('emp_license');
$p->TIN_num = $this->input->post('emp_tin');
$p->SSSNo = $this->input->post('emp_sss');
$p->PhilHealth = $this->input->post('emp_ph');
$p->DoB = $this->input->post('emp_dob');
$p->Gender = $this->input->post('emp_gender');
$p->contnum = $this->input->post('emp_mobno');
$p->ContactPerson = $this->input->post('emp_contpers');
$p->ContactPerson_Num = $this->input->post('emp_contpersnum');
$p->ContactPerson_Add = $this->input->post('emp_contpersadd');
if($p->designation_id == 1){
$p->user_type = 0;
}else{
$p->user_type = 1;
}
$result = $p->saveEmployee();
if (!$result) {
echo mysqli_error($result);
}
else {
redirect('home/goSettings', 'refresh');
}
}
Here is the view: (emp_add.php):
<h1>Add Employee</h1>
<br>
<?php echo form_open('home/saveEmployee',array('class'=>'form-horizontal'));?>
<h4> Personal Information </h4>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">First Name <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_name" name="emp_name" required="required" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="last-name">Last Name <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_lname" name="emp_lname" required="required" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Middle Name / Initial</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="emp_mname" name="emp_mname" class="optional form-control col-md-7 col-xs-12" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Gender</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<div id="gender" class="btn-group" data-toggle="buttons">
<select id="emp_gender" name="emp_gender" class="form-control">
<option id="emp_gender" name="emp_gender" value="Male">Male</option>
<option id="emp_gender" name="emp_gender" value="Female">Female</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Date Of Birth <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="emp_dob" name="emp_dob" class="date-picker form-control col-md-7 col-xs-12" required="required" type="date">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Address <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="emp_cadd" name="emp_cadd" class="form-control col-md-7 col-xs-12" required="required" type="text">
</div>
</div>
<h4> Employee Identification </h4>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">License Number</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_license" name="emp_license" class="optional form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">TIN Number</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_tin" name="emp_tin" class="optional form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Number</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_sss" name="emp_sss" class="optional form-control" >
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">PhilHealth</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_ph" name="emp_ph" class="optional form-control" >
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Username <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="username" name="username" required="required" class="form-control col-md-7 col-xs-12" onblur="return check_username();">
<div id="Info"></div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Password <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="password" id="pswrd" name="pswrd" required="required" class="form-control col-md-7 col-xs-12">
</div>
</div>
<h4> Work details </h4>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Date Employed <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="date_emp" name="date_emp" class="date-picker form-control col-md-7 col-xs-12" required="required" type="date">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Designation <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<?php echo form_dropdown('emp_desi', $dropdown, '', 'class="form-control" id="emp_desi"'); ?>
</div>
</div>
<h4> Contact Information </h4>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Mobile Number <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_mobno" name="emp_mobno" class="form-control" required="required" >
</div>
</div>
<h4> Contact Person </h4>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Name <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_contpers" name="emp_contpers" required="required" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Address <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_contpersadd" name="emp_contpersadd" required="required" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Mobile Number <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="emp_contpersnum" name="emp_contpersnum" required="required" class="form-control" >
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button type="submit" class="btn btn-success" name="emp_submit" id="emp_submit" onclick="alert('You have successfully added an employee')">Submit</button>
</div>
</div>
</form>
Here is the model (model_home.php):
public function saveEmployee() {
if (isset($this->empnum)) {
$query = $this->updateEmployee();
}
else {
$query = $this->addEmployee();
}
return $query;
}
public function get_dropdown() {
$result = $this->db->select('designation_id, designation')->get('designation')->result_array();
$dropdown = array();
foreach($result as $r) {
$dropdown[$r['designation_id']] = $r['designation'];
}
return $dropdown;
}

Try this
$this->form_validation->set_rules('username', 'Username', 'required|is_unique[users.username]');
Here is_unique[users.username], users refer to users table & username refer to field name in users table. This will automatically check for username in users table.

To see if something already exists in a database you usually either use the LIKE command or a REGEXP command to test if the entry is already there. See this article for how to use REGEXP:
mySQL regex in the where clause
If the item is already there it will return the item. If not it should return NULL or nothing.
Then all you do is to refresh the pre-existing HTML web page. The only change you need in your current web page is a bit of PHP code that just echos the returning information. So something simple like:
<?php echo $foundIt; ?>
You put that next to where they put in the username (or the username is displayed). Be sure to declare the variable with a blank field if nothing is found. I.E.:
$foundIt = "";
Otherwise your web page will generate an error saying it is undefined.
I see sriAnkush answered your question. My answer is using plain HTML. Looks like his is using CodeIgniter. :-)

try controller function like this. Get Username in $username and pass it to model. use Select query where username is equal to $username and return the array. If there is any similar username $result will be greater than or equal to 1. so it will show username already exist else it will save your data.
public function saveEmployee() {
$username=$this->input->post('username');
$result=$this->model->checkUsername($username);
if($result>=1){
echo "Username Already exists";
}
else{
// Query to save the username and data;
}
}
Good Luck!

Related

how to solve Call to a member function update() on null on laravel 8?

I am getting this kind of error call to member function update on null(). shown my code below please anyone can solve
all are the things are fine when am getting to the image file update in that time am getting this error.
here is my controller code
////
function user_update(Request $request){
$user= User::findOrFail($request->id)->update([
'name'=>$request->name,
'email'=>$request->email,
'address'=>$request->address,
'nationality'=>$request->nationality,
'date_of_birth'=>$request->date_of_birth,
'father_name'=>$request->father_name,
'mother_name'=>$request->mother_name,
'gender'=>$request->gender,
'n_photo'=>$request->n_photo,
]);
if ($request->hasFile('n_photo')) {
$photo_upload = $request ->n_photo;
$photo_extension = $photo_upload -> getClientOriginalExtension();
$photo_name = "toletx_auth_image_". $user . "." . $photo_extension;
Image::make($photo_upload)->resize(452,510)->save(base_path('public/uploads/auth/'.$photo_name),100);
User::find($user)->update([
'n_photo' => $photo_name,
]);
}
return back()->with('success','User information have been successfully Updated.');
}
///////my blade file
<form method="POST" action="{{ route('user_update') }}" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<input type="text" name="id" value="{{$list->id}}">
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">User name</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->name}}" name="name" placeholder="Location" type="text" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Email</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->email}}" name="email" placeholder="Location" type="text">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Mobile Number</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->phone}}" name="phone" placeholder="Location" type="text" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Address</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->address}}" name="address" placeholder="Location" type="text">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Nationality</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" name="nationality" value="{{$list->nationality}}" placeholder="nationality" type="numeric">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Date of birth</label>
<div class="col-sm-12 col-md-10">
<input type="text" class="form-control" placeholder="Birth Date" name="date_of_birth" value="{{$list->date_of_birth}}">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Father Name</label>
<div class="col-sm-12 col-md-10">
<input type="text" class="form-control" placeholder="Father name" name="father_name" value="{{$list->father_name}}">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Mother Name</label>
<div class="col-sm-12 col-md-10">
<input type="text" class="form-control" placeholder="mother name" name="mother_name" value="{{$list->mother_name}}">
</div>
</div>
<div class="form-group">
<div class="form-group form-float">
<div class="card">
<div class="body">
<input type="file" class="dropify" name="n_photo" >
<img src="{{ asset('uploads/auth') }}/{{ $list->n_photo }}" alt="">
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Update</button>
You've got 2 main problems with this code:
User::find($user)->update(['n_photo' => $photo_name]);
First of all, the correct syntax would be to use $user->id, not $user:
User::find($user->id)->update(['n_photo' => $photo_name]);
Second, that is super redundant; $user is already the result of User::find(), so you're doubling up for no reason.
To fix your issue, simply adjust your code to:
$user->update(['n_photo' => $photo_name]);
Just spacing issue please check these two lines
//Before // Wrong
$photo_upload = $request ->n_photo;
$photo_extension = $photo_upload -> getClientOriginalExtension();
//After //Correct
$photo_upload = $request->n_photo;
$photo_extension = $photo_upload->getClientOriginalExtension();
function user_update(Request $request){
$photo_name = '';
$user= User::findOrFail($request->id);
if ($request->hasFile('n_photo')) {
$photo_upload = $request->n_photo;
$photo_extension = $photo_upload->getClientOriginalExtension();
$photo_name = "toletx_auth_image_". $user . "." . $photo_extension
Image::make($photo_upload)->resize(452,510)->save(base_path('public/uploads/auth/'.$photo_name),100);
}
$user->update([
'name'=>$request->name,
'email'=>$request->email,
'address'=>$request->address,
'nationality'=>$request->nationality,
'date_of_birth'=>$request->date_of_birth,
'father_name'=>$request->father_name,
'mother_name'=>$request->mother_name,
'gender'=>$request->gender,
'n_photo'=>$photo_name,
]);
}
return back()->with('success','User information have been successfully Updated.');
}

Inserting Multiple rows in mysql using codeigniter : it inserts only one row and in that one row stores only one first character

I tried to insert multiple rows in sql . But it inserts only the last one row and in that one row only storing first character of each column. I prints the query by echo, it shows only one last row, but gives all characters of each column. one more thing is iam inserting values in two tables by clicking on submit button. can anyone help on this.
Here it is the view :
<form class="" method="POST" enctype="multipart/form-data" action="<?php echo base_url(); ?>dashboard/addnewjobmela" >
<input type="hidden" name="csrfmiddlewaretoken" value="LgVIVf7yFe5bL9k2Rcj9TGLLpgKJX1LkmfiiptEZnN95y9WqKXHk7V4vGixmo6Wd">
<input type="hidden" id="cperson_no" value="1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Job Mela Title</label>
<input class="form-control" type="text" name="title" required="" id="title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4">
<div class="form-group">
<label>Job Mela Date</label>
<input class="form-control" type="date" name="date" required="" id="date">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4">
<div class="form-group">
<label>Last Date To Register</label>
<input class="form-control" type="date" name="laastdatetoregister" required="" id="laastdatetoregister">
</div>
</div>
<div class="col-xs-12 col-sm-12 ">
<div class="form-group">
<label>Venue Details</label>
<textarea class="form-control" name="venuedetails" cols="40" rows="2" required="" id="venuedetails"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Contact Person</label>
<input class="form-control" type="text" name="contactperson" required="" id="contactperson">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Contact Emailid</label>
<input class="form-control" type="text" name="emailid" required="" id="emailid">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Contact Number</label>
<input class="form-control" type="text" name="contactnumber" required="" id="contactnumber">
</div>
</div>
</div>
<div class="row-fluid" >
<div style="background-color:#d6e9c6 !important;padding:10px"><p class="text-success"><b>Participating Companies</b></p></div>
</div>
<div class="col-xs-12 right" style="margin-top:-40px;margin-bottom: 20px">
<button class="btn btn-success" type="button" onclick="addingcompanies()">Add More</button>
</div>
<div id="companies">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Company Name</label>
<input class="form-control" type="text" name="company" required="" id="company">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Job Title</label>
<input class="form-control" type="text" name="jobtitle" required="" id="jobtitle">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Required Qualification</label>
<?php
echo form_dropdown('qualification', $education,'' ,'required="" class="form-control"');
?>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Specialization</label>
<input class="form-control" type="text" name="specialization" required="" id="specialization">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Sector</label>
<input class="form-control" type="text" name="sector" required="" id="sector">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4">
<div class="form-group">
<label>Job Type</label>
<?php
echo form_dropdown('jobtype', $jobtype,'' ,'required="" class="form-control"');
?>
</div>
</div>
<!-- <div class="col-xs-12 col-sm-12 col-md-4">
<div class="form-group">
<label>Company Logo</label>
<input class="form-control" type="file" name="picture" accept=".jpeg,.JPEG,.JPG,.jpg,.png,.PNG" required="" id="id_picture">
</div>
</div> -->
<div class="col-xs-12 col-sm-12 col-md-4 ">
<div class="form-group">
<label>Job Location</label>
<input class="form-control" type="text" name="joblocation" required="" id="joblocation">
</div>
</div>
</div>
</div>
<div class="col-xs-12" style="text-align:center">
<button class="btn btn-success" type="submit" value="Save Profile">Submit</button>
</div>
</form>
Javascript/Jquery to add ADD More Fields
<script type="text/javascript">
function addingcompanies()
{
var cperson_no = $('#cperson_no').val();
var j=parseInt(cperson_no)+1;
$('#cperson_no').val(j);
var qualification = '<?php echo str_replace("'", '', preg_replace("/\r|\n/", "", form_dropdown('qualification', $education,'' ,'required="" class="form-control"'))); ?>';
var jobtype = '<?php echo str_replace("'", '', preg_replace("/\r|\n/", "", form_dropdown('jobtype', $jobtype,'' ,'required="" class="form-control"'))); ?>';
$('#companies').append('<div class="row group"><hr/><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Company Name</label> <input class="form-control" type="text" name="company" required="" id="company' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Job Title</label> <input class="form-control" type="text" name="jobtitle" required="" id="jobtitle' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Required Qualification</label> '+ qualification +'</div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Specialization</label> <input class="form-control" type="text" name="specialization" required="" id="specialization' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Sector</label> <input class="form-control" type="text" name="sector" required="" id="sector' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-12 col-md-4"> <div class="form-group"> <label>Job Type</label>'+jobtype+' </div></div><div class="col-xs-12 col-sm-12 col-md-4 "> <div class="form-group"> <label>Job Location</label> <input class="form-control" type="text" name="joblocation" required="" id="joblocation' + cperson_no + '"> </div></div><div class="col-xs-12 col-sm-4 col-md-4 "><button class="btn btn-danger" style="margin-top: 25px; !important;" type="button" onClick="con_grpremove(this)">Remove</button></div></div></div>');
}
function con_grpremove(obj) {
$(obj).closest(".group").remove();
}
</script>
Here it is the controller :
public function addnewjob()
{
$this->load->model('dashboard_model');
$result = $this->dashboard_model->addnewjob();
$this->session->msg = "New Job Added Successfully";
redirect('dashboard');
}
public function addnewjobmela()
{
$this->load->model('dashboard_model');
$result = $this->dashboard_model->addnewjobmela();
$this->session->msg = "New Job Mela Added Successfully";
redirect('dashboard');
}
Model :
public function addnewjobmela() {
$data['title'] = $this->input->post('title');
$data['date'] = $this->input->post('date');
$data['lastdate'] = $this->input->post('laastdatetoregister');
$data['venue'] = $this->input->post('venuedetails');
$data['contactperson'] = $this->input->post('contactperson');
$data['emailid'] = $this->input->post('emailid');
$data['contactnumber'] = $this->input->post('contactnumber');
$data['status'] = 1;
$data['id'] = $this->db->insert_id();
print_r($data);
$this->db->insert('jobmelas', $data);
$jobmelaid = $this->db->insert_id();
$jcdata['company'] = $this->input->post('company');
$jcdata['jobtitle'] = $this->input->post('jobtitle');
$jcdata['qualification'] = $this->input->post('qualification');
$jcdata['specialization'] = $this->input->post('specialization');
$jcdata['sector'] = $this->input->post('sector');
$jcdata['jobtype'] = $this->input->post('jobtype');
$this->insCompanies($jobmelaid, $jcdata);
}
public function insCompanies($jobmelaid, $jcdata, $update='') {
if($update == 'update'){
$this->db->query('delete from job_mela_companies where jobmelaid = '.$jobmelaid.'');
}
print_r($jcdata);
for ($i = 0; $i < count($jcdata['company']); $i++) {
$cp_data[] = array(
'jobmelaid' => $jobmelaid,
'company' => $jcdata['company'][$i],
'jobtitle' => $jcdata['jobtitle'][$i],
'qualification' => $jcdata['qualification'][$i],
'specialization' => $jcdata['specialization'][$i],
'jobtype' => $jcdata['jobtype'][$i],
'sector' => $jcdata['sector'][$i]
);
$this->db->insert('job_mela_companies', $cp_data);
}
}
First table is giving result correct but
The output of more fields giving like this:
try below array and remove $cp_data[] = array(....) from insCompanies function.
$cp_data = array(
'jobmelaid' => $jobmelaid,
'company' => $jcdata['company'][$i],
'jobtitle' => $jcdata['jobtitle'][$i],
'qualification' => $jcdata['qualification'][$i],
'specialization' => $jcdata['specialization'][$i],
'jobtype' => $jcdata['jobtype'][$i],
'sector' => $jcdata['sector'][$i]
);

mysqli cannot display full data

I have a table named testads and the data are as below
Link to table image
I want to make an update page where the data are select by row. I have no problem with updating the data however when the data is displayed in update page, instead of the company name Subway Malaysia, it only shows Subway. This happens to all the data with more than one word. I can't seem to find what's the problem
Here are the php code:
<?php
include('./include/connection.php');
$ID=$_GET['no'];
$query = "SELECT no, id_company, company_name, jobName,state, location, jobDesc, contact FROM testads where no='$ID'";
$result = mysqli_query($link, $query) or die ("error.");
while ($row = mysqli_fetch_array($result)){
$id=$row['no'];
?>
<form class="form-horizontal form-label-left" method="post" action="edit_query.php<?php echo '?no='.$id; ?>">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="comp-name">Company Name <span class="required"></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="comp-name" required="required" name="cname" class="form-control col-md-7 col-xs-12" value=<?php echo $row['company_name']; ?>>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="comp-id">Company ID <span class="required"></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="comp-id" required="required" name="cid" class="form-control col-md-7 col-xs-12" value=<?php echo $row['id_company']; ?>>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="job-name">Job Name <span class="required"></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="job-name" required="required" name="jname" class="form-control col-md-7 col-xs-12" value=<?php echo $row['jobName']; ?>>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="desc">Job Description<span class="required"></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea id="desc" name="description" class="form-control" maxlength="2000" value=<?php echo $row['jobDesc']; ?>></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="stt">State</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select name="state" class="form-control">
<option value="Johor"<?php echo $row['state'] == 'Johor' ? 'selected="selected"' : '' ; ?>>Johor</option>
<option value="Malacca"<?php echo $row['state'] == 'Malacca' ? 'selected="selected"' : '' ; ?>>Malacca</option>
<option value="Pahang"<?php echo $row['state'] == 'Pahang' ? 'selected="selected"' : '' ; ?>>Pahang</option>
<option value="Selangor"<?php echo $row['state'] == 'Selangor' ? 'selected="selected"' : '' ; ?>>Selangor</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="loc">Location<span class="required"></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="loc" required="required" name="location" class="form-control col-md-7 col-xs-12" value=<?php echo $row['location']; ?>>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="ctc">Contact<span class="required"></span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="ctc" required="required" name="contact" class="form-control col-md-7 col-xs-12" value=<?php echo $row['contact']; ?>>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button class="btn btn-success" type="submit" name="submit">Update</button>
</div>
</div>
</form>
You need to quote your values:
<input ... value="<?php echo $row['company_name']; ?>">

laravel get parameter from url to pass it with the action of the form

so i have this problem that i can't get pass it, i'm trying to get an parameter i sent with this view
#if(Auth::user())
Apply
#else
Apply
#endif
when the user isn't login it redirect him to the register form and it send the 'VacanciesID' to the register form, so its there in the url but i can't get the parameter of the url to the blade to send it with the action of the form that looks like this /careers/1/apply i need '1'.
heres my register form
<form action="/register/" method="POST" class="careersform">
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
<h4>User Details</h4>
<hr>
<div class="row">
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Username</label>
<input name="EmployeeUsername" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Password</label>
<input name="EmployeePassword" type="text" class="form-control">
</div>
</div>
</div>
<h4>Personal Details</h4>
<hr>
<div class="row">
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>First Name</label>
<input name="FName" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Last Name</label>
<input name="LName" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Gender</label>
<select name="Gender" class="form-control">
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Date of Birth</label>
<input name="DOB" type="date" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Marital Status</label>
<select name="MaritalStatus" class="form-control">
<option>Single</option>
<option>Married</option>
<option>Other</option>
</select>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Country of Nationality</label>
<select name="CountryOfNationality" class="form-control">
<option>Jordan</option>
<option>Other..</option>
</select>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>National ID</label>
<input name="NationID" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Image</label>
<input name="Image" type="file" class="form-control">
</div>
</div>
<div class="form-group col-md-6 col-xs-12">
<img id="ImgUpload" src="/images/avatar.png" alt="Uploaded Image"/>
</div>
</div>
<h4>Education Details</h4>
<hr>
<div class="row">
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>School's Name</label>
<input name="SchoolName" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Education Level</label>
<select name='EducationLevel' class="form-control">
<option>High school</option>
<option>Some college</option>
<option>Bachelor's degree</option>
<option>Master's degree</option>
</select>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Major</label>
<input name="EducationMajor" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>GPA</label>
<input name="GBA" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Add Another Education</label>
<input name="education" type="text" class="form-control">
</div>
</div>
</div>
<h4>Experience Details</h4>
<hr>
<div class="row">
<div class="col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Company's Name</label>
<input name="CompanyName" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Job Title</label>
<input name="JobTitle" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Salary</label>
<input name="Salary" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Start date</label>
<input name="StartDate" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>End date</label>
<input name="EndDate" type="text" class="form-control">
</div>
</div>
<div class="col-md-6 col-xs-12">
<div class="form-group careersform-group-styled required">
<label>Add Another Experience</label>
<input name="anotherexperience" type="text" class="form-control">
</div>
</div>
</div>
<button type="submit" class="btn turquoiseButton">Submit</button>
</form>
register route:
Route::post('register/', 'UsersController#careerportalregister');
controller:
public function careerportalregister(request $request){
$EmployeeUsername = $request->input('username');
$EmployeePassword = $request->input('password');
$role_id = $request->input('role_id');
$rol = $request->input('roles');
$roles= explode("," ,$rol);
$validator = Validator::make($request->all(), [
'password' => 'required|min:5|confirmed',
'confirm_password' => 'required|min:6|confirmed'
]);
// if ($validator->fails()) {
// return redirect('/user/create')
// ->withErrors($validator)
// ->withInput();
// } else {
$employee = user::create([
'username' => $request->input('EmployeeUsername'),
'password' => bcrypt($request->input('EmployeePassword')),
]);
$Title = $request->input('Title');
$Gender = $request->input('Gender');
$FName = $request->input('FName');
$LName = $request->input('LName');
$DOB = $request->input('DOB');
$MaritalStatus = $request->input('MaritalStatus');
$CountryOfBirth = $request->input('CountryOfBirth');
$CountryOfNationality = $request->input('CountryOfNationality');
$NationID = $request->input('NationID');
$Image = $request->input('Image');
$user_id = $employee->id;
PersonalDetails::CreatePersonalDetails($Title,$Gender ,$FName ,$LName,$DOB,$MaritalStatus,$CountryOfBirth,$CountryOfNationality,$NationID,$Image,$user_id);
$EducationMajor = $request->input('EducationMajor');
$EducationLevel = $request->input('EducationLevel');
$SchoolName = $request->input('SchoolName');
$GBA = $request->input('GBA');;
$user_id = $employee->id;
EducationDetails::CreateEducationDetails($EducationMajor,$EducationLevel ,$SchoolName ,$GBA,$user_id);
$CompanyName = $request->input('CompanyName');
$StartDate = $request->input('StartDate');
$EndDate = $request->input('EndDate');
$Salary = $request->input('Salary');
$JobTitle = $request->input('JobTitle');
$UserID = $employee->id;
ExperienceDetails::CreateExperienceDetails($CompanyName,$StartDate ,$EndDate ,$Salary,$JobTitle,$UserID);
foreach ($roles as $role) {
$count = DB::table('roles')->where('name', $role)->count();
if ($count != 0) {
$s = DB::table('roles')->where('name', $role)->first();
DB::table('role_user')->insert([
'user_id' => $employee->id,
'role_id' => $s->id
]);
}
}
Auth::login($employee, $remember = true);
return Redirect::to('/apply/success/');
}
In controller \Request::segment(2)
In blade {{Request::segment(2)}}
2 is the index number, feel free to change it based on your needs.
I would pass the id to the register url as an optional parameter.
Like this :
Route::post('register/{vacanciesId?}', 'UsersController#careerportalregister');
Then get it in the controller and do what you want with it.
You could also save it in session.
Have a nice day,
Assuming this is on your vacancies page and the problem you are having is that you are not able to put the ID into the url you need to iterate through each vacancy and set the ID that way so that the links are generated with a unique ID for each vacancy.
Also considering you have two different ways to provide details for the vacancy you would need to to create two Route::Get' for /apply/success/{{id}} and /careers/{{id}}/apply
When displaying the vacancies, I assume you are retrieving from the datebase and iterating through the results to show in the view? You should edit your post and display that.

php - error in Autopopulate not working

<div class="form-group">
<label class="col-md-3 col-sm-3 col-xs-12" for="">Member No of the Transferer<span class="required">*</span></label>
<div class="col-md-3 col-sm-6 col-xs-12">
<input required="" class="form-control col-md-7 col-xs-12" id="old_member_no" type="number" name="old_member_no" <?php if(isset($editData)) echo 'readonly style="pointer-events: none;"'; ?> value="{{ old('old_member_no',isset($editData) ? $editData->old_member_no:"") }}">
<input type="hidden" name="member_no" value="{{ old('member_no',isset($editData) ? $editData->member_no:"") }}" />
</div>
<span class="help-block"></span>
</div>
<div class="form-group">
<label class="col-md-3 col-sm-3 col-xs-12" for="">Name of the Transferer<span class="required">*</span></label>
<div class="col-md-3 col-sm-6 col-xs-12 ">
<input required="" class="form-control col-md-7 col-xs-12" id="" type="text" name="full_name" value="{{ old('full_name',isset($editData) ? $editData->full_name:"") }}">
</div>
<span class="help-block"></span>
</div>
<div class="form-group">
<label class="col-md-3 col-sm-3 col-xs-12" for="">Address of the Transferer<span class="required">*</span></label>
<div class="col-md-3 col-sm-6 col-xs-12 ">
<input required="" class="form-control col-md-7 col-xs-12" id="house" type="text" name="house" value="{{ old('house',isset($editData) ? $editData->house:"") }}">
</div>
<span class="help-block"></span>
</div>
Hello guys. I'm doing a project. I'm developing a form with autopopulate. If number is typed and after pressing enter the address and name should be fetched from database. kindly help me
Since it is not clear to me but base on your descriptions... You would need to use Javascript/JQuery here and use Event functions when the entering a field

Categories