I have this interesting setup that I hope someone can help me fix. I've searched and found some solutions but nothing that comes close to what I'm trying to achieve, so nothing really lead me in a direction that could get me to a solution for what I'm doing
I have a dropdown of litters. You select a litter, onchange happens, it gets the available pick spots from the database, then echos out the results with another foreach to another dropdown menu
The problem is, these dropdown menus are inside of a foreach loop (for each client available to the site user). So the first client works as expected, but because it has the same ID, the second client's dropdowns don't do anything since it's still on the same page
Hopefully someone has some insight on how to make this work :(
Here's the dropdown:
<select class="form-control mb-3" name="the_chosen_litter" id="litter" required>
<option disabled selected>First, Choose A Litter...</option>
<?php
foreach($labradoodle_two as $litter_waitlist_form){
?>
<option id="<?php echo $litter_waitlist_form['litter_id']; ?>" value="<?php echo $litter_waitlist_form['litter_id']; ?>" style="<?php if($litter_waitlist_form['status']=='LITTER CLOSED'){ echo 'color:red;'; }else{ echo 'color:green;'; } ?>" >
<?php echo $litter_waitlist_form['litter_names']; ?>
</option>
<?php } ?>
</select>
<select class="form-control mb-3" name="buyer_pick_gender" id="pick_list" required> <!-- uses jquery to fill in this field -->
<option disabled selected>Litter Selection Required...</option>
</select>
Here's the javascript/ajax:
$(document).ready(function() {
$("#litter").on("change", function() {
var litterID = document.getElementById("litter").value;
$.post('secure/litter_list_dropdown.php', { littersubmit: litterID }, function(result) {
$('#pick_list').html(result);
}
);
});
});
And for reference, here's my "litter_list_dropdown" code that pulls from the database and echos out the available "pick spots":
<?php
session_start();
include '../../database/dbh.class.php';
$litter_id = $_REQUEST['littersubmit'];
$femalespickedquery = $dbpdo->query("SELECT * FROM pick_assignment WHERE litter_id=? AND gender_id=?",$litter_id,"1")->fetchAll();
$malespickedquery = $dbpdo->query("SELECT * FROM pick_assignment WHERE litter_id=? AND gender_id=?",$litter_id,"2")->fetchAll();
?>
<option disabled="disabled" selected="selected">Choose A Pick</option>
<?php foreach ($femalespickedquery as $pick){
$picktaken=$pick['user_id'];
$pickcustom=$pick['custom_name'];
$pickposition=$pick['pick_id'];
$suffix=ordinal($pickposition);
?>
<option value="<?php echo $pickposition.' 1'; ?>" <?php if($picktaken!="0" OR $pickcustom==true){ echo "disabled"; } ?>>
<?php echo "FEMALE - " .$pickposition.$suffix. " Pick"; if($picktaken!="0" OR $pickcustom==true){ echo " *RESERVED*"; }?>
</option>
<?php } ?>
<?php
foreach ($malespickedquery as $pick){
$picktaken=$pick['user_id'];
$pickcustom=$pick['custom_name'];
$pickposition=$pick['pick_id'];
$suffix=ordinal($pickposition);
?>
<option value=<?php echo $pickposition.' 2'; ?>" <?php if($picktaken!="0" OR $pickcustom==true){ echo "disabled"; } ?>>
<?php echo "MALE - " .$pickposition.$suffix. " Pick"; if($picktaken!="0" OR $pickcustom==true){ echo " *RESERVED*"; }?>
</option>
<?php } ?>
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.
Good morning, I have a problem that is: I can not keep several options selected after submitting the form and I would like someone to help me.
<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
<?php while ($reg_sql=mysqli_fetch_array($res_sql)){?>
<option value="<?php echo $reg_sql['ID_USER']; ?>"><?php echo $reg_sql['NOMEUSER']; ?></option>
<?php } ?>
</select>
<script type="text/javascript">
document.getElementById('utilizadores').value = "<?php echo $_POST['utilizadores[]'];?>";
</script>
this is my code to have the various options in the select box
You have to check if $_POST['utilizadores'] or $_GET['utilizadores'] it depends on your request type. I will use $_POST in here for explain my answer.
your select is multiple, you can use in_array function for checking that if result from db record is in array of $_POST['utilizadores']
<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
<?php while ($reg_sql=mysqli_fetch_array($res_sql)){?>
**<option value="<?php echo $reg_sql['ID_USER']; ?>"
<?php
if(isset($_POST['utilizadores'])){
if(in_array($reg_sql['ID_USER'], $_POST['utilizadores'])){
echo 'selected';
}else{
echo '';
}
}
?>
>**<?php echo
$reg_sql['NOMEUSER']; ?></option>
<?php } ?>
</select>
You may be able to do it more efficiently if your database result also contains which rows are selected, but when you loop through, just add the selected="selected" attribute to the <option> tag.
Assuming your $_POST array exists in this scope, you can use the in_array function in PHP to determine if the option has been selected (docs).
The ternary based operation is as follows:
in_array($reg_sql['ID_USER'],$_POST['utilizadores']) ? 'selected="selected"' : ''
Which says "if the ID_USER is in the post array, then print the selected attribute, otherwise, print a blank string"
Putting it all together:
<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
<?php while ($reg_sql=mysqli_fetch_array($res_sql)){?>
<option value="<?php echo $reg_sql['ID_USER']; ?>" <?= in_array($reg_sql['ID_USER'],$_POST['utilizadores']) ? 'selected="selected"' : '' $?>>
<?php echo $reg_sql['NOMEUSER']; ?>
</option>
<?php } ?>
</select>
An example of how you can do this. Either add the "selected" string if yes or leave blank if no. You can also write selected="selected". You can do the same thing to set disabled or readonly.
<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
<?php while ($reg_sql=mysqli_fetch_array($res_sql)){?>
<?php $selected = isset($reg_sql["mi_variable"]) ? "selected" : ""; ?>
<option value="<?php echo $reg_sql['ID_USER'];?>" <?php echo $selected; ?> >
<?php echo $reg_sql['NOMEUSER']; ?>
</option>
<?php } ?>
</select>
<script type="text/javascript">
document.getElementById('utilizadores').value = "<?php echo $_POST['utilizadores[]'];?>";
</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 magento 2.1.0. I created two dropdown on customer registration form ,where the data is fetching from database.
There are two databases :
city database which contain city_id and city column.
send is area table which contain area_id, city_id and area column.
on frontend data is sucessfully fetch, and even onchange is also working on city.
City dropdown:
<select name="city" onchange="getArea()" id="city">
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('/city')->serviceCity();
foreach ($model as $d)
{ ?>
<option id="<?php echo $d['city_id']; ?>" value="<?php echo $d['city_id']; ?>"><?php echo $d['city']; ?></option>
<?php } ?>
</select>
Area dropdown:
<div class="field area required">
<label for="area" class="label"><span><?php /* #escapeNotVerified */
echo __('Area') ?></span></label>
<select name="area" id="area">
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\Area')->serviceArea();
foreach ($model as $a)
{ ?>
<option value="<?php echo $a['area']; ?>"><?php echo $a['area']; ?></option>
<?php } ?>
</select>
</div>
Onchange script:
<script>
function getArea(){
alert(document.getElementById('city').value);
}
</script>
Now my question is, what should I do, when I change the city, in second dropdown related area will display only?
Thanks in advance
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 } ?>