PDA

View Full Version : Selecting data from a drop down box


theowls
06-18-2015, 11:58 PM
Sorry if this has been asked before, did do a search.

I have a drop down box that gets its data from a database, I want the user to select a value from the drop down box which will take them to another page.

Can someone give me a bit of a heads up on this please?

Thanks in advance

edbr
06-19-2015, 12:47 AM
whats your code so far?

theowls
06-19-2015, 01:12 AM
<?php
include('config.php');
$query_parent = mysqli_query($con,"SELECT * FROM categories") or die("Query failed: ".mysqli_error());
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dependent DropDown List</title>
<style>
form {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 18px;
}
select {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 18px;
}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="img/loading.gif" alt="loading subcategory" /></div>');
$.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
$("#sub_cat").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
});
});

});
</script>
</head>

<body>
<form method="get">
<label for="category">Cuisine</label>
<select name="parent_cat" id="parent_cat">
<?php while($row = mysqli_fetch_array($query_parent)): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>

<label>Restaurant</label>
<select name="sub_cat" id="sub_cat"></select>
</form>
</body>
</html>

edbr
06-19-2015, 02:24 AM
so you want to add a link to this?
<option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?></option>

what will the page be to link to. meaning how will it be called?

theowls
06-19-2015, 02:36 AM
Let's say the page will be called indian.php

edbr
06-19-2015, 02:44 AM
ok as you are creating the link from database it shpouls also be a dynamic name.
example in html
<a href="indian.php"> indian</a>

dynamically it could be
<a href="<php echo $row['category_name']"> <php echo $row['category_name']"></a>

if 'category_name' is the correct data

theowls
06-19-2015, 02:46 AM
thanks for all the trouble in helping me..

edbr
06-19-2015, 02:50 AM
welcome post back if you have trouble

theowls
06-20-2015, 03:27 AM
Hi edbr

Sorry but where in the above code would I position -

<a href="<php echo $row['category_name']"> <php echo $row['category_name']"></a>

as you stated?

Thanks

edbr
06-20-2015, 03:48 AM
it would be in your option tags but you need to edit to the correct rows form your database as i dont know what they contain, so i was giving you an example.
imn your code you show a <select> containing a while loop for the <option>

using my example ( whics depends on your database data would be

<select name="parent_cat" id="parent_cat">


<?php while($row = mysqli_fetch_array($query_parent)): ?>

<option><a href="<php echo $row['category_name']"> <php echo $row['category_name']"></a></option>


<?php } ?>


</select>