Issue
I have two drop down menus where the options are not get from the database.
The first one, lets the user to select a category.
<select name="category">
<option value="0">None</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
</select>
The options of the second, are depended from the choice in the first dropdown menu. For example, if the user chooses the First option then the second dropdown will show
<select name="items">
<option value="3">Smartphone</option>
<option value="8">Charger</option>
</select>
but when the user change his mind, or select the second option first, then the second dropdown will now show
<select name="items">
<option value="1">Basketball</option>
<option value="4">Volleyball</option>
</select>
My question is how can I achieve this ? Can this be done without using a database?
Thank you!
Solution
See below to see the Working Example without using a Database.
Working Example Using MySQL Database
If you wanna connect it using Database, yeah, it is surely possible. Consider this table:
CREATE TABLE `contents` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR (255),
`parent` INT DEFAULT 0
);
INSERT INTO `contents` (`name`, `parent`) VALUES
('Names', 0),
('Places', 0),
('Animals', 0),
('Praveen', 1),
('Bill Gates', 1),
('Steve Jobs', 1),
('India', 2),
('New York', 2),
('London', 2),
('Singapore', 2),
('Cat', 3),
('Dog', 3),
('Tiger', 3),
('Deer', 3)
Table Structure
+----+------------+--------+
| id | name | parent |
+----+------------+--------+
| 1 | Names | 0 |
| 2 | Places | 0 |
| 3 | Animals | 0 |
| 4 | Praveen | 1 |
| 5 | Bill Gates | 1 |
| 6 | Steve Jobs | 1 |
| 7 | India | 2 |
| 8 | New York | 2 |
| 9 | London | 2 |
| 10 | Singapore | 2 |
| 11 | Cat | 3 |
| 12 | Dog | 3 |
| 13 | Tiger | 3 |
| 14 | Deer | 3 |
+----+------------+--------+
Initial HTML & PHP Code
Now, lets use PHP to first populate the initial <select>
:
<?php
mysql_connect();
mysql_select_db("contents");
$result = mysql_query("SELECT * FROM `contents` WHERE `parent` = 0");
while(($data = mysql_fetch_array($result)) !== false)
echo '<option value="', $data['id'],'">', $data['name'],'</option>'
?>
Now the <select>
is ready. With its onchange function, we can fire an AJAX event to get the new <select>
with the data provided by the parent <select>
.
<select onchange="ajaxfunction(this.value)">
<!-- Options would have been initially populated here -->
</select>
Now for the jQuery function, you can do this way:
<script type="text/javascript">
function ajaxfunction(parent)
{
$.ajax({
url: 'process.php?parent=' + parent;
success: function(data) {
$("#sub").html(data);
}
});
}
</script>
In the HTML, after the <select>
, you need to give another select
with an id
as sub
.
<select onchange="ajaxfunction(this.value)">
<!-- Options would have been initially populated here -->
</select>
<select id="sub"></select>
Processing PHP Source Code
Finally the source code of process.php
:
<?php
mysql_connect();
mysql_select_db("contents");
$result = mysql_query("SELECT * FROM `contents` WHERE `parent` = " . mysql_real_escape_string($_GET["parent"]));
while(($data = mysql_fetch_array($result)) !== false)
echo '<option value="', $data['id'],'">', $data['name'],'</option>'
?>
Working Example without using a Database
You just need to replace this in the PHP.
<?php
$parent = array("Name", "Place", "Animals");
foreach ($parent as $id => $name)
echo '<option value="s', $id,'">', $name,'</option>'
?>
And for the process.php
:
<?php
$parent = array("Name", "Place", "Animals");
$s0 = array("Praveen", "Bill Gates", "Steve Jobs");
foreach (${$_GET["parent"]} as $id => $name)
echo '<option value="', $data['id'],'">', $data['name'],'</option>'
?>
Answered By - Praveen Kumar Purushothaman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.