JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 14th, 2001, 06:11 PM
Dalamar Dalamar is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 4 Dalamar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question getting javascript error when using PHP and javascript

I'm having some problems with some code I've been working on. I'm creating a form in HTML with 3 combo boxes. The data for the combo boxes is filled dynamically from arrays in javascript. The arrays are populated using PHP to access a mySQL database.
The selection made in the first combo box will decide what is available in the second combo box and the choice in the second combo box affects the third. I think I got the hard part taken care of and a look at "View Source" is giving me a good indication that my PHP is creating the arrays correctly for the javascript. The problem comes as soon as I make my selection in the first combo box I get a javascript error saying that the array that the javascript function calls is undefined. Here is the line that the error points to:

<SELECT id=sbuChoice name=sbuChoice onchange="selectChange(this, productChoices.familyChoice, arrFamilyName, arrFamily);">

It specifically mentions arrFamilyName which is one of the arrays.
Here's the code for the definition of the arrary and the populating of it:

var arrFamily = new Array();
var arrFamilyName = new Array();

<?php
mysql_select_db("solutions")
or die("Could not select database");
$sql = mysql_query("SELECT * FROM sbu");
while($row=mysql_fetch_row($sql)) {
$sbu_id = $row[0];
$sbu_name = $row[1];

$sql2 = mysql_query("SELECT * FROM family where sbu_id=$sbu_id");
while ($row2=mysql_fetch_row($sql2)) {
$family_name = $row2[0];
$family_id = $row2[1];
echo "arrFamily[] = ".$sbu_id.";";
echo "arrFamilyName[] = \"".$family_name."\";";
}
}
?>


The array clearly appears to be defined and I can't understand why I am having a problem. I think it may be related to the PHP only because the same javascript code will work if I take out all the PHP and put in static arrays.

If anyone has any ideas that would be greatly appreciated.
Let me know if you need to see part of the code I left out.

Thanks again.

-Steve

Reply With Quote
  #2  
Old December 14th, 2001, 07:09 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
What does the same code look like after the page has been built? The javascript code block that is.

Reply With Quote
  #3  
Old December 14th, 2001, 07:25 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
Looking at the code structure for an array in javascript, you may be using it wrong. Not for sure, but how about trying something like this:
PHP Code:
<?php
mysql_select_db
("solutions") or die("Could not select database");
$sql mysql_query("Select * from sbu");
while(
$row mysql_fetch_row($sql)) {
    
$sbu_id[] = $row[0]; //we will use this to build the js array
    
$sbu_name $row[1]; //really don't understand why you are gathering this, it is not being used

    
$sql2 mysql_query("Select * from family where sbu_id=$row[0]");
    while(
$row2 =  mysql_fetch_row($sql2)) {
        
$family_name[] = $row2[0];
        
$family_id $row2[1];  //really don't understand why you are gathering this, it is not being used
    
}
}
$arrFamily implode(",",$sbu_id);
$arrFamilyName implode("\",\"",$family_name); //this one should combine to "element1","element2","etc..."
echo "var arrFamily = new Array($arrFamily);\n";
echo 
"var arrFamilyName = new Array($arrFamilyName);\n";
?>

Last edited by Onslaught : December 14th, 2001 at 07:31 PM.

Reply With Quote
  #4  
Old December 14th, 2001, 07:26 PM
Dalamar Dalamar is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 4 Dalamar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This is copied directly from a "View Source":

function selectChange(control, controlToPopulate, ItemArray, GroupArray)
{
//Generate all arrays for families
var arrFamily = new Array();
var arrFamilyName = new Array();

arrFamily[] = 1;arrFamilyName[] = "Controller";arrFamily[] = 1;arrFamilyName[] = "Settop";arrFamily[] = 1;arrFamilyName[] = "Headend";arrFamily[] = 2;arrFamilyName[] = "Controller";arrFamily[] = 3;arrFamilyName[] = "Cable Modem";


The only thing I see off is the formatting which as far as I know doesn't matter.

Reply With Quote
  #5  
Old December 14th, 2001, 07:34 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
Like I said in my last post, I think you are using the array wrong. I may be wrong, but I could find no reference where js using automatic incrementing indexes for arrays. This is the way you are using it.
As far as I know the formatting does not matter, just easier to read.
How do you declare the arrays(and values) when you do it without php?

Reply With Quote
  #6  
Old December 14th, 2001, 08:03 PM
Dalamar Dalamar is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 4 Dalamar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think you're right....when I was doing it manually in javascript I was indexing them. I probably could index them the way I was doing but your way seems smarter and chews up less code. One problem I'm getting now though is I'm getting a Warning: Bad arguments to implode() on line 55

I've entered it exactly as you had above and double checked the syntax for implode since I've never used it before. Is it possible my PHP server just sucks?

Reply With Quote
  #7  
Old December 14th, 2001, 08:23 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
try using single quotes instead:
PHP Code:
 $arrFamilyName implode("','",$family_name); 
//or like this
foreach($family_name as $fn) {$arrFamilyName .= "\"$fn\",";}
$arrFamilyName substr($arrFamilyName,0,(strlen($arrFamilyName)-1));  //to remove the last , this may need to be 2 instead of 1 

Reply With Quote
  #8  
Old December 16th, 2001, 12:14 PM
Dalamar Dalamar is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 4 Dalamar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

Looks like it's all working now! Thanks for all the help Onslaught!

Reply With Quote
  #9  
Old December 17th, 2001, 07:41 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
No problem.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > getting javascript error when using PHP and javascript

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap