Combobox based on another combobox - javascript

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 } ?>

Related

Insert data that doesnt exist from selectbox

I would want a select box that can insert data if the data typed doesn't exist in the DB, could anyone help me, please?
<div class="form-group">
<label for="">Catégorie</label>
<select class="form-control select2" name="category" required>
<?php
$select = $pdo->prepare("SELECT * FROM tbl_category");
$select->execute();
while($row = $select->fetch(PDO::FETCH_ASSOC)){
extract($row)
?>
<option><?php echo $row['cat_name']; ?></option>
<?php
}
?>
</select>
</div>
Use select2 plugging. It's facilitated to search and add new value which is not in dropdowns.
Click here Select2
<div class="form-group">
<label for="">Catégorie</label>
<select class="form-control select2" name="category" required id="category">
<?php
$select = $pdo->prepare("SELECT * FROM tbl_category");
$select->execute();
while($row = $select->fetch(PDO::FETCH_ASSOC)){
extract($row)
?>
<option><?php echo $row['cat_name']; ?></option>
<?php
}
?>
</select>
</div>
$("#category").select2({
tags: true
});

$_GET to accept values with space

I checked all the possible solutions here, unfortunately it won't work. What seems to be the problem with my code?
I tried the following.
urldecode (http://php.net/manual/en/function.urldecode.php)
str_replace http://php.net/manual/en/function.str-replace.php)
But no luck.
I tried the following in isset
$accounttitle = $_GET['accounttitle'];
urlencode($_GET["accounttitle"])
$accounttitle = str_replace(" ", "", $accounttitle );
Here's my isset
if(isset($_GET['accounttitle'])){
$accounttitle = $_GET['accounttitle'];
} ?>
Here's my form
<div class="box-header with-border">
<!--<i class="fa fa-plus"></i> New -->
<div class="form-group">
<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" style="text-transform:uppercase" required>
<option value="">PLEASE SELECT OPTION</option>
<?php
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
echo "<option value=".$row['accounttitle'].">".$row['accounttitle']."</option>";
}
?>
</select>
</div>
</div>
</form>
Here's my script
$(function(){
$('#select_account_title').change(function(){
window.location.href = 'earnings_amendment.php?accounttitle='+$(this).val();
});
});
</script>
I can echo() but it won't work on values with space.
Your problem is not receiving $_GET but rather with your outgoing link.
<select class="form-control" id="select_account_title" style="text-transform:uppercase" required>
<option value="">PLEASE SELECT OPTION</option>
<?php while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)): ?>
<option value="<?= urlencode($row['accounttitle'])?>"><?= $row['accounttitle'] ?></option>
<?php endwhile; ?>
</select>
I prefer trim 'trim()' php function not string replace.

How to hide select options when there is no data

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.

How to select a selected value from database in PHP

Im trying to make a select that will help me in my "edit user" page.
I have this code but I don´t know how to change it so I can load the value selected from the database.
This code it´s working...
<div class="form-group">
<th><label for="text">Tipo Sanguineo:</label></th>
<td><select class="form-control" id="sangue" name="tipo_sangue">
<option value="O-" <?php if ($cl_tipo_sangue == "O-") echo 'selected';?> >O-</option>
<option value="O+" <?php if ($cl_tipo_sangue == "O+") echo 'selected';?> >O+</option>
<option value="A-" <?php if ($cl_tipo_sangue == "A-") echo 'selected';?> >A-</option>
<option value="A+" <?php if ($cl_tipo_sangue == "A+") echo 'selected';?> >A+</option>
<option value="B-" <?php if ($cl_tipo_sangue == "B-") echo 'selected';?> >B-</option>
<option value="B+" <?php if ($cl_tipo_sangue == "B+") echo 'selected';?> >B+</option>
<option value="AB-" <?php if ($cl_tipo_sangue == "AB-") echo 'selected';?> >AB-</option>
<option value="AB+" <?php if ($cl_tipo_sangue == "AB+") echo 'selected';?> >AB+</option>
</select></td>
</div>
I need to do this with this code:
<?php $dts = DBRead11(); ?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>"><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
The code that I need to change is loading the values from the database to the site, and Im not sure how to see the selected one if I load them from the database.
Try this,
<?php $dts = DBRead11(); ?>
<?php $cl_tipo_sangue = "B+"; ?>// here you need to add db value which you saved in DB
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>" <?php if ($cl_tipo_sangue == $option['nome']) { echo 'selected="selected"'; } ?> ><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
Steps:
1) Take an array to save drop down options' id name pairs.
2) Loop over it in the <select> tag for <option>s.
3) Check if selected id is the current iteration id.
4) If yes, add attribute selected="selected" to current <option>
And check in the loop if selected one.
<?php
$bloodGroups = [
'O-' => 'O-',
'O+' => 'O+',
'A-' => 'A-',
'A+' => 'A+',
'B-' => 'B-',
'B+' => 'B+',
'AB-' => 'AB-',
'AB+' => 'AB+'
];
?>
<div class="form-group">
<th><label for="text">Tipo Sanguineo:</label></th>
<td><select class="form-control" id="sangue" name="tipo_sangue">
<?php
if (! empty($bloodGroups)) {
foreach ($bloodGroups as $bgid => $bloodGroup) {
$selected = ($cl_tipo_sangue == $bgid) ? 'selected="selected"' : '';
?>
<option value="<?php echo $bgid;?>" <?php echo $selected;?>><?php echo $bloodGroup;?></option>
<?php
}
}
?>
</select></td>
</div>
You need to fetch the value you saving in database then
<?php
$nome = $database_value;
?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>" <?php echo ($option['nome'] == $nome)?'selected':'' ?>><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
Using javascript
<?php $dts = DBRead11(); ?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>"><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
<script>
document.querySelector('#diretor_turma').value = '<?= $cl_tipo_sangue ?>'
</script>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
$select = (isset($cl_tipo_sangue) && $cl_tipo_sangue == $option['nome'])?"selected = 'selected'":"";?>
<option value="<?php echo $option['nome']; ?>" <?php echo $select; ?>><?php echo $option['nome']; ?></option>
<?php } ?>
</select>

Retrieving data from databse using the dropdown selected list in php and mysql

I have texbox and dropdown list which is populated from mysql database. I want to retrieve data from database and wants to display in textbox using dropdown selected list, without refreshing the page. Here is my code and Thanks in Advance.
<select name="select1" class="form-control" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php
require("dbcon.php");
$getallcompanies = mysql_query("SELECT * FROM ifcandetails6");
while($viewallcompanies = mysql_fetch_array($getallcompanies)){
?>
<option id="<?php echo $viewallcompanies['tcuid']; ?>"><?php echo $viewallcompanies['tcname'] ?></option>
<?php
}
?>
</select>
Input Textbox:
<input type="text" id="field1" value="<?php echo $viewallcompanies['tccontact']?>" disabled/>
Separate things out like this:
<?php
require("dbcon.php");
$query = mysql_query("SELECT tcuid, tcname, tccontact FROM ifcandetails6");
while($row = mysql_fetch_array($query)){
$contacts[] = $row['tccontact'];
$companies[] = $row;
}
?>
<select name="select1" class="form-control" id="dropdownlist1">
<option id="0">-- Select the Company --</option>
<?php foreach($companies as $company) { ?>
<option id="<?= $company['tcuid']; ?>"><?= $company['tcname'] ?></option>
<?php } ?>
</select>
<?php foreach($contacts as $contact) { ?>
<input type="text" id="field1" value="<?= $contact['tccontact']?>"/>
<?php } ?>

Categories

Resources