Hide options in a select list using jQuery - php

How can I hide room numbers that is not at Hotel1 and send chosen room number to next page when you click on submit button. As it is now I will be forced to send only the id for chosen hotel because i need to have the value for selHotel options and selroom the same.
I'm using php, html and Sqlite database. hotelrows and roomrows contains all rows of hotel table and rooms table. Can anyone give me an idea of ​​how I could get this to work? Ie when choosing Hotel1 I can just choose room 101 or 102 in selroom for example. (Sorry if my English is bad, hopefully someone understands what I'm asking for.)
Hotels:
id.....Hotel
1......Hotel1
2......Hotel2
Rooms:
hid....Room#
1.......101
1.......102
2.......301
My php/html:
<select id="selHotel" name="selH">
<option value="default" selected> Select hotel</option>
<?php
foreach($hotelrows as $row)
{
echo "<option value='". $row['id'] . "'>". $row['hotelname'] . "</option>";
}
?>
</select>
<select id="selroom" name="selr">
<option value="default" selected>Select room </option>
<?php
foreach($roomrows as $row)
{
echo "<option value='". $row['hid'] . "'>". $row['roomnum'] . "</option>";
}
?>
</select>
jQuery to hide Hotel2 Rooms then i choose Hotel1:
$("#selHotel").change(function(){
var hotelVal = $(this).find("option:selected").val();
$('#selroom option').hide();
$('#selroom option[value='+hotelVal+']').show();
});

Use the roomnum as value, add your hid as class, then use it as filter when selected:
php:
echo "<option class='cls_". $row['hid'] . "' vlaue='".$row['roomnum']."'>". $row['roomnum'] . "</option>";
page
$("#selHotel").change(function(){
var hotelVal = $(this).find("option:selected").val();
$('#selroom option').hide();
$('#selroom .cls_'+hotelVal).show();
$('#selroom .cls_'+hotelVal).first().attr('selected',true);
});

Related

Dependent Drop down list with just php and mysql

