PDA

View Full Version : useing php to change javascript var



armasmike
01-02-2008, 05:36 AM
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>');
}

codeexploiter
01-02-2008, 06:31 AM
<?
$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.

tech_support
01-02-2008, 10:56 AM
<?= $phpVar1?>
You're best off not using short-hand codes altogether. Some server configurations might not support it.


<!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:


<?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>