Results 1 to 3 of 3

Thread: useing php to change javascript var

  1. #1
    Join Date
    Apr 2007
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default useing php to change javascript var

    How can i make a php page that can change the var showimage_1 ?

    I am guess i have to store the var showimage_1 in a data base and have the js read that part of the database for a yes or no.

    How does not get started ?


    var showimage_1 = "yes"


    if (showimage_1 == "yes") {
    document.write('<br><TABLE cellpadding="0" cellspacing="0" border="0"><tr><td class="borders">');
    document.write('<IMG SRC="sidebar.jpg" border="0"><br>');
    document.write('</td></tr></table><br>');
    }

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    <?
    	$phpVar1 = "This is a test";
    	$phpVar2 = 1000;	
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript">
    	var jsVar1,jsVar2;
    	jsVar1 = "<?= $phpVar1?>";
    	alert(jsVar1);
    	
    	jsVar2 = "<?= $phpVar2?>";
    	alert(jsVar2);
    	
    </script>
    </head>
    
    <body>
    
    </body>
    </html>
    Check the above code for passing the php variable values into JavaScript. The technique can be used for your purpose.

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Code:
    <?= $phpVar1?>
    You're best off not using short-hand codes altogether. Some server configurations might not support it.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    Planning to drop IE support?

    Here's a better version:

    HTML Code:
    <?php $myVar = 'hi!'; ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    var myVar = '<?php echo $myVar; ?>';
    </script>
    </head>
    
    <body>
    </body>
    </html>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •