Friday 7 September 2018

Check if browser is Internet Explorer in jQuery

If you want to know that this browser is IE or not. You can check by following code:


<script>
function isIE() {
  ua = navigator.userAgent;

  /* MSIE used to detect old browsers and Trident used to newer ones*/

  var is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;
  
  return is_ie; 
}

/* Create an alert to show if the browser is IE or not */

if (isIE()){
alert('It is InternetExplorer');
}else{
alert('It is NOT InternetExplorer');
}
</script>






Friday 17 August 2018

Codeigniter : How to get the selected option value in controller



Get the selected option value in controller


Put this code in view:

<select name="color" id="color" class="form_input">
    <option value="Red">Red </option>
    <option value="Green">Green</option>
   <option value="Blue">Blue</option>
   <option value="Yellow">Yellow</option>
   <option value="Pink">Pink</option>
</select>

Now, add following code in controller for get selected value : 

<?php $color= $this->input->post("color"); ?>



Now, get multiple selected option values in contorller


Put this following code in view:

<select  class="default " id="color" name="color[]">
   <option value="">Select color</option>
   <?php foreach ($colors as $color) {  ?>
   <option value="<?php echo $color->color_id;?>">
   <?php echo $color->color_name;?>
   </option>
   <?php } ?>
</select>


Now, add following code in controller or model for get selected values : 

<?php $colors = $this->input->post("color"); ?>










Thursday 16 August 2018

Codeigniter : Check if id already exists in database

Check if any id already exists in database table and not exist id, insert in table.



$product_category = $this->input->post('product_category');    // product_category is multiple checkbox name


$dataCatBatch = array();
foreach($product_category as $_category){
$ros = $this->db->get_where('product_category',array('product_id'=>$productId,'category_id'=>$_category))->num_rows();
if($ros == 0){
$dataCatBatch[] = array('product_id'=>(int)$productId,'category_id'=>(int)$_category);
}
}

if(count($dataCatBatch) > 0){
$this->db->insert_batch('product_category',$dataCatBatch);
}

Description : product_category is table name. You can change column name according your table column name. 

Jquery : Sum of multiple input fields in one input

Sum of multiple input fields if all input fields have same class in one input field by Jquery

<input type="text" class="pro_qty" value="" />

<input type="text" class="pro_qty" value="" />
<input type="text" class="pro_qty" value="" />
<input type="text" class="pro_qty" value="" />
<input type="text" class="pro_qty" value="" />
<input type="text" class="pro_qty" value="" />
<input type="text" class="total_qty" value="" />
<script>
$(document).on("change", ".pro_qty", function() {
var sum = 0;

$(".pro_qty").each(function(){
sum += +$(this).val();
});

$(".total_qty").val(sum); });
</script>