For drop down. I want to apply validation for update drop down. Everything works fine. Updated value inserted in data base - codeigniter-3

But when i select drop down value and any error related firstname etc. and on update button it remove selected value and show old value.
<td align=left>Select Month:<select name=month id="month">
<?php
$date = $row->birth;
$stamp = strtotime($date);
?>
<?php
echo '<option .date("m", $stamp). '.$selected.'>'.date("m", $stamp).'</option>';
?>
<option name=""></option>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='12'>December</option>
</select>
<?php echo form_error('month','<div style="color:red">', '</div>'); ?>
<span class="error"></span>
</td>

You can use codeigniter's set_select function to keep the selection if form validation fails:
Select Month:<select name=month id="month">
<option name=""></option>
<option value='01' <?php echo set_select('month', '01'); ?>>January</option>
<option value='02' <?php echo set_select('month', '02'); ?>>February</option>
<option value='03' <?php echo set_select('month', '03'); ?>>March</option>
<option value='12' <?php echo set_select('month', '12'); ?>>December</option>
</select>
See also: https://www.codeigniter.com/userguide3/helpers/form_helper.html#set_select
If you have a value you'd like to set in the dropdown when the page first loads, you can use the third argument:
Select Month:<select name=month id="month">
<?php
$date = $row->birth;
$stamp = strtotime($date);
$month = date("m", $stamp);
?>
<option name=""></option>
<option value='01' <?php echo set_select('month', '01', $month == '01'); ?>>January</option>
<option value='02' <?php echo set_select('month', '02', $month == '02'); ?>>February</option>
<option value='03' <?php echo set_select('month', '03', $month == '03'); ?>>March</option>
<option value='12' <?php echo set_select('month', '12', $month == '12'); ?>>December</option>
</select>

Related

Undefined variable in Option Value CodeIgniter

What I'm I doing wrong?
application/views/pegawai_read.php
<select name="" id="" class="form-control">
<?php
foreach($nm_berkas_jn->result() as $j){
?>
<option value="<?php echo $j->id_berkas_jn;?>"><?php echo $j->nm_berkas_jn;?></option>
<?php } ?>
</select>
application/controllers/pegawai.php
public function read($id) {
$id_berkas_jn=$this->session->userdata('id_berkas_jn');
$text = "SELECT * FROM kpg_berkas_jn";
$d['nm_berkas_jn'] = $this->app_model->manualQuery($text);
$d['level']=$this->session->userdata('level');
}
application/models/app_model.php
function manualQuery($q) {
return $this->db->query($q);
}
Add the following line to the bottom of your controller. This send the data to the view
$this->load->view('pegawai_read', $d);

Codeigniter multiple language cause facebook share error why?

I have a project with multiple language when I try to share it on facebook I got this error.
File: path/Language.php: 15 function:_error_handler.
function switch_lang($language = "") {
$language = ($language != "") ? $language : "english";
$this->session->set_userdata('site_lang', $language);
setcookie('site_lang', $language, time() + (86400 * 30), "/");
redirect($_SERVER['HTTP_REFERER']);//This is Line 15
//When I remove the redirect above it work.
}
And this is how the switch_language function get call.
<select class="form-control" onchange="javascript:window.location.href='<?php echo base_url(); ?>language/switch_lang/'+this.value;">
<option value="korean" <?php if($this->session->userdata('site_lang') == 'korean') echo 'selected="selected"'; ?> > 한국어 </option>
<option value="chinese" <?php if($this->session->userdata('site_lang') == 'chinese') echo 'selected="selected"'; ?> > 中文 </option>
</select>
I sovel the problem by using javascript history.back function instead of php redirect
echo '<script> window.history.back();</script>';

Opencart get select value to controller for filter

