Pass "selected" <option> as page loads - javascript

I've got a problem. I'm trying to show restaurant's revenue graph of selected year and as a default. I want it to be showing this year's graph, in this case 2020. I used selected on this year's date, but the problem is that I have onchange. It only shows graph if I change something, reselect. Is there a good way to use first option as default?
<select id="yearPick" name="yearPick" onchange="load_new_content()">
<option value="year" disabled>year</option>
<option value="<?php echo date("Y"); ?>" selected><?php echo date("Y"); ?></option>
<?php for ($i = date("Y") - 1; $i >= 1980; $i--) { ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>

Related

Cant get the correct value from select [duplicate]

I have been trying to select the table number according to its location. My form is working and the record is added to the database however at the number section, the number that is inserting is "32" no matter what I choose, Even if I choose in the select "tnum3" a different number. I don't know what has happened.
The form
<label for="tnum">table number
<select type="number" id="tnum1" name="num">
<?php for ($i = 2; $i <= 9; $i++) : ?>
<option id="in" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?></select>
<select type="number" id="tnum2" name="num" style="display: none;">
<?php for ($i = 12; $i <= 23; $i++) : ?>
<option id="out" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?></select>
<select type="number" id="tnum3" name="num" style="display: none;">
<?php for ($i = 32; $i <= 35; $i++) : ?>
<option id="tr" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select></label><br>
<label for="loc">location
<select id="loc" name="location" style="width:100%;" onchange="change_tnum(this.value)" required>
<option value="inside" >inside</option>
<option value="outside" >outside</option>
<option value="Porch" >Porch</option>
</select></label><br>
This is the JS function that just toggles the select tags according to the location I choose:
function change_tnum(x){
$('#tnum1').hide();
$('#tnum2').hide();
$('#tnum3').hide();// hide any options already shown
switch (x) { // show whichever option is appropriate
case 'inside':
$('#tnum1').show();
inside();
break;
case 'outside':
$('#tnum2').show();
outside();
break;
case 'porch':
$('#tnum3').show();
terrace();
break;
default:
break;
}
}
I don't see any issue with your select, however even if you set the element to display: none, it's value will still be submitted. Since there are multiple elements of the same name the last value overwrites the previous values.
If you want only one element value then you should add disabled attribute to it. The elements marked disabled are not sent to the server. Reference
So, your code changes to -
function change_tnum(x){
$('#tnum1').hide();
$('#tnum1').attr('disabled', true);
$('#tnum2').hide();
$('#tnum2').attr('disabled', true);
$('#tnum3').hide();// hide any options already shown
$('#tnum3').attr('disabled', true);
switch (x) { // show whichever option is appropriate
case 'inside':
$('#tnum1').show();
$('#tnum1').attr('disabled', false); // or simply remove disabled attribute
inside();
break;
case 'outside':
$('#tnum2').show();
$('#tnum2').attr('disabled', false);
outside();
break;
case 'porch':
$('#tnum3').show();
$('#tnum3').attr('disabled', false);
terrace();
break;
default:
// do something
}
}
Hope it helps you.

How to give id to auto generated element automatically

I want to make a dropdownlist that contains something from my database, all going well until I need to give each of the option unique id
<div class="div3" >
<select id="input2" class="drop" name="a" required>
<option value="" selected disabled>a</option>
<?php while ($row1 = mysqli_fetch_array($result)) :; ?>
<option class="option"><?php echo $row1[0]; ?></option>
<?php endwhile; ?>
</select>
</div>
Put an id unique value in option tag, and order you code for make an array with html tag, for example if $row1[0] = uniqueid and $row1[1] = name
<?php while ($row1 = mysqli_fetch_array($result)){?>
<option id="<?php echo $row1[0]; ?>" class="option"><?php echo $row1[1]; ?></option>
<?php }?>
Best Regards

Content changeable Dropdownlist from database data

Am loading my user records from database to the table, Am using:
foreach ($userArray as $key => $value) {
I have isAdmin row which contains 'Y', 'N' data.I need to make dropdownlist from this row.If data in Db is 'Y' other dropdownlist option should be 'N' and v/v.
Am using <select id="<?php echo $userArray[$key]["isAdmin"]?>">
I tried something like that, but if my data is Y it gives two more options , Y and N, it should be N, how to repair that?Here is my code:
<select id="<?php echo $userArray[$key]["isAdmin"]?>">
<option value="<?php echo $userArray[$key]["isAdmin"]?>">
<?php echo $userArray[$key]["isAdmin"];
if (strcmp($userArray[$key]["isAdmin"],"Y")==0){
?></option>
<option value="N">N</option>
<option value="Y" style="display:none;">Y</option>
<?php
}
else
?>
<option value="Y">Y</option>
<option value="N" style="display:none;">N</option>
</select>
I found a solution, here is my code:
<option value="<?php echo $userArray[$key]["isAdmin"]?>">
<?php echo $userArray[$key]["isAdmin"];
if (strcmp($userArray[$key]["isAdmin"],"Y")==0){
?></option>
<option value="N">N</option>
<?php
}
if (strcmp($userArray[$key]["isAdmin"],"N")==0){
?>
<option value="Y">Y</option>
<?php
}
?>
</select>
Create a if condition in your nav bar checking isAdmin and then echo the other dropdownlist.
if($isadmin == "Y"){
echo '?>
//Put Html code here for admin
<?php ';
}else{
echo'?>
//Put Html code here for non admin
<?php';
}

How to assign value returned by a function into a variable?

I need to determine the sceen width using media query then properly display the results retreived from database. For now results are shown using a function this way. This function actually to display dropdown list for a particular menu.
<?php Core::getHook('block-branches'); ?>
So tried something as below to display content according to screen width:
<?php
if (isset($_GET['width'])) {
$width = $_GET['width'];
if ($width <= 480) { //mobile devices
$list = Core::getHook('home-menu-mobile'); **//assigning function value into a variable**
} elseif ($width <= 720){ //tablets
$list = Core::getHook('home-menu-mobile'); **//assigning function value into a variable**
} else { //desktops
$list = Core::getHook('block-branches'); **//assigning function value into a variable**
}
}
?>
HTML
<select id="myselect">
<option>Go To ...</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/">Home</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/page/about-us">About Us ▾</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/page/the-centre-point-of-any-web-projects">Centre-Point of Web Projects</option>
<option>Branches ▾
<?php echo $list;?>**(display according to screen width)**
</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/news">News</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/main/event">Events</option>
<option value="<?php echo $data['config']['SITE_DIR']; ?>/contact">Contact Us</option>
</select>
<script>
//$('#myselect').on('change', function() {
//location.href=$(this).data('url');
//});
document.getElementById("myselect").onchange = function(d){
window.location = this.value;
};
</script>
I think the function getHook('block-branches') is returning an array not a value so you must loop the $list and display the value like this
<option>Branches ▾
<?php foreach($list as $value){
echo $value;
}?>**(display according to screen width)**
</option>

How to query table based on option value selected?

How do I hide 'role' selection option until 'project' is selected and then query role where project selected = $project?
I only want to select a role corresponding to its project. The table selectproject contains a row with $id_project, $project and $role. I would like to select the role based on first selecting a project. Ex: "select $role where selected = $project".
How do I use Java script or Ajax to accomplish this? Please provide code for I am still learning Java/Ajax.
<?php
$result4= mysqli_query($con, $idq);
echo '<select id="project" name="project" class="textBox">';
echo '<option value="">-Select-</option>';
while ($row = $result4->fetch_assoc()){
?>
<option value="<?php echo $row['project']; ?>"><?php echo $row['project']; ?></option>
<?php
}
echo "</select>";
?>
<?php
$result5= mysqli_query($con, $idq2);
echo '<select id="role" name="role" class="textBox2">';
echo '<option value="">-Select-</option>';
while ($row = $result5->fetch_assoc()){
?>
<option value="<?php echo $row['role']; ?>"><?php echo $row['role']; ?></option>
<?php
}
echo "</select>";
?>

Categories