Ok, so assuming you have a drop-down and want to use jquery to fetch new content into a display area based on its value, simply do something like this:
HTML (or PHP) Front End
<select id='category'>
<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
</select>
<div id='display_area'></div>
- Notice that each
option
as the value
parameter set appropriately
JavaScript/jQuery
<script type="text/javascript">
$(document).ready(function(){
$('#category').change(function(){
//Retrieve Content from the back-end PHP page, and pass the ID selected
var url = 'fetch_category.php?id=' + $(this).val();
$('#display_area').load(url);
});
});
</script>
- The function will call the back-end PHP page and pass the selected ID
Back-end fetch_category.php
<?php
//Resolve the ID passed by the Javascript Function
$id = $_REQUEST['id'];
//Generate Content Specific to the ID contained in $id
?>
- Anything Generated in this PHP file will be populated in the designated display area
ALTERNATIVE METHOD
Rather than using ajax (depending on your needs) you could just use PHP in the initial page generation to create a div for each category content. You can use an id for each like id='category_1'
so that your JS will look like this:
<script type="text/javascript">
$(document).ready(function(){
$('#category').change(function(){
var id = $(this).attr('id');
$('#display_area').html($('#category_' + id).clone());
});
});
</script>