I'm trying to create a dependent drop down list.in the code there are two select box one is department other being doctor.so what i want is if i select department 1 only the doctor in dept 1 with be in the option.what can i do?
what i wanted to do was using id ='iddept' from the department block and using it in the doctor's block.
<form class="" action="" method="post">
<select class="" name="deptname">
<?php
$deptsql=mysqli_query($con,"select deptname,deptid from department where status='1'");
while ($row = mysqli_fetch_assoc($deptsql))
{
echo "<option id='iddept' value='" . $row['deptid'] ."'>" . $row['deptname'] ."</option>";
}
?>
</select>
<br><br><br>
<label>select doctor</label>
<select class="" name="doc">
<?php
$dee=$_POST['iddept'];
echo $dee;
$docsql= mysqli_query($con,"SELECT * FROM doctor INNER JOIN department ON department.deptid=doctor.deptid WHERE department.deptid='$dee'");
while ($row = mysqli_fetch_assoc($docsql))
{ echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
?>
</select>
</fieldset>
</form>
is it possible to this with just php and mysql?
Of course you can, having it set up this way. By the way, you can use a fixed id value in a loop operation as the id value must be unique in one html document.
As I get it, when you select a department, you need to submit the form. If so, then you can control what gets shown in the doctors dropdown. You don't even necessarily need to use join between tables, since you already have all information you need in the doctors table. Your second query would look like:
$docsql= mysqli_query($con, "SELECT * FROM doctor WHERE deptid = '$dee'");

Based on a condition, how to make a dropdown list single select or multiselect?

I had a requirement where in an employee list dropdown given,
if selectMultiple is set, the dropdown should allow multiple selects, and if selectMultiple is not set, it shouldn't.
<select name="employeeList[]" id="employeeList" class="form-
control" multiple="<?=$selectMultiple?>">
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
</select>
It's a phtml file, and $selectMultiple is passed from a controller(as in Phalcon). I have tried something like passing, $selectMultiple ="multiple", so the code will look like
<select name="employeeList[]" id="employeeList" class="form-control" multiple="multiple">
and $selectMultiple="" for the single select case.
<select name="employeeList[]" id="employeeList" class="form-control" multiple="">
But the very presence of multiple attribute itself makes the dropdown list elligible for multiselect.
In short, in either cases, it triggers multiselect regardless of the condition. Please help.
You can simply write your code like
You need to pass $selectMultiple = "multiple" or $selectMultiple = ''
<select name="employeeList[]" id="employeeList" class="form-control" <? echo !empty($selectMultiple) ? $selectMultiple : '' ?>>
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
I tried introducing a new variable "chooseId" to get rid of the multiple attribute and hence worked!
Modified the select tag to,
<select name="employeeList[]" id="employeeList" class="form-control"
<?php if ($chooseId== 1) { ?> multiple="multiple" <?php } ?>>
<?php
foreach($employeeList as $employee) {
echo "<option value='" . $employee->$employeeId . "'>" .
$employee->employeeName . "</option>";
}
?>
</select>
Now the code recognises the multiple attribute and enables multiselect only when chooseId is 1, otherwise single select works normally.

Remeber Dropdown Selection Choice after Search Laravel

I have a few dropdown box filters , once you click submit it searches based on what you selected. When the results have loaded, the dropdown boxes reset back to how they were originally opposed to remembering what you previously selected.
This is what they look like.
<select id="buyer" name = "buyer" class="form-control" style="width: 100%" data-placeholder="">
<option value="" >Buyer</option>
<?php
foreach($buyer as $key){
echo '<option value=' . $key->LenderName . ">" . $key->LenderName . "</option>";
}
?>
</select>
How do get the dropdown box to remember the previous choice after search.
You need to say what option needs to be selected after submit, in your case during the loop if the value of "for" is equal with the "buyer" value posted, then add
selected
attribute in your :
<select id="buyer" name = "buyer" class="form-control" style="width: 100%" data-placeholder="">
<option value="" >Buyer</option>
<?php
foreach($buyer as $key){
echo '<option value=' . $key->LenderName;
if ($_POST['buyer']==$key->LenderName) {
echo " selected";
}
echo ">" . $key->LenderName . "</option>";
}
?>
</select>

Selection list populated from database

I have been having a problem trying to figure out how to create a selection list which is populated with data from a table in a database, namely with the options being customer's last names.
Here is what i have tried:
I am trying to make the selection list access the table "customer" and fields "customerID"
Any help would be greatly appreciated, if any more information is needed just ask.
You are messing in quotes. Try below.
$options .= "<option value='" . $id ."'>" . $name ."</option>";
And than use it like,
<select>
<option value=0>Choose</option>
<?php echo $options; ?>
</select>
"<OPTION VALUE=\"$id\">$name </option>";
This will prevent names, such as O'mally, from accidental truncation.
<?php
while ($row=mysqli_fetch_array($rs)) {
$id=$row["customerID"]
$name=$row["lastName"]
$options.="<OPTION VALUE=\"$id"\">".$name."</OPTION>;
}
?>
<p>Select a customer's ID to view information on</p>
<select>
<option value=0>Choose</option>
<?=$options?>
</select>
Change this:
$options.="<OPTION VALUE=\"$id"\">".$name;
to this:
$options.="<OPTION VALUE='" . $id. "'>" . $name . "</option>";
And if you have an error, tell us what error it is?

related dropdown list

I'm new in PHP.. I need your help..
I have 2 dropdownlist that related:
dropdown 1 : manually insert the value
dropdown 2 : attach value from database (value based on condition that selected in dropdown 1)
Then, both value which are selected will display in textbox at another form.
My problem is:
1) The value in 2nd dropdown can't be display.
2) The value in 1st dropdown can pass to other form but the 2nd can't.
Please kindly guide me.
I don't know how to share my code here.
form1.php
//1st dropdown
<select name="fruit_name" id="fruit_name" style="font-family: Calibri;font-size: 10pt;" onchange="loadXMLDoc(this.value); ">
<option value="0">-- please choose --</option>
<option value="Pineapple">Pineapple</option>
<option value="Apple">Apple</option>
//2nd dropdown
$fruit_name = $_POST['fruit_name'];
#Connect to MySQL
#Connect to database
$result = mysql_query("SELECT colour FROM fruit WHERE fruit_name = '$fruit_name'");
echo "<select name='colour' id='colour' style='font-family: Calibri;font-size: 10pt;'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = ''>" . $row['colour'] . "</option>";
}
echo "</select>";
mysql_free_result($result);
//Closes specified connection
?>
form2.php
<?php
//connection
$fruit_name = $_POST['fruit_name'];
$colour = $_POST['colour'];
?>
<label>
<input type="text" name="fruit_name" id="fruit_name" value = "<?php echo $fruit_name;?>" readonly>
</label>
<p>
<label>
<input type="text" name="colour" id="colour" value="<?php echo $colour;?>" readonly>
</label>
</p>
I usually don't do this but since I've some spare time on hand right now, I'm going to give the general approach that you can follow:
Include the following between your <head> tag.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
Below that, paste this code
<script type="text/javascript">
$(function(){
$('select#fruit_name').change(function(){
var selectedVal = $(this).val(); // get the selected value
$.ajax({ // send ajax request to the php file to process data
type:'post',
url:'php-page-name.php',
data:{'value':selectedVal},
success:function(ret) // display the result from php-page-name.php page
{
$('div#result').html(ret);
}
});
});
});
</script>
Lets move on to your HTML now
<select name="fruit_name" id="fruit_name" style="font-family: Calibri;font-size: 10pt;">
<option value="0">-- please choose --</option>
<option value="Pineapple">Pineapple</option>
<option value="Apple">Apple</option>
</select>
<div id="result">
<select>
<option>Select One</option>
</select>
</div>
php-page-name.php page (Do not forget to create this page and put it in the same folder as form1.php)
<?php
// put the code to connect to your database here
$fruit_name = $_POST['value']; // this will contain the value selected from first dropdown
$result = mysql_query("SELECT colour FROM fruit WHERE fruit_name = '$fruit_name'");
echo "<select name='colour' id='colour' style='font-family: Calibri;font-size: 10pt;'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row['colour']."'>" . $row['colour'] . "</option>";
}
echo "</select>";
mysql_free_result($result);
?>
PS : I'm using the mysql_* functions in this example since I'm assuming you're too. But this is not recommended as they are going to be deprecated soon. You might want to switch to mysqli or PDO

Categories