I'm making a website, where I should be able to delete some brands from a database, the brands is stored in categories.
In my form, I have a that echo's out all the categories in my database.
The next should then contain all brands that is within' my selected category.
My categories contains a categoryID which is the value of the in the first dropdown - The brands is having it's own ID, but also having the categoryID, from the category they're in.
How would you do this? - And can you give an example, it might need javascript or jquery, which is fine with me.
This is my code:
<form action="deleteBrand.php" method="post">
<fieldset class="delete">
<legend>Delete Brand</legend>
<div>
<label for="categoryid">Category:<br />
<?php
$stmt = $db->prepare("SELECT categoryid, category FROM categories");
$stmt->execute();
echo "<select name='categoryid' onchange='this.form.submit()'>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='{$row['categoryid']}'>{$row['category']}</option>";
}
echo "</select>";
?>
</label>
</div>
<div>
<label for="brand">Brand Name:<br />
<?php
$stmt = $db->prepare("SELECT brands.id, brand FROM brands WHERE brands.categoryid = ")
?>
</label>
</div>
<div>
<input type="submit" value="Delete Brand" />
</div>
</fieldset>
</form>
I'm still missing the last dropdown, but if I just can get the value from the selected item from the first dropdown, into a variable, it shouldn't be hard for the rest of the code!
Thank you a lot
The below code is an example of how you can get the selected option value for a dropdown box. Depending on your needs, you can either preload your category id->brand mappings into a javascript variable that you can refer to, or use an ajax request to get the appropriate brands for your second combo box on demand, when the box is clicked.
<script>
function getSelected(obj)
{
alert(obj.options[obj.selectedIndex].value);
// Now use AJAX or preloaded variable to figure out
// what brands map to this category, and populate
// your second combobox
}
</script>
<form action="deleteBrand.php" method="post">
<label for="categoryid">Category:<br />
<select name='categoryid' onchange=getSelected(this)>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
</form>
Related
I am building a shopping website whereby I give the users the opportunity to filter the food items according to category and price (Low to high and vice-versa). I have implemented this using a dropdown list.
I am now having an issue displaying the products. I have to use AJAX to filter the results. On page load I have set the default category to 'All' where all the food items have to be displayed and as I change the category the food items get filtered accordingly.
The issue is that I use $("#Category option:selected").val(). I get the value as the page gets loaded and if I use on change, there will be an action only on change but nothing happens on default value. How shall I handle this?
<div class="collapse" id="filterdiv">
<form class="d-inline">
<select id="Category">
<option value='' selected>All</option>
<?php
$fCategory="SELECT DISTINCT Food_Type from food";
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$res=$conn->query($fCategory);
if($res->rowCount()>0)
{
while($row=$res->fetch(PDO::FETCH_ASSOC))
{
echo "<option value=".$conn->quote($row['Food_Type']).">".$row['Food_Type']."</option>";
}
}
?>
</select>
<select id="price">
<option value="">Price</option>
<option value="lowtohigh">Low to High</option>
<option value="hightolow">High to Low</option>
<</select>
</form>
</div>
<div class="row" id="result">
<!-- I will display the result from the query here using ajax -->
</div>
If you trigger a change event on the select when the page loads it will call the onchange event handler you have set up (you do have one right?)
$(document).ready( function() {
$('#Category').trigger('change');
});
I have a problem about a drop-down menu. I retrieved the data from my database in order to create the actual drop-down menu and now I want to use it for filtering purposes. I managed to get the "SEARCH" field working and actually filtering the results based on what the user typed, but I want also to allow the user to filter the data by selecting a specific town from the drop down menu. When the user chooses a town from the list I want to retrieve all the informations from the jobs table (jobname, jobtype, town, department, experiencelevel). I searched this website and I couldn't find any topic about doing this with only PHP code. I also included the connection with the database.
I tried to get it working with if(isset($_POST['dropbox'])) same as I did with the search field, but it just gives me no results every time I choose something.
Creating the drop-down code:
<div class="sect1">
<h2> Finding a job has never been so easy! </h2>
<div class="jobsearchbox">
<h3> Search for opportunities </h3>
<form action="jobsearch.php" method="POST">
<input type="text" name="indexjobsearch" placeholder="What job are you looking for?"/> <br>
<select name ='dropbox'>
<?php
while($rows = mysqli_fetch_assoc($result)){
$jobtypename = $rows['town'];
echo "<option hidden disabled selected value> Which town? </option>
<option name='selecttype' value='typejob'> $jobtypename </option>";
}
?>
</select><br>
<button type="submit" name="job-search"> Search now </button>
</form>
</div>
</div>
Actually the issue was, the value of your option tag was static(value='typejob') so in that case, no matter which option the user selected it was always going to return "typejob" instead of the value the user selected. To fix the problem you just have to change the value of your option tag from "typejob" to "$jobtypename" as i have done in the code below.
<div class="sect1">
<h2> Finding a job has never been so easy! </h2>
<div class="jobsearchbox">
<h3> Search for opportunities </h3>
<form action="jobsearch.php" method="POST">
<input type="text" name="indexjobsearch" placeholder="What job are you
looking for?"/> <br>
<select name ='dropbox'>
<?php
while($rows = mysqli_fetch_assoc($result)){
$jobtypename = $rows['town'];
echo "
<option hidden disabled selected value> Which town? </option>
<option name='selecttype' value='$jobtypename'> $jobtypename
</option>
";
}
?>
</select><br>
<button type="submit" name="job-search"> Search now </button>
</form>
</div>
</div>
The type of sport-options are selected from a database where I have all the type of sports.
Type_sport contains the name of the sport. Everything works great and all the sports in the database are shown in an option list. But how do I $_REQUEST what type of sport the user have choosen when the options are comming from a database?
The code in the form looks like this:
<p>Choose sport: </p>
<form action="page2.php" method="post">
<select name="sports">
<option>Choose sport</option>
<?php
$db=mysqli_connect("xxxxxxxx","xxxxx","","xxxxx");
$sql="Select* from sports";
$result=mysqli_query($db,$sql);
$numberOfRows=mysqli_affected_rows($db);
for($i=0;$i<$numberOfRows;$i++){
$row=mysqli_fetch_array($result);
echo "<option> ".$row["type_sport"]."</option>";
}
?>
</select>
</form>
I have tried: $sport=$_REQUEST["sports"]; but it does not work.
`
You need to give the option tag a value attribute that's the only way you'll get the value from the request superglobal echo "<option value='".$row["type_sport"]."'> ".$row["type_sport"]."</option>";
I have a locations dynamic dropdown (country,region,city) done with ajax. Works great.
My goal is to retrieve data (posts) from mysql table, based on the location selections. I already have the posts in mysql table that correspond to the locations.
Now the issue I am having is that the query won't retrieve data based on the region and city. It will only work with the $_GETcountry. It comes to my understanding the reason is because region and city <select> queries are in a separate PHP files and are called through js code in the head section, so I am unable to use $_GET method to get their inputs.
Here is my form setup:
<form action="" method="get" enctype="multipart/form-data >
<div id="countrydiv">
<select id="country" name="country" onChange="showRegion(this.value);">
<option value="0">--Select Country--</option>
<?php
$getCountry = DB::getInstance()->query("SELECT * FROM countries");
if(!$getCountry->count()) {
echo 'No Country found!';
} else {
foreach($getCountry->results() as $row) {
$country_id = escape($row->countryId);
$country_name = escape($row->countryName);
?><option value="<?php echo $country_id; ?>" ><?php echo $country_name; ?></option><?php
}
}
?>
</select>
</div>
<div id="regiondiv"></div>
<div id="citydiv"></div>
<input type="submit" value="Search">
</form>
I can get the country input name from above with this $_GET['country'], but I also need it for the region and city. How can I do that?
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>