country drop down not being populate with data codeigniter - codeigniter

country drop down not being populate with data codeigniter, can any body help me out, i want list of country on view so that i could select country. when i add select code in view for drop down it hides the list and save button.
my model
public function get_Country() {
$this->db->select('code, name')->from("country");
$query = $this->db->get();
return $query->result_array();
}
my controller
public function get_Country() {
$ctry = $this->job_post_model->get_Country();
// debug($ctry);
}
my view code
<div class="row">
<div class="span8">
<div class="about-heading">
<div class="head-menu"> </div>
</div>
<div class="about-section">
<div class="about-content rigs-content">
<form name="jobpost" action=" <?php echo base_url('employer/job_post/post_job'); ?>" method="post">
<h2>Your Job Detail :</h2>
<div class="invest-form">
<ul>
<li class="pull-right">
<label>Job Category:</label>
<input type="text" name="jobcat" value="" placeholder="Enter Job Category*" />
</li>
<li class="pull-right">
<label>Job Title:</label>
<input type="text" name="jobtitle" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Job Type:</label>
<input type="text" name="jobtype" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Preffered Age:</label>
<input type="text" name="age" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Preffered Gender:</label>
<input type="text" name="gender" value="" placeholder="Enter Job Type*" />
</li>
<li>
<label>Job Description:</label>
<input type="text" name="desc" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Location:</label>
<input type="text" name="location" value="" placeholder="Enter the Country*" />
</li>
<li>
<label>Post Code:</label>
<input type="text" id="post" rel="popover" data-trigger="hover" name="postcode" placeholder="Enter the City*" />
</li>
<li class="pull-right">
<label>Salary:</label>
<input type="text" id="salaries" name="salary" placeholder="Enter the Salary*" />
</li>
<li>
<li>
<label>Qualification:</label>
<input type="text" id="qualification" rel="popover" data-trigger="hover" name="qualification" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Category:</label>
<input type="text" id="category" class="span12" required name="category" placeholder="Enter Job Tags" />
</li>
<label>Benifits:</label>
<input type="text" id="benefits" rel="popover" data-trigger="hover" name="benefits" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Job Tags:</label>
<input type="text" id="jobtags" class="span12" required name="jobtag" placeholder="Enter Job Tags" />
</li>
<li class="pull-right">
<label>Career Level:</label>
<input type="text" id="career" class="span12" required name="career" placeholder="Enter Job Tags" />
</li>
<li>
<label>Country:</label>
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country->name ?>" </option>
<?php }
?>
</select>
</li>
<li class="pull-right">
<table id="question">
<th>Add Killer Questions</th>
<tr>
<td><input type="text" id="questions" name="question[]"/></td>
</tr>
</table>
<td><input type="button" id="btnAdd" class="button-add" onClick="insertTextBox()" value="Add More"></input></td>
</li>
<br>
<input type="submit" value="Save As Draft" class="button-next" />
</div>
</form>
<div class="wid-social">
</div>
</div>
</div>
</div>
<script>
var index = 1;
function insertTextBox(){
var text = document.getElementById("question");
var row=text.insertRow(text.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "question"+index;
t1.name = "question[]";
cell1.appendChild(t1);
var cell2=row.insertCell(1);
index++;
}
</script>

In model:
Repalce
return $query->result_array();
With
return $query->result();
In View :
Repalce
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country->name ?>" </option>
<?php }
?>
</select>
With
<?php $countries = get_Country(); ?>
<?php echo form_dropdown('country',$countries, 0,'required'); ?>
In Codeigniter Use Codeigniter From Dropdown

Your model is correct, but you do need to decide if you are going to access the data as an array or an object. That will dictate how you access your data in the View.
$query->result_array() returns an array
$query->result() returns an object
In your controller you need to use your model.
// Load your model into your controller
$this->load->model('your_model');
// use your_model method to set $data['countries']
$data['countries'] = $this->your_model->get_Country();
// You will access $countries in your view by passing it to your view
$this->load->view('your_view'), $data);
In your view, if you choose $query->result() in your model :
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country): ?>
<option value="<?php echo $country->name ?>"><?php echo $country->name ?></option>
<?php endforeach; ?>
</select>

