I am developing a combo box whenever i select something in the contractor column( as per attach in the link) , the vehicle combo box will retrieve the info from database based on the contractor selection.
Problems faced:
Vehicle combo box can't display the data based on the contractor selection.
Illustration about the interface design:
I've attached in the first part of the code which is my HTML table code.
<td>
<select class="form-control" name='opt_contractor[]' id='opt_contractor' onChange="getState(this.value);" <?php if ($view==1) {?> disabled <?php }?> >
<option>-- Select One --</option>
<?php
foreach ($get_contractor as $contractor ){
?>
<option value='<?php echo $contractor->ref_id;?>'<?php if($contractor->ref_id == $reqtype){echo ' selected';}?>><?php echo $contractor->ref_desc;?>
</option>
<?php } ?>
</select>
</td>
<td>
<select class="form-control" name="opt_vehicle[]" id="opt_vehicle" >
<option value="">-- Select Vehicle --</option>
</select>
</td>
Script
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getState(countryId)
{
var strURL="refresh.php?country="+countryId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
document.getElementById('opt_vehicle').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
PHP CODE
$query ="SELECT ref_id, ref_desc FROM ref_mst WHERE ref_val5 = '" . $country . "'";
$result = $db->query($query);
?>
<select name="opt_vehicle" >
<option>Select State</option>
<?php while($row=mysqli_fetch_assoc($result)) { ?>
<option value=<?php echo $row['ref_id'] ?> ><?php echo $row['ref_desc'] ?>
</option>
<?php } ?>
</select>
For this part( refer php code), i've made several attempt to fix the problem occur, i've attached the problem in below as well.
1. <?php while($row=mysqli_fetch_assoc($result)) { ?>
2. <?php while($row=mysqli_fetch_array($result)) { ?>
3. <?php while($row=mysqli_fetch_object($result)) { ?>
Illustration about the error:
Just Viewed your PHP Code
I think there is a Issue in your PHP Code Kindly Replace it with my code below
<?php
//re updated the query code instead , now used the AND and markers
$query ="SELECT `ref_id` AND `ref_desc` FROM `ref_mst` WHERE `ref_val5` = '.$country.'";
$result = $db->query($query);
?>
<select name="opt_vehicle" >
<option>Select State</option>
<?php while($row=mysqli_fetch_assoc($result)) { ?>
<?php
foreach ($row as $key => $value) {
echo $value;
echo $tableRow[$key];
}
?>
<option value="<?php echo $tableRow['ref_id']; ?>"><?php echo $tableRow['ref_desc']; ?></option>
<?php } ?>
</select>
To Debug your Self
i was Echo'ed there
echo $value;
There is Issues with the Quotation i Think.. So give a try and Update me.. Thank you..
and Kindly Try to use PDO Prepared Statements or Mysqli Prepared statements, in Your Normal Code Your are using Direct Queries to DB.
I've resolved my problem, instead of using
<?php while($row=mysqli_fetch_assoc($result)) { ?>
I used this method
$results = $db->get_results($query);
<?php
foreach($results as $vhc) {
?>
<option value='<?php echo $vhc->ref_id;?>'><?php echo $vhc->ref_desc; ?> </option>
I've attached in the modification of the codes i've made to resolved my issue.
if(!empty($contract)) {
$query = "SELECT ref_id, ref_desc FROM ref_mst WHERE ref_val5 = '" .$contract. "'";
$results = $db->get_results($query);
?>
<select name="opt_vehicle" >
<option value="">Select Vehicle ....</option>
<?php
foreach($results as $vhc) {
?>
<option value='<?php echo $vhc->ref_id;?>'><?php echo $vhc->ref_desc; ?> </option>
<?php
} ?>
<select>
<?php }
Thank you once again to Ajmal Praveen for your assistance.
Related
I'm building a drop-down list with options containing data from a database and want to hide options which have zero data.
I have tried an if statement using continue but failing to catch the live data values from the database.
<select name='Database' title="Choose from database">
<option value="">All</option>
<?php foreach($database as $row):
if ($row['topic'] == 0) {
continue;
}
else {
?>
<option value="<?= $row['topic']; ?>"
<?php if ($row['topic'] == $_SESSION['prosess']){echo "
selected";}?>>
<?= $row['topic']; ?>
<?php }?>
</option>
<?php endforeach; ?>
</select>
Is there any clever javascript-, php-, etc. code that can deactivate/hide options from a database which are empty.
Add this in your css:
select option:empty {
display:none
}
I think you should try this.
<select name='Database' title="Choose from database">
<option value="">All</option>
<?php
if(count($database) > 0)
{
foreach($database as $row)
{
?>
<option value="<?= $row['topic']; ?>"
<?php if ($row['topic'] == $_SESSION['prosess']){echo "
selected";}?>>
<?= $row['topic']; ?>
<?php }?>
</option>
<?php
}
}
?>
</select>
Hope it helpful for you.
I'm trying to do the following. I am selecting an item from a combobox then I want the values of the next combobox to be the result of a query based on the first selection. This is what I have so far.... HTML
<fieldset>
<div class="form-group">
<select class="custom-select" name="serviciuSelect" id="serviciuSelect" onchange="arataAngajat()">
<option selected="">Selecteaza serviciul dorit</option>
<?php foreach ($servicii as $item): ?>
<option value="<?php echo $item['denumire']; ?>"><?php echo $item['denumire']; ?></option>
<?php endforeach;?>
</select>
</div>
</fieldset>
<fieldset>
<div class="form-group">
<select class="custom-select" name="angajatSelect" id="angajatSelect" style="visibility: hidden">
<option selected="">Selecteaza angajatul dorit</option>
<!-- <?php foreach ($angajati as $item): ?>
<option value="<?php echo $item['nume']; ?>"><?php echo $item['nume']; ?></option>
<?php endforeach;?> -->
</select>
</div>
</fieldset>
JavaScript
<script>
function arataAngajat() {
document.getElementById("angajatSelect").style.visibility = "visible";
var e = document.getElementById("serviciuSelect").value;
document.cookie = "selectSer = " + e;
}
</script>
PHP
<?php
function getSelectServiciu(){
$selectServiciu= $_COOKIE['selectSer'];
$query = "SELECT COD_A FROM angajat WHERE COD_A IN (
SELECT COD_A FROM angajat_serviciu WHERE COD_S='".
$selectServiciu."')";
$result = $this->db->query($query)->result_array();
return $result;
}
?>
I got stuck when I had to call the PHP function getSelectServiciu(). Also, If there's a better way of doing this, please let me know.
Thanks,
Tibi
this script is get data from server under a service display as a combobox options
services.php
<?php
//i'm using array u can use mysql array
$services=array();
$services[]=array('name'=>'Flowers');
$services[]=array('name'=>'Fruits');
//data under services
$data=array();
$data['Flowers']=array('Rose','Belly');
$data['Fruits']=array('Banana','Mango');
//check the the client is requested for data under a service
if(isset($_GET['get_data'])){
$current_service=$_GET['get_data'];
$data_array=$data[$current_service];
echo '<option selected="">Select</option>';
foreach($data_array as $op){
echo '<option value="'.$op.'">'.$op.'</option>';
}
}else{
?>
<fieldset>
<div class="form-group">
<select class="custom-select" name="Services" id="Services" onchange="Getnext_option(this.value)">
<option selected="">Select</option>
<?php foreach ($services as $item): ?>
<option value="<?php echo $item['name']; ?>"><?php echo $item['name']; ?></option> <?php endforeach;?>
</select>
</div>
</fieldset>
<fieldset>
<div class="form-group">
<select class="custom-select" name="Loaded_data" id="Loaded_data" style="display: none"> </select>
</div>
</fieldset>
<script>function Getnext_option(id){
var load_to=document.getElementById('Loaded_data');
load_to.style.display="none";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
load_to.innerHTML=
this.responseText;
load_to.style.display="block";
}
};
xhttp.open("GET","services.php?get_data="+id, true);//get data from server
xhttp.send();
}</script>
<?php } ?>
I have this code
<?php for($i=1;$i<=5;$i++){ ?>
<select name="bonus<?php echo $i;?>" style="margin-top:30px;">
<?php foreach($bonusable as $bns) {?>
<option value="<?php echo $bns['type'];?>"><?php echo $bns['name'];?></option>
<?php } ?>
</select>
<?php }?>
This code display the five select and these have same options with the same value , and i need to delete the option if is selected in another select tag.. I have an image that shows what this code displays
please help me, and sorry for my English!
Try this below code
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<div>
<?php for($i=1;$i<=5;$i++){ ?>
<select id="bonus<?php echo $i;?>" name="bonus<?php echo $i;?>" onchange="Process_form('<?php echo $i;?>',this.value)" style="margin-top:30px;" >
<option value="1">1</option>
<option value="2">2</option>
</select>
<?php }?>
</div>
<script>
function Process_form(Id,Value)
{
<?php for($i=1;$i<=5;$i++){ ?>
var i='<?php echo $i; ?>';
if(i!=Id)
{
$("#bonus"+i+" option[value="+Value+"]").attr('disabled','disabled');
}
<?php } ?>
}
</script>
This is the solution for this.. i tried everything but this give me an error when i click submit:
Undefined index: bonus1 ..
<script>
var $select = $("select");
$select.on("change", function() {
var selected = [];
$.each($select, function(index, select) {
if (select.value !== "") { selected.push(select.value); }
});
$("option").prop("disabled", false);
for (var index in selected) { $('option[value="'+selected[index]+'"]').prop("disabled", true); }
});
</script>
Can someone help me on how to link up 2 dropdown list which data came from the same table in the database, is it possible to do this way because I can only find a solution which there must be 2 table in the database to link up those two. this is how should it work, 1st, the user choose the department and the 2nd dropdown will show only the people that assigned from that particular department.
database table has id,name,department.
the department works already but the 2nd dropdown didnt work to filter from the department. Can someone help me to know what is wrong with this?
index.php
<?php
$query ="SELECT DISTINCT company from hrar";
$results = $db_handle->runQuery($query);
?>
<script>
function getName(val) {
$.ajax({
type: "POST",
url: "getName.php",
data:'department='+val,
success: function(data){
$("#nameList").html(data);
}
});
}
function selectDepartment(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
<label>Department:</label><br/>
<select name="country" id="country-list" class="demoInputBox" onChange="getName(this.value);">
<option value="">Select Department</option>
<?php
foreach($results as $country) {
?>
<option value="<?php echo $country["id"]; ?>"><?php echo $country["company"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Name:</label><br/>
<select name="state" id="nameList" class="demoInputBox">
<option value="">Select Name</option>
</select>
//below is the getName.php
if(!empty($_POST["company"])) {
$query ="SELECT DISTINCT name from hrar = '" . $_POST["company"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select Name</option>
<?php
foreach($results as $name) {
?>
<option value="<?php echo $name["id"]; ?>"><?php echo $name["name"]; ?></option>
<?php
}
}
?>
Try this.
<select name="filter" id="filter" onchange="this.form.submit();">
<option value="name">Select Name</option>
<?php
$sql = "SELECT DISTINCT name FROM hr";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>";
}
?>
</select>
I am using dependent drop down in my project. I have checked my code several time but still there was an error.
Data has loaded to 'Category' drop down. when Iam select item from 'Category' following message will be displayed. Not data loaded into 'Sub category'.
"There was a problem while using XMLHTTP"
please look at my code
<!-- /.Category -->
<div class="form-group">
<label>Product Category</label>
<select name="category_id" class="form-control col-sm-5" id="category" onchange="get_category(this.value)">
<option value="">Select Product Category</option>
<?php if (!empty($category)): ?>
<?php foreach ($category as $v_category) : ?>
<option value="<?php echo $v_category->category_id; ?>"
<?php
if (!empty($product_info)) {
echo $v_category->category_id == $product_category->category_id ? 'selected' : '';
}
?> >
<?php echo $v_category->category_name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<!-- /.Sub Category -->
<div class="form-group">
<label>Subcategory<span class="required">*</span></label>
<select name="subcategory_id" class="form-control col-sm-5" id="subcategory">
<option value="">Product Subcategory</option>
<?php if (!empty($subcategory)): ?>
<?php foreach ($subcategory as $v_subcategogy) : ?>
<option value="<?php echo $v_subcategogy->subcategory_id; ?>"
<?php
if (!empty($product_info)) {
echo $v_subcategogy->subcategory_id == $product_info->subcategory_id ? 'selected' : '';
}
?> >
<?php echo $v_subcategogy->subcategory_name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
AJAX CODE (i think issue in the response (req.status == 200) part.)
//*********************************************
// Product Category to Subcategory
//*********************************************
function get_category(str) {
if (str == '') {
$("#subcategory").html("<option value=''>Select Subcategory</option>");
} else {
$("#subcategory").html("<option value=''>Select Subcategory</option>");
var link = getBaseURL();
var strURL = link + "admin/product/get_subcategory_by_category/" + str;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
var result = req.responseText;
//alert(result);
$("#subcategory").html("<option value=''>Select Subcategory</option>");
$("#subcategory").append(result);
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("POST", strURL, true);
req.send(null);
}
}
}
It will help you if you are looking for Dependent dropdown (library) using CodeIgniter.
Look over It: Dependent dropdown (library)