/    Sign up×
Community /Pin to ProfileBookmark

i need help in javascript function :S !

i need help in javascript function :S !
———————————————

hi guys,
i need help in this thing :
i have 2 ‘<select></select>’

1 for the main category | and | 1 to sub category

how can i allow automatic refresh and change to the sub category if i choose a main category ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@codefishMay 15.2012 — use the onchange event of the main category and either refresh the page or use ajax to get the html only of the subcatecory select
Copy linkTweet thisAlerts:
@mekhaauthorMay 16.2012 — i used the ajax:
[code=php]
<?php
require_once("base2.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script src="jquery-1.7.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(document).ready(function(){



$('#maincategory').change(function() {
var id= $(this).val();
$.ajax({

type: "POST",
url:'subcat.php',
data: "aid="+id+,

success: function(data) {
if(data){

jQuery("#childcategory").html(data) ;
}
}
});




})
</script>
</head>

<body>


<form action="" method="post">
<select id="maincategory">
<?php
mysql_query("SET NAMES 'utf8'");
$result2 = mysql_query("SELECT Id as value,category_name as title FROM categories");
while($row = mysql_fetch_assoc($result2)){
extract($row);

?>
<option value="<?=$value?>"><?=$title?></option>
<?php
}
?>
</select>

<select id="childcategory">

</select>

</form>


</body>
</html>
[/code]


and this is the: subcat.php
[code=php]
<?php
include 'base2.php';
$cat =mysql_real_escape_string($_POST['aid']);
mysql_query("SET NAMES 'utf8'");
$result22 = mysql_query("SELECT sub_id as value2,sub_name as title2 FROM sub_categories WHERE category_full=".$cat);
while($row2 = mysql_fetch_assoc($result22)){
extract($row2);

?>
<option value="<?=$value2?>"><?=$title2?></option>
<?php
}
?>
[/code]
×

Success!

Help @mekha spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...