Your controller didn't return anything, so need to modify controller as below.
controller :
public function get_Country() {
return $ctry = $this->job_post_model->get_Country();
}
and also your VIEW returning result as an array, you need to use $country['name'] in the place of $country->name.
VIEW:
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country['code'] ?>"><?php echo $country['name'] ?></option>
<?php }
?>
</select>

Related

input type hidden using onclik

I have a problem when entering data into the database, where I want to enter the province_name attribute, but what is stored in the database is the id
my code
<div class="form-group col-md-4">
<label>Province Name</label>
<select name="province_order" id="provinsi" class="form-control select2" style="width: 100%;">
<option value="0">-PILIH-</option>
<?php foreach($data->result() as $row):?>
<option value="<?php echo $row->id_prov;?>"><?php echo $row->nama_provinsi;?></option>
<?php endforeach;?>
</select>
<input type="text" name="province_order">
</div>
function myFunction() {
var x = document.getElementById("provinsi").value;
document.getElementById("klik").innerHTML = x;
}
nama_provinsi not save in database, but id save.
how to save the province__name attribute
Try this:-
<div class="form-group col-md-4">
<label>Province Name</label>
<select name="province_order" id="provinsi" class="form-control select2" style="width: 100%;">
<option value="0">-PILIH-</option>
<?php foreach($data->result() as $row):?>
<option value="<?php echo $row->id_prov;?>" province="<?php echo $row->nama_provinsi;?>" ><?php echo $row->nama_provinsi;?></option>
<?php endforeach;?>
</select>
<input type="text" name="province_order">
</div>
function myFunction() {
var x = $("#provinsi").attr('province');
//document.getElementById("klik").innerHTML = x;
}

how to set and get checked or unchecked values in single checkbox using another form

i want to set value and retrieved value in the checkbox field when submitting a value from another form hidden filed.
<form action="store" method="post">
<div class="form-group">
<div class="row">
<label class="control-label col-lg-3"><b>Enable/Disable App</b>
</label>
<label class="Sg-switch">
<input type="checkbox" name="is_active" value="<?php $_POST['enable']; ?>"<?php if (isset($setting['is_active'])==1) { ?> checked="checked" <?php } ?>><span class="Sg-slider round"></span>
</label>
</div>
<div class="clearfix"></div>
<div class="form-group">
<button type="submit" class="sg-main-btn sg-primary-tn">Save</button>
My submit form is:-
<div class="modal-body">
<form class="form-horizontal" method="post" action="#" name="enable">
<div class="modal-footer" style="text-align:center;">
<input type="text" name="is_active" value="1" hidden>
<button type="submit" name="enable" class="sg-main-btn sg-success-btn dis_btn"><i class="fa fa-check"></i><i class="fa fa-spin fa-spinner" style="display: none;"></i>Yes</button>
<i class="fa fa-times"></i>No
</div>
</form>
you can use jquery.
Give your input id like id = "input".
<input type="text" name="is_active" value="1" hidden id = 'input'>
<input type="checkbox" id="input_new" name="is_active" value="<?php $_POST['enable']; ?>"<?php if (isset($setting['is_active'])==1) { ?> checked="checked" <?php } ?>>
<span class="Sg-slider round"></span>
<button id="button1">also add yourclass and type etc</button>
$('#button1').click( function() {
var active = $('#input').val();
if(active == 1){
$('#input_new').attr('checked');
}});

Iam trying to make one view for add and edit in codeigniter when i try to update html elements show but when i need to use add elements hides

