|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
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>
|
|
#2
|
||||
|
||||
|
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);?>"); |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > How to get PHP array into Javascript variable? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|