Lisa23 Posted October 6, 2011 Share Posted October 6, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/248552-is-it-possible-to-put-a-php-variable-into-a-javascript-variable/ Share on other sites More sharing options...
Andy-H Posted October 6, 2011 Share Posted October 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248552-is-it-possible-to-put-a-php-variable-into-a-javascript-variable/#findComment-1276449 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.