Jump to content

is it possible to put a php variable into a javascript variable


Lisa23

Recommended Posts

Hi i have this scroolbar javascript where you implement the text inside the java variable but because i want the data to come from the database i wanted to use a php example  <? print($marker); ?> so i was wondring if it is possible to add a php variable into a javascript like this my javascript

 

at moment the javascript variable is like this

var myMainMessage="  Try this out! The message just keeps repeating. Use it for announcements, news and other items! ";

i want to change to this

var myMainMessage="<? print($marker); ?>";

is it possible if so can someone show me an example thanks

 

the whole javascript

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- (c) http://www.wyka-warzecha.com -->

<!-- Begin
// THESE VARIABLES where i want to add the php //
var myMainMessage="  Try this out! The message just keeps repeating. Use it for announcements, news and other items! ";

var speed=150;
var scrollingRegion=50;

// END CHANGEABLE VARIABLES //
var startPosition=0;
function mainTextScroller() {
        var mainMessage=myMainMessage;
        var tempLoc=(scrollingRegion*3/mainMessage.length)+1;
        if (tempLoc<1) {tempLoc=1}
        var counter;
        for(counter=0;counter<=tempLoc;counter++)
           mainMessage+=mainMessage;
document.mainForm.mainTextScroller.value=mainMessage.substring(startPosition,startPosition+scrollingRegion);
        startPosition++;
        if(startPosition>scrollingRegion) startPosition=0;
        setTimeout("mainTextScroller()",speed); }
//  End -->
</script>

What you have should work, providing your server is configured with short tags enabled, although short tags is considered to be bad practice. Also, don't quote me on this; but I read somewhere that echo is faster than print because print outputs an error on failure (which never happens).

 

 

If you insist on using short tags the short tag to echo a variable is

<?= $var; ?>

 

 

Try

 

 

<?php echo $var; ?>

 

 

That will work if your problem is short tags being disabled.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.