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>
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 10 dropdown menus that are like:
Please Select
Item 1
Item 2
Item 3
---Appetizers---
Item 4
Item 5
---Main Courses---
Item 6
Item 7
---Lunch Specials---
Item 8
I want to grab the value if an Item is selected and print it on a confirmation page. Can I do that with a for loop and javascript? Like if I do this, how would I call it in HTML?
function getItems() {
var items = [
document.getElementById("item1").value,
document.getElementById("item2").value,
document.getElementById("item3").value,
document.getElementById("item4").value,
document.getElementById("item5").value,
document.getElementById("item6").value,
document.getElementById("item7").value,
document.getElementById("item8").value,
document.getElementById("item9").value,
document.getElementById("item10").value
];
for (int i = 0; i < items.length; i++) {
var count = 0;
if (
(item[i] != "Please Select") ||
(item[i] != "---Appetizers---") ||
(item[i] != "---Main Courses---") ||
(item[i] != "---Lunch Specials---")
) {
document.getElementById(i).innerHTML = item[i];
count++;
}
}
}
The dropdowns are populated from a MySQL database using php. My HTML/php:
Item 4
<select id="item4">
<option value"">Please Select</option>
<?php if ($resultApp4->num_rows > 0) { ?>
<option value"">---Appetizers---</option>
<?php while($row = $resultApp4->fetch_assoc()) { ?>
<option value="<?php echo $row['price']; ?>"><?php echo $row['name']; ?> - $<?php echo $row['price']?></option>
<?php } } ?>
<?php if ($resultMain4->num_rows > 0) { ?>
<option value"">---Main Courses---</option>
<?php while($row = $resultMain4->fetch_assoc()) { ?>
<option value="<?php echo $row['price']; ?>"><?php echo $row['name']; ?> - $<?php echo $row['price']?></option>
<br>
<?php } } ?>
<?php if ($result4->num_rows > 0) { ?>
<option value"">---Lunch Specials---</option>
<?php while($row = $result4->fetch_assoc()) { ?>
<option value="<?php echo $row['price']; ?>"><?php echo $row['name']; ?> - $<?php echo $row['price']?></option>
<br>
<?php } } ?>
</select>
<br>
You seem to generate a single drop down with all options. If that is intended, then make it one in which multiple selections can be made, which is not the default behaviour. For that to happen, add the multiple attribute to the select.
Then you need to add a listener to the change event, so that your code executes whenever the user selects or unselects item(s).
Note that you have forgotten some = after the value attributes. Also, it is not allowed to put a <br> tag after an option element. A select element should only have option children elements.
Here is how the corrected PHP could look:
Item 4
<select id="item4" multiple size="8">
<option value="">Please Select</option>
<?php if ($resultApp4->num_rows > 0) { ?>
<option value="">---Appetizers---</option>
<?php while($row = $resultApp4->fetch_assoc()) { ?>
<option value="<?php echo $row['price']; ?>">
<?php echo $row['name']; ?> - $<?php echo $row['price']?></option>
<?php } } ?>
<?php if ($resultMain4->num_rows > 0) { ?>
<option value="">---Main Courses---</option>
<?php while($row = $resultMain4->fetch_assoc()) { ?>
<option value="<?php echo $row['price']; ?>">
<?php echo $row['name']; ?> - $<?php echo $row['price']?></option>
<?php } } ?>
<?php if ($result4->num_rows > 0) { ?>
<option value="">---Lunch Specials---</option>
<?php while($row = $result4->fetch_assoc()) { ?>
<option value="<?php echo $row['price']; ?>">
<?php echo $row['name']; ?> - $<?php echo $row['price']?></option>
<?php } } ?>
</select>
The JavaScript could be much simpler, as in this working snippet:
// Listen to the selection changes made:
document.querySelector('#item4').addEventListener('change', function () {
// For this demo, the text is stored in a DIV
document.getElementById('output').textContent =
[...this.selectedOptions] // convert selected options list to array
.filter ( option => option.value.length ) // option must have a value
.map( option => option.textContent ) // get text of option
.join('\n'); // add line breaks
});
Item 4 (hold Ctrl key to add selections)<br>
<select id="item4" multiple size=8>
<option value="">Please Select</option>
<option value="">---Appetizers---</option>
<option value="1">drink 1 - $1</option>
<option value="1.10">drink 2 - $1.10</option>
<option value="">---Main Courses---</option>
<option value="15">dish 1 - $15</option>
<option value="16">dish 2 - $16</option>
<option value="">---Lunch Secials---</option>
<option value="10">lunch 1 - $10</option>
<option value="11">lunch 2 - $11</option>
</select>
<div id="output" style="white-space:pre"></div>
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 } ?>
I am selecting data from a database using PHP with some javascript inside the loop:
<script type="text/javascript">
$('#voip_type<?php echo $i; ?>').on('change',function(){
var val = $(this).val();
if( val ==="no"){
$("#producttype<?php echo $i; ?>").show()
}
else if( val ==="extension")
{
$("#producttype<?php echo $i; ?>").val("VoIP Telephony").show()
}
else {
$("#producttype<?php echo $i; ?>").val("VoIP Telephony").show()
}
});
</script>
each time it loops round, it adds +1 onto $i
this does the same in the textfield IDs so the code in the JS and the text field IDs match names but the values aren't changing on the textfields and select elements
here is one example of a select element:
<select name="voip_type<?php echo $i; ?>" id="voip_type<?php echo $i; ?>" style="width:120px;">
<option value="">VoIP Item?</option>
<optgroup label="No">
<option value="no" <?php if($result["voip_type"] != 'extension' or $result["voip_type"] != 'queue' or $result["voip_type"] != 'ivr' or $result["voip_type"] != 'storage') { echo 'selected="selected"'; } ?>>Continue</option>
</optgroup>
<optgroup label="Yes">
<option value="extension" <?php if($result["voip_type"] == 'extension') { echo 'selected="selected"'; } ?>>Extension</option>
<option value="queue" <?php if($result["voip_type"] == 'queue') { echo 'selected="selected"'; } ?>>Queue</option>
<option value="ivr" <?php if($result["voip_type"] == 'ivr') { echo 'selected="selected"'; } ?>>IVR</option>
<option value="storage" <?php if($result["voip_type"] == 'storage') { echo 'selected="selected"'; } ?>>Storage</option>
</select>
UPDATE - Full code:
<?php
$sql="SELECT * FROM customer_billing where customer_seq = '".$_GET["seq"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$i=0;
while($result=mysql_fetch_array($rs)) {
$i++; ?>
<script type="text/javascript">
$('#voip_type<?php echo $i; ?>').on('change',function(){
var val = $(this).val();
if( val ==="no"){
$("#producttype<?php echo $i; ?>").show()
}
else if( val ==="extension")
{
$("#producttype<?php echo $i; ?>").val("VoIP Telephony").show()
}
else {
$("#producttype<?php echo $i; ?>").val("VoIP Telephony").show()
}
});
</script>
<input type="hidden" name="sequence<?php echo $i; ?>" size="30" value="<?php echo $result["sequence"]; ?>" />
<select name="voip_type<?php echo $i; ?>" id="voip_type<?php echo $i; ?>" style="width:120px;">
<option value="">VoIP Item?</option>
<optgroup label="No">
<option value="no" <?php if($result["voip_type"] != 'extension' or $result["voip_type"] != 'queue' or $result["voip_type"] != 'ivr' or $result["voip_type"] != 'storage') { echo 'selected="selected"'; } ?>>Continue</option>
</optgroup>
<optgroup label="Yes">
<option value="extension" <?php if($result["voip_type"] == 'extension') { echo 'selected="selected"'; } ?>>Extension</option>
<option value="queue" <?php if($result["voip_type"] == 'queue') { echo 'selected="selected"'; } ?>>Queue</option>
<option value="ivr" <?php if($result["voip_type"] == 'ivr') { echo 'selected="selected"'; } ?>>IVR</option>
<option value="storage" <?php if($result["voip_type"] == 'storage') { echo 'selected="selected"'; } ?>>Storage</option>
</select><br />
<select name="producttype<?php echo $i; ?>" id="producttype<?php echo $i; ?>" style="width:120px;">
<option value="">none</option>
<option value="Broadband">Broadband</option><option value="Hosted Exchange">Hosted Exchange</option><option value="Offsite Backup">Offsite Backup</option><option value="PC Maintenance">PC Maintenance</option><option value="Phone Lines (PSTN/ISDN)" selected="selected" >Phone Lines (PSTN/ISDN)</option><option value="Software Development">Software Development</option><option value="VoIP Telephony">VoIP Telephony</option><option value="Web Hosting">Web Hosting</option> </select>
<input type="text" name="productname<?php echo $i; ?>" size="30" value="<?php echo $result["productname"]; ?>" />
<?php
}
?>
Your code is super hard to debug. My suggestion is that you turn your code into 2 separated piece of code one for PHP and one for Javascript. You can mix 2 languages but it doesn't mean that you should mix them. Instead do this
<script type="text/javascript">
var result = <?= json_encode($the_final_query_result) ?>;
</script>
The idea is to turn all your PHP variables that will be used in JS into JS variables. Then in you JS code you can just use normal JS functions to loop through the variables instead of mixing PHP loops and JS code (and HTML code) which makes your code "impossibru" to debug
Another thing is that mysql_fetch_array is deprecated as of PHP 5.5 (http://fi1.php.net/mysql_fetch_array), you should consider switching to something else
if you want to php code inside javascript or jquery then you should do like this..
funciton() {;}