validation not working on multiple select - codeigniter

here i have given validation for multiple select and even if i choose value from select field and gave submit then also its telling to select the field and am using codeigniter 3.0.6. searched alot but couldn't find the solution;
Here is my controller
function online_booking() {
$this->form_validation->set_rules('course', 'Course', 'required');
$this->form_validation->set_rules('branch', 'Branch', 'required');
if($crs = $this->input->post('course'))
{
$course = implode(',',$crs);
var_dump($course);
}
if ($this->form_validation->run() == TRUE)
{
$this->load->model('branch_model');
$data = array('course' => $course, 'branch' => $this->input->post('branch'));
$id = $this->branch_model->insert_enquiry($data);
if ($id)
{
$this->session->set_flashdata('message', 'Booking has been Succeed,We will contact you shortly.');
redirect('online-booking');
}
else
{
$this->session->set_flashdata('message', 'Error has been occured,try again.');
redirect('online-booking');
}
}
$data['active'] = 'online';
$data['program'] = $this->home_model->get_program();
$this->load->view('online_bookin', $data);
}
here is my view page
<div class="form-group col-md-6">
<label for="exampleInputEmail1">Course to be opted*</label>
<select class="form-control" name="course[]" id="course" multiple="multiple">
<option value="">Select Course</option>
<?php foreach ($program->result() as $row) if($row->parent_id !=0) {?>
<option value="<?php echo $row->id; ?>" <?php echo set_select('course',$row->id) ?> ><?php echo $row->name; ?></option>
<?php } ?>
</select>
</div>
this is my callback function
function is_multiple_select() {
$crs=$this->input->post('course');
if(!$crs)
{
$this->form_validation->set_message('is_multiple_select','You did not select any course to upload.');
return false;
}
else
{
return true;
}
}
this is my model
public function insert_enquiry($data=array())
{
if($this->db->insert(`enquiryform`,$data))
{
return $this->db->insert_id();
}
else
{
return false;
}
}

Why are you using this code:
if($crs = $this->input->post('course'))
{
$course = implode(',',$crs);
var_dump($course);
}
Remove it and check if it works. Or place it inside the $this->form_validation->run() == TRUE block.

function is_multiple_select() {
$crs=$this->input->post('course');
if(!empty($crs) && count($crs)>0)
{
return true;
}
else
{
$this->form_validation->set_message('is_multiple_select','You did not select any course to upload.');
return false;
}
}
//Changes
if ($this->form_validation->run() == TRUE)
{
//You can't save an array directly.
$data = array('course' => implode(",",$this->input->post('course') ), 'branch' => $this->input->post('branch') );
$this->db->insert(`enquiryform`,$data);
$insert_id = $this->db->insert_id();
}
while retrieving explode the course it.

Related

Dependent dropdown, second dropdown not populated, Codeigniter

I have a dependent drop down that get the category on the first drop down and should get the subcategory on the second drop down but the subcategory drop down isn't populated. I have something like this in my Model:
public function category()
{
$response = array();
$this->search(array(), 'date_created');
$this->db->select('*');
$query = $this->db->get('categories');
$response = $query->result_array();
return $response;
}
public function sub_category($parent_id)
{
$query = $this->db->get_where('sub_categories', array('parent_id' => $parent_id));
return $query->result_array();
}
And then something like this on my Controller:
public function edit_product($id)
{
$data['title'] = 'Update Product';
$this->load->view('../admin/template/admin_header');
$products = new Admin_model;
$data['products'] = $products->edit_product($id);
$data['category'] = $this->Admin_model->category();
$data['subcategory'] = $this->Admin_model->sub_category($data['products']->category_id);
$this->load->view('../admin/template/admin_topnav');
$this->load->view('../admin/template/admin_sidebar');
$this->load->view('../admin/products/manage_product', $data);
$this->load->view('../admin/template/admin_footer');
}
function get_subcategory()
{
if ($this->input->post('parent_id')) {
echo $this->Admin_model->get_subcategory($this->input->post('parent_id'));
}
}
public function insert_product()
{
$productData = array();
if ($this->input->post('productData')) {
$this->form_validation->set_rules('category_id', 'Category', 'required');
$this->form_validation->set_rules('sub_category_id', 'Sub Category', 'required');
$this->form_validation->set_rules('product_name', 'Product Name', 'required');
$this->form_validation->set_rules('department', 'Department', 'required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('status', 'Status', 'required');
if ($this->form_validation->run() == true) {
$ori_filename = $_FILES['product_image']['name'];
$update_image = time() . "" . str_replace(' ', '-', $ori_filename);
$config = [
'upload_path' => './images/products',
'allowed_types' => 'gif|jpg|png',
'file_name' => $update_image,
];
$this->load->library('upload', $config);
if (!$this->upload->do_upload('product_image')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view(base_url('admin/products'), $error);
} else {
$image = $this->upload->data('file_name');
$productData = array(
'category_id' => $this->input->post('category_id'),
'sub_category_id' => $this->input->post('sub_category_id'),
'product_name' => $this->input->post('product_name'),
'department' => $this->input->post('department'),
'description' => $this->input->post(htmlentities('description')),
'status' => $this->input->post('status'),
'upload_path' => $image
);
$img = new Admin_model;
$img->insert_product($productData);
$this->session->set_flashdata('status', 'Package InsertedSuccesfully');
redirect(base_url('admin/products'));
}
}
}
$data['title'] = 'Insert Product';
$data['category'] = $this->Admin_model->category();
$data['subcategory'] = $this->Admin_model->get_subcategory();
$this->load->view('../admin/template/admin_header');
$this->load->view('../admin/template/admin_topnav');
$this->load->view('../admin/template/admin_sidebar');
$this->load->view('../admin/products/manage_product', $data);
$this->load->view('../admin/template/admin_footer');
}
And then the view:
<div class="form-group">
<label for="" class="control-label"> Category</label>
<select name="category_id" id="category" class="custom-select select">
<option value="">Select Category</option>
<?php
foreach ($category as $row) {
echo '<option value="' . $row['id'] . '"';
if (isset($products) && $row['id'] == $products->category_id) {
echo ' selected';
}
echo '>' . $row['category'] . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="" class="control-label">Sub Category</label>
<select name="sub_category_id" id="subcategory" class="custom-select select">
<option value="">Select Sub Category</option>
<?php
foreach ($subcategory as $row) {
echo '<option value="' . $row['id'] . '"';
if ($row['id'] == $products->sub_category_id) {
echo ' selected';
}
echo '>' . $row['sub_category'] . '</option>';
}
?>
</select>
</div>
And here's the script. I'm not really familiar at AJAX so i tried to ask and get answers here which helps me progress but I still can't populate the subcategory drop down.
<script type="text/javascript">
$(document).ready(function() {
$('#category').change(function() {
var parent_id = $('#category').val();
console.log(parent_id)
if (parent_id != '') {
$.ajax({
url: "<?php echo base_url(); ?>admin/get_subcategory",
method: "POST",
data: {
parent_id: parent_id
},
success: function(data) {
$('#subcategory').html(data);
console.log(data)
}
});
} else {
$('#subcategory').html('<option value="">Select Category First</option>');
}
});
});
Also, here's what I get in the console
The result from $this->Admin_model->sub_category(...) is an array.
echo works on string values only, which is why you're getting the error. You'll need to loop over the result array (in admin/get_subcategory) and print the values one by one. Also surround each value with an <option> tag so the result can be placed in the select box without further modification:
public function get_subcategory() {
if ($this->input->post('parent_id')) {
$subcategories = $this->Admin_model->sub_category($this->input->post('parent_id'));
foreach ($subcategories as $subcategory) {
echo '<option value="'.$subcategory['id'].'">'.$subcategory['sub_category'].'</option>';
}
}
}

How to autocomplete select option in CodeIgniter

I'm trying to make a select option in CodeIgniter, and want to make it autocomplete fill. How do I do that?
This is my code:
Model:
public function get_nama_customer(){
$data = array();
$query = $this->db->get('tabel_customer');
if($query->num_rows() > 0){
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Controller:
public function add_data()
{
$this->load->model('model_tabel_material');
$data['tabel_customer'] = $this->model_tabel_material->get_nama_customer();
$this->load->view('datamaster/tabel_material/v_add_material', $data);
}
public function do_add()
{
$nama_customer = $_POST['nama_customer'];
$data_add = array(
"nama_customer" => $nama_customer,
);
$temp = $this->model_tabel_material->insert_data('tabel_material', $data_add);
if($temp >=1){
redirect('tabel_material/controller_tabel_material/index');
}
}
View:
<select class="form-control" name="nama_customer" id="nama_customer" required>
<?php if(count($tabel_customer)){ ?>
<option value=''>--Pilih Customer--</option>
<?php foreach ($tabel_customer as $list){ ?>
<?php
echo "<option value='".$list['nama_customer']."'>".$list['nama_customer']."</option>";
?>
<?php } ?>
<?php } ?>
</select>

how to store multiple selected dropdown values in database in codeingiter

i have two tables in my database one is requirements another one is users..i have those user names in my requirements form with select box..how to store that user names id in database..
This is my view:
<div class="form-group">
<label>Choose Vendor</label>
<select class="form-control" multiple class="form-control" data-placeholder="user name" name="user_id[]" >
<option value="0"></option>
<?php foreach($user as $rows) { ?>
<option value="<?php echo $rows->user_id?>"><?php echo ucfirst($rows->first_name)?></option>
<?php } ?>
</select>
</div>
This is my controller:
public function requirement()
{
$data["msg"]="";
$this->load->model('RequirementModel');
$data['user']=$this->RequirementModel->getusers();
if($this->input->post())
{
$this->RequirementModel->add_requirement($this->input->post());
redirect(base_url('index.php/Login/dashboard'));
}
$this->load->view('Requirements/requirements',$data);
}
This is my model:
function getusers()
{
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
//echo $this->db->last_query();
return $query->result();
}
model add_requirement:
public function add_requirement($data)
{
$data=array('role_name'=>$post['role_name'],'vacancies'=>$po‌​st['vacancies'],'exp‌​erience'=>$post['exp‌​erience'],'jd'=>$pos‌​t['jd'],'hiring_cont‌​act_name'=>$post['hi‌​ring_contact_name'],‌​'hiring_contact_numb‌​er'=>$post['hiring_c‌​ontact_number'],'use‌​r_id'=>$user_id);
$this->db->insert('requirements', $data);
}
how to do this?
Thank you
Change your function add_requirement like below and try;
function add_requirement($data)
{
if(isset($data['user_id']) && !empty($data['user_id'])) {
$data['user_id'] = implode(',',$user_id);
}
$data=array('role_name'=>$post['role_name'],'vacancies'=>$po‌​st['vacancies'],'exp‌​erience'=>$post['exp‌​erience'],'jd'=>$pos‌​t['jd'],'hiring_cont‌​act_name'=>$post['hi‌​ring_contact_name'],‌​'hiring_contact_numb‌​er'=>$post['hiring_c‌​ontact_number']);
$this->db->insert('requirements', $data);
}
}

A PHP error was encountered severity:trying to get non-object file name:

view part where the error occured
<select name="standard" class="form-control">
<option <?php if($course->medium =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course->medium =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>
</select>
model part
public static function getStandard($id){
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
foreach($res->result() as $value) {
return $value;
}
}
controller part
public function add_standard($action='',$id='') {
$this->load_syles();
$course = array();
if ($action == 'edit' and $id != '' and is_numeric($id)) {
$course = $this->news_model->getStandard($id);
}
$this->data['course'] = $course ;
$this->data['title'] = 'Add Standard';
$this->data['page_header'] = "Add Standard";
$this->data['page_header_desc'] = "Add Standard";
$details=$this->news_model->viewAllStandard();
$this->data['details']=$details;
$this->data['count']=count($details);
$this->data['is_edit']=$action;
You wrote
$course = array()
and then used
$course->medium
as if it was an object
you should use $course['medium'] in the view(html)
Change your model like
public static function getStandard($id)
{
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
return $res->row_array();
}
Change $course->medium to $course['medium']
<select name="standard" class="form-control">
<option <?php if($course['medium'] =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course$course['medium'] =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>

Magento - How to validate my radio button?

in my custom module, I have a form which has radio buttons.
When I click the submit button it does not validate the radio button.
<?php
$question = Mage::getModel('emme_question/question')->getCollection()->getLastItem();
$answers = $question->getSelectedAnswersCollection();
?>
<h4><?php echo $this->escapeHtml($question->getValue()); ?></h4>
<ul>
<?php foreach ($answers as $answer): ?>
<li>
<label><?php echo $this->escapeHtml($answer->getValue()) ?></label>
<input class="required required-field" type="radio" name="my_custom_answer" value="<?php echo $answer->getId() ?>" required>
</li>
<?php endforeach ?>
and
<?php
// app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php
class Envato_Custompaymentmethod_Model_Paymentmethod extends Mage_Payment_Model_Method_Abstract {
protected $_code = 'custompaymentmethod';
public function validateRadioIsSelected()
{
$var options = $$('input.Classname');
for( i in options ) {
if( options[i].checked == true ) {
return true;
}
}
return false;
}
public function getOrderPlaceRedirectUrl()
{
return Mage::getUrl('custompaymentmethod/payment/redirect', array('_secure' => false));
}
}
Parse error: syntax error, unexpected 'options' (T_STRING) in /home/mmstore9/public_html/demo/app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php on line 27
use this magetno default validation class to validate the radio button
validate-one-required-by-name
OR
validate-one-required
I solved
public function validate()
{
foreach (Mage::getModel('emme_question/question')->load(1)->getSelectedAnswersCollection() as $answer)
{
if ($answer->getIsCorrect())
{
if ($answer->getId() == $_POST['my_custom_answer'])
{
Mage::getSingleton('core/session')->addSuccess('Risposta esatta');
} else
{
Mage::throwException('Risposta sbagliata!');
}
}
}
}

Resources