- Get link
- X
- Other Apps
Dynamically adding option to the dropdown list instenly using text box | How to Show/Hide text box when select Other from dropdown list
JavaScript Code:
$(document).on('click','.btcl', function(){
var dataString = 'Rule=comp_list';
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
cache: false,
success: function(data){
$("#company_list").html(data);
}
});
});
$(document).on('change','#enq_company', function(e){
var t=e.target.type;
//var val = $('#enq_company option:selected').val();
var val = $(this).val();
if(val=="new" || t=="text") {
var dataString = 'val='+val+'&Rule=ins_comp';
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
cache: false,
success: function(data){
$("#company_list").html(data);
}
});
}
});
HTML Page:
<select name="enq_company" id="enq_company" style="width: 310px" required>
<option value="">Select Company Name</option>
<option value="1">Google</option>
<option value="2">Yahoo</option>
<option value="3">Amazon</option>
<option value="4">Flipkart</option>
<option value="new">Other</option>
</select>
PHP Code in Ajax File:
<?
if(!empty($_REQUEST['Rule']) && $_REQUEST['Rule']=="comp_list") {
?>
<select name="enq_company" id="enq_company" style="width: 310px" required>
<option value="">Select Company Name</option>
<?php
dropdown('hmp_companies', 'comp_id,comp_name', 'comp_status=1 order by comp_name', @$insid);
?>
<option value="new">Other</option>
</select>
<?
} else if(!empty($_REQUEST['Rule']) && $_REQUEST['Rule']=="ins_comp") {
$val=secure($_REQUEST['val']);
if($_REQUEST['val']=='new' || empty($val)) {
?>
<input name="enq_company" id="enq_company" type="text" value="<? if(isset($_POST['enq_company'])) echo $_POST['enq_company']; else if(!empty($getid)) echo $row['enq_company'];?>" placeholder="Enter New Company Name" required>
<a href="#x" class="icn-link-blue tooltip btcl" title="Back to Companies List"><i class="fa fa-refresh fa-lg" aria-hidden="true"></i></a>
<?
} else {
// Insert
$results = get_data("hmp_companies","count(*) as cnt,comp_id","comp_name ='".$val."'");
if($results[0]>0){
$insid = $results[1];
}else{
$orderby=rns_max('hmp_companies','comp_order_by');
$ins_rec['comp_order_by']= $orderby;
$ins_rec['comp_added_by']= $ERP_Uid;
$ins_rec['comp_added_date']= $now_time;
$ins_rec['comp_updated_date']= $now_time;
$ins_rec['comp_name']=$val;
$ins_rec['comp_description']=$val;
$insid=insert_Defined('hmp_companies', array_map('trim',$ins_rec),1);
}
?>
<select name="enq_company" id="enq_company" style="width: 310px" required>
<option value="">Select Company Name</option>
<?php
dropdown('hmp_companies', 'comp_id,comp_name', 'comp_status=1 order by comp_name', @$insid);
?>
<option value="new">Other</option>
</select>
<?
}
}?>
Comments
Post a Comment