I am trying to make one view for add and edit in Codeigniter when I try to update HTML elements show but when I need to use additional elements hides I know the problem is that I am using foreach what can I do to make work
<?php include_once 'Header.php'; ?>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if($Operation == 2){ foreach ($records as $value) :?>
<?php if($Operation == 1) {echo form_open_multipart('Slider_Controller/AddNewSlid');} else if($Operation == 2){echo form_open_multipart('Slider_Controller/UpdateSlid/'.$value->Slid_ID. ''); } ?>
<div class="form-group">
<label>Slider Title</label>
?>
<input type="text" name='SlidTitle' class="form-control" value='<?php if($Operation == 2){echo $value->Title; } else { echo ""; }?>' placeholder="Slider Title">
</div>
<div class="form-group">
<label>Upload Icone</label>
<input type="file" name='userfile'>
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" rows="3" name='SlidDescription'><?php if($Operation == 2){echo $value->Description; }else if($Operation == 1){ echo null;}?></textarea>
</div>
<div class="form-group">
<label>Select Slide status</label>
<select class="form-control" name='Staus'>
<option value='0'>Select status</option>
<option value='1' <?php if($Operation == 2){if ($value->Status == "1"){echo 'selected="selected"';}}?>>Active</option>
<option value='2' <?php if($Operation == 2){if ($value->Status == "2"){echo 'selected="selected"';}}?>>NonActive</option>
</select>
</div>
<?php endforeach; } ?>
<input type="submit" name='fffff' value='go'>
<button type="reset" class="btn btn-default">Reset Button</button>
<?php echo form_close(); ?>
<?php echo validation_errors('<span class="error">', '</span>'.'<br>'); ?>
</div>
<!-- /.col-lg-6 (nested) -->
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
</div>
<!-- /.panel-body -->
<?php include_once 'Footer.php'; ?>
your code only validates for $operation == 2, there is no block if $operation == 1.
You have written form for AddNewSlid inside if $operation == 2, which is never going to be true.
Rather, move out AddNewSlid form code out of $operation == 2
Try This
<?php include_once 'Header.php'; ?>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php
//$Operation = 1;
$records = array('Slid_ID' => 2,"Title"=>"vijay", "Description" => 'bangalore india', 'Status' => 1);
$value = json_decode(json_encode($records));// comment this line means then it work for add else for edit
// echo "<pre>";print_r($value);die;
?>
<form action="<?= 'Slider_Controller/AddNewSlid/' . (isset($value->Slid_ID) ? $value->Slid_ID : '') ?>">
<div class="form-group">
<label>Slider Title</label>
<input type="text" name='SlidTitle' class="form-control" value="<?= isset($value->Title) ? $value->Title : "" ?>" placeholder="Slider Title">
</div>
<div class="form-group">
<label>Upload Icone</label>
<input type="file" name='userfile'>
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" rows="3" name='SlidDescription'><?= isset($value->Description) ? $value->Description : "" ?></textarea>
</div>
<div class="form-group">
<label>Select Slide status</label>
<select class="form-control" name='Staus'>
<?php $status = isset($value->Status) ? $value->Status : '' ?>
<option value=''>Select status</option>
<option value='1' <?= ($status == "1") ? 'selected' : '' ?>>Active</option>
<option value='2' <?= ($status == "2") ? 'selected' : '' ?>>NonActive</option>
</select>
</div>
<input type="submit" name='fffff' value='go'>
<button type="reset" class="btn btn-default">Reset Button</button>
</form>
<?php echo validation_errors('<span class="error">', '</span>' . '<br>'); ?>
</div>
<!-- /.col-lg-6 (nested) -->
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
</div>
<!-- /.panel-body -->
<?php include_once 'Footer.php'; ?>
this page is for add/edit
please pass $value to edit only other wise $value should be empty
`

how to validate a field in tab?

I need that the customer field is required, if it is empty when pressing the continue button, this not must allow continue to next step and it then should appear a message indicating that the field is empty and need be completed.
Please, i need a help or advice about how to could make the validation.
Using boostrap.
<ul class="nav nav-pills nav-justified" id="NotTab">
<li class="active disabledTab">1. <?php echo $tab_customer; ?></li>
<li class="disabled disabledTab">2. <?php echo $tab_payment; ?></li>
</ul>
<div id="tab-customer" class="tab-pane active">
<div class="control-group">
<label class="control-label" style="color: #F00;"><b><?php echo $entry_customer; ?></b></label>
<div class="controls">
<input type="text" name="customer" value="<?php echo $customer; ?>" class="span3" onclick="this.select();">
<input type="hidden" name="customer_id" value="<?php echo $customer_id; ?>">
<input type="hidden" name="customer_group_id" value="<?php echo $customer_group_id; ?>">
</div></div>
<div class="form-actions">
<button class="btn btn-primary prevtab" type="button" onclick="return showNext()"><span class="glyphicon glyphicon-arrow-right"></span> Next </button></div>
</div>
<script>
var $tabs = $('#NotTab li');
function showPrev() {
$tabs.filter('.active').prev('li').removeClass("disabled");
$tabs.filter('.active').prev('li').find('a[data-toggle]').each(function () {
$(this).attr("data-toggle", "tab");
});
$tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').tab('show');
$tabs.filter('.active').next('li').find('a[data-toggle="tab"]').each(function () {
$(this).attr("data-toggle", "").parent('li').addClass("disabled");
})
}
function showNext() {
$tabs.filter('.active').next('li').removeClass("disabled");
$tabs.filter('.active').next('li').find('a[data-toggle]').each(function () {
$(this).attr("data-toggle", "tab");
});
$tabs.filter('.active').next('li').find('a[data-toggle="tab"]').tab('show');
$tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').each(function () {
$(this).attr("data-toggle", "").parent('li').addClass("disabled");;
})
}
</script>
Thanks.
You can use a simple check, to check if the input is empty.
if( !$('[name=customer]').val() ) {
alert('Input is empty');
return;
}

codeigniter select list generated from database error when submitting the form

I have created a form which has 2 select box which also generate the options from database but the problem is when I do click on submit button the select box becomes empty.
here is the codes:
View:
<ol class="breadcrumb-icon">
<li><i class="fa fa-home"></i> Home</li>
<li class="active"><i class="icon fa fa-inbox"></i> Add Courses</li>
</ol>
<div class="page-header">
<h1>Add Courses:</h1>
</div>
<div class="well">
<?php if(isset($error)) echo $error;?>
<?php
$this->load->helper('form');
$attributes = array('role' => 'form','class'=>'form-horizontal');
echo form_open_multipart('admin/addcourses',$attributes);
?>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Course Category:</label>
<div class="col-sm-10">
<select name="category" class="form-control">
<?php
foreach ($cat as $row)
{
echo "<option value='".$row->category_id."'>".$row->category_name."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Course Location:</label>
<div class="col-sm-10">
<select name="location" class="form-control">
<?php
foreach ($loc as $row)
{
echo "<option value='".$row->location_id."'>".$row->location_name."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Course Language:</label>
<div class="col-sm-10">
<select name="language" class="form-control">
<option value="English">English</option>
<option value="Arabic">Arabic</option>
</select>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Course Name</label>
<div class="col-sm-10">
<input type="text" name="coursename" class="form-control" id="inputPassword3" placeholder="">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Course Date:</label>
<div class="col-sm-10">
<input type="text" name="date" id="range-picker" readonly class="form-control kd-daterange">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Course Image</label>
<div class="col-sm-10">
<input type="file" name="course_image" class="upload" readonly class="form-control kd-upload">
<p>keep it empty to use the default image.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Add Course</button>
</div>
</div>
</form>
</div>
</div> <!-- /.container laaast -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script src="js/extended.min.js"></script>
<script>
extended = new Extended();
</script>
Controller addcourses.php
public function addcourses()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('admin/read_cat_loc');
$data['cat']= $this->read_cat_loc->read_categories();
$data['loc']= $this->read_cat_loc->read_locations();
$this->load->view('admin/header_view');
$this->load->view('admin/addcourses_view',$data);
$this->load->view('admin/footer_view');
}
else {
redirect('admin/home');
}
}
model codes
class Read_cat_loc extends CI_Model {
public function read_categories() {
$query= $this->db->get('course_category');
return $query->result();
}
public function read_locations() {
$query= $this->db->get('course_location');
return $query->result();
}
}
ok solved:
I did this in main controller section :
$this->load->model('admin/read_cat_loc');
$data['cat']= $this->read_cat_loc->read_categories();
$data['loc']= $this->read_cat_loc->read_locations();
$this->load->view('admin/header_view');
$this->load->view('admin/addcourses_view',$data);
$this->load->view('admin/footer_view');
but in other areas where I have to reload the view I did this only:
$this->load->view('admin/header_view');
$this->load->view('admin/addcourses_view',$data);
$this->load->view('admin/footer_view');
without the model part to retain the data again ...
If you know better way It will be great. Thanks

Resources