Dependent Drop down list with just php and mysql - php

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'");

Related

How to make the Html dropdown list selected value to be printed as a CSV?

I have this html code and I want when I select name from table category with export.php to export all the data only for this category name.Can you help me please?
<select>
<option disabled selected></option>
<?php
include "config.php"; //
$products_cat = mysqli_query($con, "SELECT name From category");
while ($product_categoty = mysqli_fetch_array($products_cat)) {
echo "<option value='" . $product_categoty['name'] . "'>" . $product_categoty['name'] . "</option>";
}
?>
</select>
<form method="post" action="export.php">
<input type="submit" name="expo" value="Export Category">
</form>
</form>```
I think Select should be inside the form and please follow these steps to export data as csv.
https://www.codexworld.com/export-data-to-csv-file-using-php-mysql/

Filtering my postgreSQL table using drop down box

The Problem
I am having trouble filtering my table, no errors are coming up so I am unsure why it isn't working. I am wondering whether it is to do with the placement of SQL code or whether the submit button isn't coded correctly... Or even if the ive programmed it all wrong haha.
Expected Outcome
What I expect to see when press submit is for the table to change to what ever it was that submitted. For exmple, in the drop down box there is an option for 'Canned' food, if I was to press this I would expect to see the table change, showing only canned food.
What the actual outcome is
Nothing seems to be happening, and with no errors, i'm completely unsure as to why.
Here is the code for the drop down box
I am unsure whether this is the correct form for what I am trying to do. I want to stay on the same page, I only want to affect the table.
<form action="database.php">
<select name="category" id="category">
<option value="Alcoholic">Alcohol</option>
<option value="Canned">Canned Food</option>
<option value="Dairy">Dairy</option>
<option value="Dessert">Dessert</option>
<option value="Frozen">Frozen Food</option>
<option value="Fruit">Fruit</option>
<option value="Junk Food">Junk Food</option>
</select>
<input type="submit" name="submit" value="Search"/>
</form>
Here is the code for the table and the SQL commands
<?php
$conn = pg_connect("host=db.dcs.aber.ac.uk port=5432
dbname=teaching user=csguest password=********");
// Empty var that will be populated if the form is submitted
$where = '';
if (isset($_POST['submit'])) {
if (!empty($_POST['category'])) {
// Where conditional that will be used in the SQL query
$where = " WHERE Category = '".pg_escape_string($_POST['category'])."'";
}
}
$res = pg_query($conn, "SELECT Foodtype, Manufacturer, Description, Price
FROM food " . $where . " ORDER BY Category ASC");
echo "<table id=\"myTable\" border='1'>";
while ($a = pg_fetch_row($res)) {
echo "<tr>";
for ($j = 0; $j < pg_num_fields($res); $j++) {
echo "<td>" . $a[$j] . "</td>";
}
echo "<td><form id='cart' name='cart' method='POST' action='addToBasket.php'>
<input type='submit' name='Select' id='Select' value='Add To Basket'>
</form></td>";
echo "</tr>\n";
}
echo "</table>\n";
$Alcoholic = pg_query("SELECT Foodtype, Manufacturer,
Description, Price FROM food WHERE Category = 'Alcoholic'");
$Canned = pg_query("SELECT Foodtype, Manufacturer,
Description, Price FROM food WHERE Category = 'Canned'");
?>
The last two SQL statements above are supposed to filter the table, there will one for each of the drop down options
Since the discussion on the comments lead to the solution, I'm adding it here:
Your problem is that you defined a form without a method which means that it will be by default GET and in your code you are trying to get POST variables so change your form to:
<form action="database.php" method="post">

Get data into a jq easy ui combobox using php

< input id="cc" class="easyui-combobox" name="dept"
data-options="valueField:'id',textField:'text',url:'get_data.php'">
This is a code used in creating a jquery easyui combobox. Can anyone help me with how to write the php code in get_data.php. That means for example lets say there is a table called DEPT with two colums dep_id and dep_name.I want to diplay dep_name in the combobox and valueField to be dep_id.
I use this all the time, and there is no call to a seperate php file to get the results:
<label for="cc">Name of DDB here</label>
<select id="cc" name="cc" style="width: 300px">
<option value="">--Select ?--</option>
<?php
$query = "SELECT dep_id, dep_name FROM YOUR TABLE HERE ORDER BY dep_name";
$result = $conn->query($query);
while ($row = $result->fetch_assoc()) {
echo '<option value="' . $row['dep_id'] . '" >' . $row['dep_name'] . '</option>';
}
?>
</select>
and all of this goes right where you want your drop down box.

How do I insert the results from multiple dropdown menus, which have multiple answers from a form into MySQL?

I am creating a massive, relational database in MySQL and using PHP to process a form. The MySQL table has the following fields:
Server_Name
Location_id
Location_Contact
app_contact_O
app_contact_Contact
OS_id
Database_id
Vendor_id
last_update
updated_by
For every Server Name, there can be multiple Location Contacts, app_contact_O's, app_contact_Contacts, and Database_id's.
The multiple fields are inputted through a dropdown menu where you can select the multiple choices.
How do I get the multiple fields into the database. I've tried a number of different solutions and have not found anything that works.
I can get the results into MySQL from a single drop-down menu using a foreach() loop, but I need help from the multiples.
My code for this page is like 438 lines long...I don't think it would be useful to the community to post.
==========================
Okay let me clarify using a shortened version of the form:
I have the following form:
<form action="" method="post">
Server Name: <input type="text" name="Server_Name" size="40" /><br />
Location: <select name="servLocation">
<option value="none" selected="selected">Select Location</option>
<?php
$getLocation=mysql_query("SELECT * FROM locationTable ORDER BY Location");
while($r=mysql_fetch_array($getLocation)){
extract($r);
echo "<option value='" . $Location_id . "'>" . $Location . ": " . $City . ", " . $Country . ", " . $GSL_id . "</option>";
}
?>
</select>
<br />
Location Contact:
<select name="servLocCon[]" multiple='multiple' size="3">
<option value="none" selected="selected">Select Contact</option>
<?php
$getContacts=mysql_query("SELECT * FROM hpatt.contacts ORDER BY Last_Name");
while($r=mysql_fetch_array($getContacts)){
extract($r);
echo "<option value='" . $Contact_id . "'>" . $Last_Name . ", " . $First_Name . "</option>";
}
?>
</select><br />
Application Contact:
<select name="servAppCon[]" multiple='multiple' size="3">
<option value="none" selected="selected">Select Contact</option>
<?php
$getContactsB=mysql_query("SELECT * FROM hpatt.contacts ORDER BY Last_Name");
while($r=mysql_fetch_array($getContactsB)){
extract($r);
echo "<option value='" . $Contact_id . "'>" . $Last_Name . ", " . $First_Name . "</option>";
}
?>
</select><br />
<input type="submit" name="submit" value="Submit" />
</form>
Okay, So what I want to do is have the user to have the option to select multiple location contacts and application contacts for 1 server. Which is accomplished using the size and multiple attributes. Then I want a separate record to be inserted into the MySQL table to read like the following:
Record 1:
ServerA, Wendel Holmes, John Smith
Record 2:
ServerA, Wendy Martin, John Smith
Record 3:
ServerA, Wendel Holmes, Juanita Gomez
Record 4:
ServerA, Wendy Martin, John Smith
So that every possibility is handled and then can be extracted. I can do this manually, by creating records individually, but PHP should be able to do this.
for example for location field who have multiple value, you can create a table with the same name (location: id, name) and other table named location_server(id_location,id_server)
I'm not sure I quite follow you, but say I had a table:
SID Server Name
1 S1
2 S2
3 S3
I would make an xref table, such as:
SID Prop1 Prop2 ... PropX
1 1 2 11
1 2 5 12
2 1 1 33
3 4 2 4
In this case the SID is used multiple times in the xref table.
Each prop is actually an ID into some other table.
Then I make a multi-column key on the xref table to prevent duplicate rows.
You can get all the POST/GET results of a multi select in a similar fashion as to a single select.
For example, using your serverLocCon[]:
$sLocCon = $_POST['servLocCon'];
foreach ($sLocCon as $item)
...

Hide options in a select list using jQuery

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);
});

Categories