PDA

View Full Version : PHP/ JavaSript Problem



b.rich
April 28th, 2004, 12:03 AM
hey,

I am making a site in which everything is loaded from a funtions.php file... that way when i change one thing it changes it on the whole site...

anyway everything works fine.. i understand when using the echo feature you must change the "" to '' but when i put some javascript in there it doesnt work!!!

please help!


echo "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<HEAD>
<script language='JavaScript' type='text/JavaScript'>


<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf('?'))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<title>$title</title>
</head>

and i know i am missing the end tags but i didnt want to past the whole code in here... the javascript part is the only thing that doesnt work..

thanks-

ScriptFlipper
April 28th, 2004, 07:09 AM
try this:


<?php echo( ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf('?'))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<title><?php ); echo($title); echo( ?></title>
</head>
<?php ); ?>

b.rich
April 28th, 2004, 01:49 PM
well... i entered that and now i get nothing.... a blank screen... with the previous code everything else showed up. Just the javascript wouldn't work...now nothing works... :(

ScriptFlipper
April 28th, 2004, 02:09 PM
haha, I'm so stupid..
this will work, I'm certain:


<?php
// your code before the javascript here...
// then close your php-tag
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf('?'))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<title><?php echo($title); ?></title>
</head>
<?php
// your other code continues here...
?>

b.rich
April 28th, 2004, 03:18 PM
hmm... still not working... dont i have to put and echo tags before and after the javascript too? you have start and end tags for the code before it and after it but no tags around the javascript.... is that how it is supposed to be?

thanks agian...

ScriptFlipper
April 28th, 2004, 03:37 PM
could you attach your php file?

b.rich
April 28th, 2004, 03:51 PM
here. the code in question is about a 1/3 of the way down on the page.. everything else is workin great.

thanks

ScriptFlipper
April 28th, 2004, 05:04 PM
whoa, dude.. you should really start thinking about using the include() function and divide that document into pieces... I don't know where to start. But I can say this:

if you have a HTML tag that you need to print from your PHP code, you can do it like you have done:


<?php
$justAnExampleVariable1 = "blaha";
$justAnExampleVariable2 = "blahablaha";

echo "<td width='150'>$justAnExampleVariable1</td>";
echo "<td width='150'>$justAnExampleVariable2</td>";
?>


Or, the more convenient way:


<?php
$justAnExampleVariable1 = "blaha";
$justAnExampleVariable2 = "blahablaha";
?>
<td width="150"><?php echo($justAnExampleVariable1); ?></td>
<td width="150"><?php echo($justAnExampleVariable2); ?></td>



The last method makes it easier for you when you have those massive HTML sections, with lot's of " that needs to be replaced by ' .


This also applies to if/else functions:



<?php
if ($variable = 1) {
?>
<td width="150"><?php echo($justAnExampleVariable1); ?></td>
<?php
} else {
?>
<td width="150"><?php echo($justAnExampleVariable2); ?></td>
<?php
}
?>




You might wanna paste this code into dreamweaver or something, because the code coloring is off here (just because I added the <?php ?> tags)

b.rich
April 28th, 2004, 08:40 PM
k well that helps thanks... i appriciate it