I have page called no.tpl, in this page am displaying customer name in select dropdown
this is the code:
no.tpl
<select name="customer_id" id="customer" style="width: 325px;margin-bottom:10px" class="form-control">
<?php foreach($customerData as $customer){ ?>
<option value=<?php echo $customer['customer_id']?>><?php echo $customer['customer_name']?></option>
<?php }?>
</select>
In controller page i have to filter selected customer list
$queryCustomer = $this->db->query("select customer_id, concat(firstname, ' ',lastname) as name, email from " . DB_PREFIX . "customer where customer_id='6'");
$selectedCustomer = $queryCustomer->row;
$selectedCustomerId = $selectedCustomer['customer_id'];
$selectedCustomerName = $selectedCustomer['name'];
$selectedCustomerEmail = $selectedCustomer['email'];
I want customer_id='6' as selected customer_id. I mean pass the select value to controller page
Try this code in view page
<select name="customer_id" id="input-sales-person" style="width: 325px;margin-bottom:10px" class="form-control">
<?php foreach($customerData as $customer){ ?>
<option id="temp" value=<?php echo $customer['customer_id']?>><?php echo $customer['customer_name']?></option>
<?php }?>
</select>
<input type="submit" id="noOrder" Onclick="return ConfirmDelete();" value="Submit" class="btn btn-primary">
Use this following script
<script type="text/javascript">
$('#noOrder').on('click', function() {
var temp1=$( "#input-sales-person option:selected" ).val();
var temp2=$( "#input-sales-person option:selected" ).text();
document.cookie = "selectedCustomerId=" +temp1;
document.cookie = "selectedCustomerName=" +temp2;
location="index.php?route=sale/no";
});
</script>
In controller pass the customer_id as $selectedCustomerId=$_COOKIE['selectedCustomerId'];
$selectedCustomerId=$_COOKIE['selectedCustomerId']; /*customer_id=6*/
$queryCustomer = $this->db->query("select customer_id, concat(firstname, ' ',lastname) as name, email from " . DB_PREFIX . "customer where customer_id='".$selectedCustomerId."'");

How to populate values in drop down list in php postgresql

I am getting error while populating the values in drop down list using php postgresql. Error means no output in drop down list.
Below is my code.
<!DOCTYPE html>
<html>
<body>
<form name="form1" id="form1" action="" method="post">
<select name="selectid" Id="select">
<option value="">--- Select ---</option>
<?php
// postgresql database connection
include ('dbconfig.php');
$list = pg_query($db, "select id, name from codevalue ");
while($row_list=pg_fetch_array($list)){
?>
<option value=<?php echo $row_list["id"]; ?>>
<?php echo $row_list["name"]; ?>
</option>
<?php
}
?>
</select>
?>
<button type="submit" name="submit" >Submit</button>
</form>
</body>
</html>
Thanks in advance.
Change
while($row_list=pg_fetch_array($list)){
to
while($row_list=pg_fetch_assoc($list)){
You are retrieving an array and not an associated value. As an array you'd have to reference column and row.
Change your loop to an assoc instead of an array. Try this example:
Check if you have any rows in the database.
if (pg_num_rows($list) == 0)
{
echo "<option>No rows exist</option>";
} else {
while($row_list = pg_fetch_assoc($list)){
?>
<option value=<?php echo $row_list["id"]; ?>>
<?php echo $row_list["name"]; ?>
</option>
<?php
}
}

Fetch records count using PHP and MongoDB

I have an HTML page with a department selectbox. when end user selects any department I have no show him the no of employs in that department. The employee collect
<select multiple="" class="chosen-select choice" id="form-field-select-4" data-placeholder="Choose a Department..." onchange="show_employee_count(this.value)">
<option value="Management">Senior Management</option>
<option value="Legal">Legal</option>
<option value="Operations">Operations</option>
<option value="Administration">Administration</option>
<option value="R&D">R&D</option>
<option value="Sales & Marketing">Sales & Marketing</option>
<option value="Finance">Finance</option>
<option value="Technology">Technology</option>
<option value="Customer Service">Customer Service</option>
<option value="Pro Services">Pro Services</option>
<option value="HR">HR</option>
</select>
The count of employee to show in this container div
<div id="emp_count"></div>
I have tried this as :
HTML
<select name='department' id='department'>
<option value="Management">Senior Management</option>
<option value="Legal">Legal</option>
<option value="Operations">Operations</option>
<option value="Administration">Administration</option>
<option value="R&D">R&D</option>
<option value="Sales & Marketing">Sales & Marketing</option>
<option value="Finance">Finance</option>
<option value="Technology">Technology</option>
<option value="Customer Service">Customer Service</option>
<option value="Pro Services">Pro Services</option>
<option value="HR">HR</option>
</select>
<div id="leads_coount"></div>
JS Code
<script type="text/javascript">
$(function(){
$("#department").change(function(){
var selectedDepartment = $(this).find(":selected").val();
console.log("the department you selected: " + selectedDepartment);
$.ajax({
type: "POST",
url: "ajax.php",
cache: false,
data: { method: "get_lead_count", department: selectedDepartment }
})
.done(function( leads ) {
$( "#leads_coount" ).html(leads);
}).fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
});
</script>
ajax.php
<?php require_once('DataAccessBean.class.php');
if($_POST['method'] == 'get_lead_count') {
// select a collection
$collection = new MongoCollection($db, 'employs');
// find the collections with department
$clause = array('Department' => $_POST['department']);
$cursor = $collection->find($clause)->count();
echo 'There are '.$cursor.' employees available in this department';
//echo $_POST['department'];
}
Is there a better way to do?