I want to convert my dropdown to ajax auto search.
Here is my dropdown, when i select any company name it displays other 2 dropdowns by taking the id.
<select name="company" class="form-control" onChange="showSubcat(this);" required>
<option value="">Select Company</option>
<?php $s1 = mysqli_query($con, "select * from company") or die (mysqli_error($con));
while($s2 = mysqli_fetch_array($s1)) { ?>
<option value="<?php echo $s2['id']; ?>"><?php echo $s2['company']; ?></option>
<?php } ?>
</select>
In showSubcat(), i display 2 dropdowns from database.
As i have huge company names stored in database, its difficult to select from dropdown , so i want to convert this into ajax auto search. I tried to do like this
<input type="text" name="company" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Company Name">
Here i am facing 2 issues
This is taking company name , but i want to take company id so that
i can display other 2 dropdowns
Not getting how to display 2 drop down based on this value, not getting how to call onChange()
Related
How to auto select the save value fetched from database in to select HTML tag.
I have a list of rooms with Hotel name , room type , facilities , descriptions in a table. i want to edit that record . when i click on edit button it fetch room_id to edit row accordingly, all other values successfully auto fetched in textboxes except values in select tag.
Here is my code to fetch values from database and echo to corresponding textboxes , except select box . i would like to auto select value in select box.
$query = "SELECT * from room where room_id = '$id'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
while($row=mysqli_fetch_assoc($result))
{
$room_id=$row['room_id'];
$room_type_id=$row['room_type_id'];
$facilities = $row['facilities'];
$long_description=$row['long_description'];
}
?>
<form action="Edit_Room_Script.php" method="post" enctype="multipart/form-data">
<label class="form-label">Hotel Name</label>
<select class="form-control" name="name" id="name">
<?php
$sel_cus = "select hotel_name from hotels ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['hotel_id'];?>"><?php echo $row['hotel_name'];?></option><?php}?>
</select>
<label class="form-label">Room Type</label>
<select class="form-control" name="room_type" id="room_type">
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>">
<?php echo $row['Room_Type_Name'];?></option>
<?php
}
?>
</select>
<label class="form-label">Facilities</label>
<input class="form-control" type="text" id="facilities" value="<?php echo $facilities;?>" name="facilities" autocomplete="Off" required >
<button class="btn btn-success btn-cons" type="submit" name="update" id="update"> Update Room</button>
</form>
i have a row in my html table is like below
Sr Hotel Name Roomtype Facility Action
1 Serena Super ABC Edit
When i click to edit button it take me to edit from where facility value successfully auto fetched from database and set in text box but hotel name and room type is not set. In select tag for hotel name and room type it populates all the hotel name and room type except serena and super how could i achieve this Please guide with some code.
Thank you
Some modifications:
1) Get $hotel_id
2) To show <select> element selected, you need to add selected="selected" attribute in your <option> tag.
If you have a drop down HTML:
<select class="form-control" name="room_type" id="room_type">
<option value="1">Villa</option>
<option value="2">Terrace</option>
<option value="3" selected="selected">Gallery</option>
<option value="4">Delux</option>
</select>
Drop down will show Gallery selected.
In your case, you can show it by:
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>"
<?php if ($room_type_id == $row['Room_Type_Id']) { echo 'selected="selected"';}?>>
<?php echo $row['Room_Type_Name'];?></option>
<?php
}
?>
3) Also, first fetch the arrays from database and then loop over them in the <select> element as writing fetching code directly in <select> is a bad practise and in case of any issue, it my expose your database field names.
I have about 10 categories and building a search form, But i want to show all categories & even a single category in option. How i can do that? So user can search in all categories with a single phrase.
<input type="text" class="form-control" value="" name="searchterm" id="searchterm" placeholder="Start Searching..." style="width: 60% "/>
<select id="fcat" class="form-control" name="catname" style="width:40%;" required />
<option value="">Select Category</option>
<?php $fetch_category = mysql_query("SELECT * FROM category ORDER BY categoryname ASC");
while($throw_category = mysql_fetch_array($fetch_category)) {
echo '<option value="">All Categories</option> <option value="'.$throw_category['categoryname'].'">'.$throw_category['categoryname'].'</option>'; } ?>
</select>
That would be more helpful if you guys could tell me a mysql query to search with a phrase in all categories. I have another table called companies and categories so if someone is search a phrase like "some business name" and select all categories then should show all companies within all categories.
Thanks in advance...
Remove 'All Categories' option from while loop.
Add some value for 'All Categories' option.
In the form action page based on the select box option write/generate a query.
Form code
<?php $fetch_category = mysql_query("SELECT * FROM category ORDER BY categoryname ASC");
while($throw_category = mysql_fetch_array($fetch_category)) {
echo ' <option value="'.$throw_category['categoryname'].'">'.$throw_category['categoryname'].'</option>';
} ?>
<option value="all">All Categories</option>
</select>
i want to post the value selected from the category table to software table by a php form but when i post the form it posting the id only instead of the value i selected
<form name="post" id="form" action="" method="post" enctype="multipart/form-data">
<select name="category" id="category">
<option value="<?php echo $category"><?php
$categoryqu="SELECT * FROM categories";
$results=mysql_query($categoryqu);
while($row=mysql_fetch_assoc($results)){
$id=$row['cid'];
$name=$row['name'];
echo"<option value='".$id."'>".$name."</option>";
}?>
</option>
</select>
<input placeholder="Title" name="title" value="<?php echo $title; ?>" type="text">
<input placeholder="Title" name="title" value="<?php echo $link; ?>" type="text">
<input placeholder="Title" name="title" value="<?php echo $description; ?>" type="text">
category table is
Category Table Image Link
and its posting only id instead of category names
here is the table posting the data from dropdown
when i post the form it posting all are well but only the value of selected category is posting the value "id" but i want it post the selected value to the database where is the what is the error on this code
But you use "mcid" to fetch the value.
its just because in you php code
$id=$row['mcid'];
$name=$row['name'];
echo"<option value='".$id."'>".$name."</option>";
you are passing $id as a value so it will take its as a value of that tag,
simply just go with this
//$id=$row['mcid'];
$name=$row['name'];
echo"<option value='".$name."'>".$name."</option>";
Drop downs only post selected value not the title if you want to post both you can use some sort of delimiter.
Example:
echo"<option value='".$id."#".$name."'>".$name."</option>";
I am new in this forum..Forgive me for any kind of mistake & help me.
I have a form with only two fields,first one textfield & next dropdown list.Now I want to show value in list from database based on textfield value from database.i.e If I type perfect username it will show me in dropdown the corresponding emailid(s) of that user.It will change after the username being changed.
Hope someone will help me in this matter.I was working a long time,but cant satisfied.Thanks in advance.This is the code what I have tried.But I want just the reverse.
`
function CBtoTB()
{document.getElementById("username").value=document.getElementById("usernameselect").value}
<?php
$result=mysql_query("select Username from users");
$options="";
while ($row=mysql_fetch_array($result)) {
$username=$row["Username"];
$options.="<OPTION VALUE=\"$username\">".$username.'</option>';
}
?>
<select name="usernameselect" id="usernameselect" onchange="CBtoTB()">
<option value="">< select user ><?php echo $options ?></option>
</select>
<input name="username" type="text" id="username" value="<?php echo $username ?>" size="25" readonly="readonly" />`
You option was writen wrongly please try this :
<select name="usernameselect" id="usernameselect" onchange="CBtoTB()">
<option value="">< select user ></option>
<?php echo $options ?>
</select>
I am working with creating an ad posting page. I have included dependable select boxes as the category selection system. All the values are being pulled from MySQL database. The table is composed of an id (auto increment) bridged by master_id and identified by category name. Every category has a subcategory. So the user is forced to pick a subcategory before being able to submit the info. I am currently looking for ideas of how to post info that has been input by user depending on the category chosen. I have created an individual page for each subcategory. So every time an input is submitted, I want it to display in the page accordingly. For example, if I pick category: Books and subcategory:Textbooks then post it in textbooks.php. Live EXAMPLE
Table Structure
Drop down menus
<form action="" method="post">
<select name="category" id="category" size="7" class="updateCategory" >
<option selected="selected" value="">Select one:</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="updateCategory"
disabled="disabled" hidden="hidden" >
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
</br>
Insert your name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
This is a little bit of a shot in the dark, as the question was vaguely worded.
I think that, first, you will need to make an update to the database. You will need to create either a new table or a new column. I would personally choose the table as that provides more flexibility, like so: id, category_id, category_page (and you could put other columns in too). Obviously, category_id references back to the main table category.
Then, do a select like this:
SELECT [COLUMNS HERE] FROM category RIGHT JOIN category_page ON category_page.category_id = category.id
You now have the values you need.
Then, in your PHP, return the url to the page (if you are using JS to dynamically show the new page):
<option value="xyz" data-url="url-for-page.php">Category Name</option>