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



Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 8th, 2004, 12:05 PM
dotcomma dotcomma is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: New Zealand
Posts: 20 dotcomma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to get PHP array into Javascript variable?

I am trying to make a SELECT change when I choose an option from another SELECT. It seems the only way to do this is to use the onChange function in Javascript.
The frist SELECT is a list of paper options; when I select a particular option here, the second SELECT should change to the options available for that particular paper type (i.e. colour, size, weight).
I don't want to submit the form, for that's not supposed to happen until all boxes have been set and are ready to be inserted/updated into the database.
I've tried all sorts of code, with varying results. At one stage the second SELECT changed, but had only one real option and a whole bunch of "undefined"s! Another time I got 104 lines of each option!?? (Yes, I counted them!)
As it stands at the moment, with the code below, I just get the last value from the array.
If anybody can help me with this, I would be very grateful.
Here's the Javascript (the NEWARRAY is where I need the PHP array):
Code:
<script language="JavaScript">
var colours = new Array("<?php echo NEWARRAY;?>");

function swapColours(colours_array)
{
  var colours_select = window.document.frmStock.StockColour;
  var colour_array = eval(colours);
  setOptionColours(window.document.frmStock.StockCol  our, colour_array);
}
function setOptionColours(the_select2, colour_array)
{
  for (loop=0; loop < the_select2.options.length; loop++)
  {
    the_select2.options[loop].text = colour_array[loop];
  }
}
</script>


This is the code for the first SELECT:

Code:
<form name="frmStock" method="POST" action="addjob.php">
    <select name="OtherJobStock" onChange="swapColours (window.document.frmStock.OtherJobStock.options[selectedIndex].text);">
<!--    <select name="OtherJobStock" onChange="document.frmStock.submit()">//-->
      <option value="" SELECTED>-- Select Stock --</option>
      <?php
                      $rsStockName = mysql_query("SELECT DISTINCT stockid, stockname FROM stockrelations, stockname WHERE stockrelations.stockid=stockname.nameid") or DIE(mysql_error());
                      while ( $stock = mysql_fetch_array($rsStockName) )
                         {
                      ?>
      <option value="<?php echo $stock['stockid']?>"<?php if ($stock['stockname']==$Job['otherjobstock']) echo "SELECTED";?>> 
      <?php echo $stock['stockname']; ?>
      </option>
      <?php
                        }
                      ?>
    </select>
<br><br>


...and the second SELECT:

Code:
<select name="StockColour">
	<option value="">-- Select a Colour --</option>
<?php
$rsStockColour = mysql_query("SELECT DISTINCT 
stockname.nameid, stockname.stockname, 
stocksize, stockrelations.sizeid, 
stockcolour, stockrelations.colourid, 
stockweight, stockrelations.weightid
FROM stockname, stocksize, stockcolour, stockweight, stockrelations
WHERE stockrelations.stockid = '".$_POST['OtherJobStock']."' 
AND stockrelations.sizeid=stocksize.sizeid
 AND stockrelations.colourid=stockcolour.colourid
 AND stockrelations.weightid=stockweight.weightid") or DIE(mysql_error());

if($_POST['OtherJobStock']){
    while ( $stockcolours = mysql_fetch_array($rsStockColour) ){
?>
      <option value="<?php echo $stockcolours['stockcolour']." - ".$stockcolours['stocksize']." - ".$stockcolours['stockweight']." gsm";?>"<?php if ($stockcolours['stockcolour']==$Job['otherjobstock']) echo "SELECTED";?>> 
      <?php echo $stockcolours['stockcolour']." - ".$stockcolours['stocksize']." - ".$stockcolours['stockweight']." gsm";?>
      </option>
<?php
    }
}
?>
</select>

Reply With Quote
  #2  
Old May 8th, 2004, 04:37 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
I trust you know that Javascript can't manipulate PHP variables directly...
It sounds like you realize that...

That's quite the SQL statement you've got there... I trust you've tested it in your database?

I wonder if you're loading your JAvascript array with the proper values...
instead of using echo NEWARRAY, you might want to look into PHP's implode() function... I've used that in the past in a similar case...

Off the top of my head:
var colours = new Array("<?php echo implode($NEWARRAY);?>");

Reply With Quote
  #3  
Old May 10th, 2004, 05:09 AM
dotcomma dotcomma is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: New Zealand
Posts: 20 dotcomma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It doesn't seem to be possible to do this on one page. Unless the form is submitted, there's no "$_POST['OtherJobStock']"!
So I am doing it over 2 pages now (perhaps 3); a bit of a nuisance.
I thought the SQL statement worked, but after some more testing it didn't always work. I've fixed it now though.

Thanks,
Toine

Reply With Quote
  #4  
Old June 2nd, 2004, 09:19 PM
sago78 sago78 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 sago78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

u could try the opossite
create the code of java script tht u need to do tht
somethin like this would work

------------------------------

<script language="JavaScript" type="text/javascript">
function combo(){
var indice=document.forms.nuevoPC.idmarca.selectedInde x;
if (indice ==0){
//document.forms.nuevoPC.idmodelo.options[0]=new Option('Todas','todas');
document.forms.nuevoPC.idmodelo.options[0]=new Option('Aspire',4);
document.forms.nuevoPC.idmodelo.options[1]=new Option('DeskPro 2000',1);
document.forms.nuevoPC.idmodelo.options[2]=new Option('Power 433',2);
document.forms.nuevoPC.idmodelo.length=3 }
if(indice == 1 ) menu1();
if(indice == 2 ) menu2();
}
//ACER
function menu1(){
document.forms.nuevoPC.idmodelo.options[0]=new Option('Power 433',2);
document.forms.nuevoPC.idmodelo.options[1]=new Option('Aspire',4);
document.forms.nuevoPC.idmodelo.length=2}
//Compac
function menu2(){
document.forms.nuevoPC.idmodelo.options[0]=new Option('DeskPro 2000',1);
document.forms.nuevoPC.idmodelo.length=1}
/*
function menu1(){
segundocombo=1;
sub0=new Option("elije una opcion","sub0","defauldSelected");
sub1=new Option("PhotoSmart 720","1");
sub2=new Option("Hpj3200A","2");
document.forms.nuevoPC.idmodelo.options[0]=sub0;
document.forms.nuevoPC.idmodelo.options[1]=sub1;
document.forms.nuevoPC.idmodelo.options[2]=sub2;
}
function menu2(){
segundocombo=2;
sub0=new Option("elije una opcion","sub0","defauldSelected");
sub1=new Option("Deskpro 2000","1");
sub2=new Option("Evo d310","2");
document.forms.nuevoPC.idmodelo.options[0]=sub0;
document.forms.nuevoPC.idmodelo.options[1]=sub1;
//document.forms.nuevoPC.idmodelo.options[2]=sub2;
}*/
</script>


:: Marca <SELECT name= "idmarca" id="idmarca" onChange="combo()">
<option value="0">Todas</option>
<option value= 1 > ACER</option>
<option value= 1 > Compac</option>
</SELECT>
:: Modelo <SELECT name= "idmodelo" id="idmodelo">
<option value="4">Aspire</option>
<option value="1">DeskPro 2000</option>
<option value="2">Power 433</option>
</SELECT>

i actually create somthin like this with a php script i did , its a little messy and need some celan up
but if u want, i could post some part

Saludos!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > How to get PHP array into Javascript variable?


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 - 2018, Jelsoft Enterprises Ltd.

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