Current location: Hot Scripts Forums » Programming Languages » PHP » how to pass php array to javascript


how to pass php array to javascript

Reply
  #1 (permalink)  
Old 12-22-05, 12:28 PM
jaishalg jaishalg is offline
Newbie Coder
 
Join Date: May 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
how to pass php array to javascript

Hey all,
I have a category dropdown list, which takes the category from the database.

now when a user select a category, it should display all the subcategory belong to the subcategory... now user can select more than one subcategory. thats why i had take array of subcategory, now can any one please tell me how do u pass the take this array value to the javascript. as i have to store more than one subcategory to the DB. i have to take an array of the subcategory.

please please help me .. here is my code please tell where am i wrong.

<script language="javascript">
function subdivchk()
{
ClearOptions(document.reg.subcategory);
var a = document.reg.category1.value;
<?php
$sql = mysql_query("select distinct Cat_Grp FROM rms_category");
while($res=mysql_fetch_assoc($sql))
{
$cname=$res['Cat_Grp'];
?>
if (a=='<?php echo $cname ?>')
{
<?php
$subsql=mysql_query("select * from rms_category where Cat_Grp='$cname'");
while($subres=mysql_fetch_assoc($subsql))
{
$scn=$subres['Name'];
$scname=$subres['MetaText'];
?>
AddToOptionList(document.reg.subcategory,"<?php print $scname; ?>","<?php print $scname; ?>", false );
<?php
}
?>
}
<?php
}
?>
}
function ClearOptions(OptionList)
{
for (x = OptionList.length; x >= 0; x = x - 1)
{
OptionList[x] = null;
}
}
function AddToOptionList(OptionList, OptionValue, OptionText,defSel)
{
OptionList[OptionList.length] = new Option(OptionText, OptionValue,"",defSel);
}
</script>
<?php
$res = mysql_query("SELECT distinct(Cat_Grp) FROM rms_category ORDER BY Cat_Grp") or die(mysql_error());
$category = "<select name=category1 OnChange='javascript:subdivchk()'>";
while ($row = mysql_fetch_assoc($res)){
$cntval = $row['Cat_Grp'];
$category .= "<option value='$cntval'> $cntval </option>";
}
$category .= "</select>";
$t->set_var("category_listing",$category);

//////////CODE FOR SUBCATEGORY
//$cntval = "";
//$res = mysql_query("SELECT * FROM rms_category where Cat_Grp='$cntval' ORDER BY Cat_Grp") or die(mysql_error());
$res = mysql_query("SELECT * FROM rms_category ORDER BY Cat_Grp") or die(mysql_error());
$subcategory = "<select name=subcategory[] multiple>";
while ($row = mysql_fetch_assoc($res)){
$subcategory .= "<option value=$row[MetaText] {".$row["Name"]."}> $row[MetaText] </option>";
}
$subcategory .= "</select>";

$t->set_var("sub_category_listing",$subcategory);


please please help me i m so much confused, how to do this???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-22-05, 01:21 PM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not entirely sure I understand you completely, but if you're trying to pass an array from PHP to JavaScript, you'll need to reload / refresh your page everytime you want to do this. Instead I would recommend looping through the PHP array (on the initial load) and simply echo the array into a JavaScript array, basically load everything at the start.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-23-05, 04:28 AM
jaishalg jaishalg is offline
Newbie Coder
 
Join Date: May 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
i dont want to refresh the page, without refressing the page o want to change the subcategory accourding to its category

its nicely working for the single subcategory.
but how can i set this for the multiple selection of the subcategory
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Javascript Variable to PHP watadude PHP 4 01-15-11 07:44 PM
Accessing PHP array in HTML to make radio buttons Newbie2005 PHP 35 10-20-05 02:32 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-25-05 12:09 AM
javascript multiple select menu for php? isaacmlee JavaScript 1 10-15-04 10:53 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 07:00 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.