Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
getting php variables into js function
Old 02-21-2006, 05:38 PM getting php variables into js function
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Im having a bit of trouble getting information from php into javscript. I know php is server side and js is browser but im sure this can work as all my variables are set on page load. The problem is variables codeofheight and codeofwidth in my js function - it works if i replace codeofwidth and codeofheight variables with appropriate values, but not otherwise. Can someone show me how this can work please.


[js]
<script>
var codeofheight = doorglass.codeofheight.value;
var codeofwidth = doorglass.codeofwidth.value;


if(document.images)
{
bar = new Array()
for(i=0;i<3;i++)
{
bar[i] = new Image()
bar[i].src = "images/"+codeofwidth+"_"+codeofheight+"_bar" + i + ".gif"
} }
function barsauto1()
{ if(document.images) { choose=document.doorglass.door1bars.options[document.doorglass.door1bars.selectedIndex].text.charAt(0)
document.barswitch1.src = "images/"+codeofwidth+"_"+codeofheight+"_bar"+choose+".gif " } }
window.onerror = null;
</script>
<?php

$codeofheight='1600'; //in the real script these variables are set by a series of if statements, determined by info from previous page.
$codeofwidth='2ds';


echo"
<form name='doorglass' action='somewhereelse.php'>
<input type='hidden' name='codeofheight' value='$codeofheight'>
<input type='hidden' name='codeofwidth' value='$codeofwidth'>
</form>
";?>

[/js]
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-21-2006, 07:24 PM Re: getting php variables into js function
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
Would this work?
HTML Code:
<?php 

    $codeofheight='1600'; //in the real script these variables are set by a series of if statements, determined by info from previous page. 
    $codeofwidth='2ds';

?>


<script>
var codeofheight = <?php echo $codeofheight; ?>
var codeofwidth = <?php echo $codeofwidth; ?>

if(document.images) 
{ 
    bar = new Array();
    for(i=0; i<3; i++)
    {
        bar[i] = new Image();
        bar[i].src = "images/"+codeofwidth+"_"+codeofheight+"_bar" + i + ".gif";
    }
} 

function barsauto1()
{
    if(document.images)
    {
        choose=document.doorglass.door1bars.options[document.doorglass.door1bars.selectedIndex].text.charAt(0);
        document.barswitch1.src = "images/"+codeofwidth+"_"+codeofheight+"_bar"+choose+".gif";
    }
} 

window.onerror = null; 
</script>
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-21-2006, 07:50 PM Re: getting php variables into js function
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
So the value of both those variables is not being echoed along with the rest of the form?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Old 02-22-2006, 12:36 PM Re: getting php variables into js function
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
No i only need the variables on this script. They are derived from session variables so i can use them wherever on the site i like.

Unfortuanately var codeofheight = <?php echo $codeofheight; ?> doesn't seem to make any difference. I think i may have misled when i said replacing the variables works. what does work is this:

HTML Code:
if(document.images) 
{ 
bar = new Array() 
for(i=0;i<3;i++) 
{ 
bar[i] = new Image() 
bar[i].src = "images/"2ds_1800_bar" + i + ".gif" 
} } 
function barsauto1() 
{ if(document.images) { choose=document.doorglass.door1bars.options[document.doorglass.door1bars.selectedIndex].text.charAt(0) 
document.barswitch1.src = "images/2ds_1800_bar"+choose+".gif" } } 
window.onerror = null; 
and what doesnt work is this:
HTML Code:
var codeofheight = 1800;
var codeofwidth  = 2ds;  

if(document.images) 
{ 
bar = new Array() 
for(i=0;i<3;i++) 
{ 
bar[i] = new Image() 
bar[i].src = "images/"+codeofwidth+"_"+codeofheight+"_bar" + i + ".gif" 
} } 
function barsauto1() 
{ if(document.images) { choose=document.doorglass.door1bars.options[document.doorglass.door1bars.selectedIndex].text.charAt(0) 
document.barswitch1.src = "images/"+codeofwidth+"_"+codeofheight+"_bar"+choose+".gif" } } 
window.onerror = null;
I cant understand why it wouldn't though.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 02-22-2006, 01:06 PM Re: getting php variables into js function
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
I didnt know that it is about strings. The following should be working
HTML Code:
<?php 

    $codeofheight='1600'; //in the real script these variables are set by a series of if statements, determined by info from previous page. 
    $codeofwidth='2ds';

?>

<script>
var codeofheight = "<?php echo $codeofheight; ?>";
var codeofwidth = "<?php echo $codeofwidth; ?>";

if(document.images) 
{ 
    bar = new Array();
    for(i=0; i<3; i++)
    {
        bar[i] = new Image();
        bar[i].src = "images/"+codeofwidth+"_"+codeofheight+"_bar" + i + ".gif";
    }
} 

function barsauto1()
{
    if(document.images)
    {
        choose=document.doorglass.door1bars.options[document.doorglass.door1bars.selectedIndex].text.charAt(0);
        document.barswitch1.src = "images/"+codeofwidth+"_"+codeofheight+"_bar"+choose+".gif";
    }
} 

window.onerror = null; 
</script>
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-28-2006, 09:37 AM Re: getting php variables into js function
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
I cant undertand why it wont work. It looks like sensible coide to me. Ive settled for this horrendously long route to tackle a simple problem:
PHP Code:
<?php
session_start
();
if (
$_SESSION[apwidth] <= '1500')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/2ds1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/2ds1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/2ds2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/2ds2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/2ds2480.php');}
}
else if (
$_SESSION[apwidth] <= '2100' and $_SESSION[apwidth] >= '1501')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/2dl1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/2dl1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/2dl2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/2dl2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/2dl2480.php');}
}
else if (
$_SESSION[apwidth] <= '2550' and $_SESSION[apwidth] >= '2101')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/3ds1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/3ds1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/3ds2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/3ds2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/3ds2480.php');}
}
else if (
$_SESSION[apwidth] <= '3150' and $_SESSION[apwidth] >= '2551')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/3dl1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/3dl1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/3dl2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/3dl2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/3dl2480.php');}
}
else if (
$_SESSION[apwidth] <= '3600' and $_SESSION[apwidth] >= '3151')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/4ds1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/4ds1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/4ds2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/4ds2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/4ds2480.php');}
}
else if (
$_SESSION[apwidth] <= '4200' and $_SESSION[apwidth] >= '3601')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/4dl1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/4dl1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/4dl2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/4dl2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/4dl2480.php');}
}
else if (
$_SESSION[apwidth] <= '4650' and $_SESSION[apwidth] >= '4201')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/5ds1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/5ds1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/5ds2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/5ds2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/5ds2480.php');}
}
else if (
$_SESSION[apwidth] <= '5000' and $_SESSION[apwidth] >= '4651')
{
       if (
$_SESSION[apheight] <='1600')                                  {include('javascript/5dl1600.php');}
  else if (
$_SESSION[apheight] <='1800' and $_SESSION[apheight] >= '1601'){include('javascript/5dl1800.php');}
  else if (
$_SESSION[apheight] <='2000' and $_SESSION[apheight] >= '1801'){include('javascript/5dl2000.php');}
  else if (
$_SESSION[apheight] <='2200' and $_SESSION[apheight] >= '2001'){include('javascript/5dl2200.php');}
  else if (
$_SESSION[apheight] <='2480' and $_SESSION[apheight] >= '2201'){include('javascript/5dl2480.php');}
}
and each one of the include statements looks something like this

HTML Code:
<SCRIPT LANGUAGE="JavaScript" 
TYPE="text/javascript"> 
<!-- 

if(document.images) 
{ 
bar = new Array() 
for(i=0;i<3;i++) 
{ 
bar[i] = new Image() 
bar[i].src = "images/large_1800_bar" + i + ".gif" 
} } 
function barsauto1() 
    { if(document.images) { 
    choose=document.doorglass.door1bars.options[document.doorglass.door1bars.selectedIndex].text.charAt(0) 
    document.barswitch1.src = "images/large_1800_bar"+choose+".gif" 
    } } 
    window.onerror = null; 
function barsauto2() 
    { if(document.images) { 
    choose=document.doorglass.door2bars.options[document.doorglass.door2bars.selectedIndex].text.charAt(0) 
    document.barswitch2.src = "images/large_1800_bar"+choose+".gif" 
    } } 
    window.onerror = null; 
function barsauto3() 
    { if(document.images) { 
    choose=document.doorglass.door3bars.options[document.doorglass.door3bars.selectedIndex].text.charAt(0) 
    document.barswitch3.src = "images/large_1800_bar"+choose+".gif" 
    } } 
    window.onerror = null; 
function barsauto4() 
    { if(document.images) { 
    choose=document.doorglass.door4bars.options[document.doorglass.door4bars.selectedIndex].text.charAt(0) 
    document.barswitch4.src = "images/large_1800_bar"+choose+".gif" 
    } } 
    window.onerror = null; 

//--> 
</SCRIPT>
If anyone has any advice regarding this please let me know. It works as it is but i feel there is a shorter and simpler way around this

Thanks for looking
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 02-28-2006, 09:43 AM Re: getting php variables into js function
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
Running the code I posted in http://www.webmaster-talk.com/212008-post5.html, what HTML does it output?
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-28-2006, 10:20 AM Re: getting php variables into js function
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
It doesnt output any html, it just stops the image selector from working.

its these 2 bits of the code that stop it from working,
HTML Code:
     "images/"+codeofwidth+"_"+codeofheight+"_bar" + i + ".gif";
     "images/"+codeofwidth+"_"+codeofheight+"_bar"+choose+".gif";
if i replaced codeofwidth and codeofheight with actual values it seems to work:
HTML Code:
"images/2ds_1800_bar" + i + ".gif";
"images/2ds_1800_bar"+choose+".gif";
Il post up the answer if i ever find it
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 02-28-2006, 10:23 AM Re: getting php variables into js function
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
Did you look at the source of the generated HTML?
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-28-2006, 11:49 AM Re: getting php variables into js function
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Oh yeah id not tried viewing source
i just moved the <script></script> from the head section so that it appears after the $codeofwidth and $codeofheight variables are set. it now shows up in the source code as '2ds 1600 not $codeofwdith $codeofheight

Thanks for your help
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 02-28-2006, 12:26 PM Re: getting php variables into js function
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
Is that what you wanted?
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 03-01-2006, 08:30 AM Re: getting php variables into js function
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
It led me to the problem yes. Thanks
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to getting php variables into js function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47200 seconds with 12 queries