Trying but failing - PHP if statement

Discuss all aspects of programming here.

Moderator: The Mod Squad

Trying but failing - PHP if statement

Postby Toby B. » Sat Mar 17, 2007 12:23 pm

I am trying to get some code inserted to a page header file that when the $page_name == Support & Drivers a block of code gets included into the page otherwise it is not included. But all attempts have failed...

This is the code:
Code: Select all
<script type="text/javascript">
<!-- Begin
function formHandler1(form1){
var URL = document.form1.site.options[document.form1.site.selectedIndex].value;
window.location.href = URL;
}
// End -->
</script>

<script type="text/javascript">
<!-- Begin
function formHandler2(form2){
var URL = document.form2.site.options[document.form2.site.selectedIndex].value;
window.location.href = URL;
}
// End -->
</script>


Thanks
Toby B.
Lead Mobo-fu Master
Lead Mobo-fu Master
 
Posts: 14277
Joined: Sun Dec 16, 2001 1:01 am
Location: Maine

Postby olly » Sun Mar 18, 2007 4:17 am

Are you using SSI or something? I've had problems with javascript when I've had seperate javascript code in a header file and a body file. Maybe try to get it working in one html file then try splitting it up with SSI?

BTW where is the PHP if statement you refer to? Looks like javascript to me in that code example.
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Postby Toby B. » Sun Mar 18, 2007 5:24 am

Thanks olly. No SSI, or anything of that nature, just straight forward php/HTML/etc...

I was able to get it working (not at all what I was hoping for but it works). What I did was simply add another include to the page.

The problem (that I am fussing over now), is even though the javascript is now showing on the page and works, its not where I want it. I want it to show in the <head></head> tags not in the <body></body> tags. Currently dont have the page publically availible, but will try to get it uploaded soon...

As far as where the if statement is, I did not post that as I have gone through at least 5 attempts of trying different things out and only the extra include works....

Broken Code is:
Code: Select all
<!doctype html public "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3c.org/TR/html4/strict.dtd">
<html>
   <head>
      <title>Consumer Technical Information - Support & Drivers</title>
   <meta name="description" content="Your number one resource for Compaq & HP Support. With the largest database of Compaq/HP info availible anywhere." />
        <meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1"/>
        <meta name="keywords" content="Compaq, Compaq PC's, compaq computers, Compaq technical support, computer hardware, compaq information, Compaq motherboards, Aspen, Aspen2, BMW/BMW-2, BMW-R, CAMARO, CE-31A, KBI-6130, KBA-6130, Mitac, FIC, Trigem, Asus, UIB1, ZZ-Top, MSI, MS-6553, Intel, BI440ZX, TR100, LORRETTO, Uwave, Uwave2, Uwave3, Uwave-R, AZ31, Pipeline, ELISE/ELISE-SWAP, Compaq Maintanence/Sevice Guides, motherboards, mainboards, mobos, HP, Presario, Pavilion, Deskpro, BRIO, Vectra, PC's, Maintainence/Service Guides, Manuals, BIOS files, drivers, motherboard identification" />
   <meta NAME="author" content="Toby Bibeau">
   <meta NAME="robots" content="all">
      <link rel="stylesheet" href="/common/compaq_style.css" type="text/css">
<?php if ($page_name == Support & Drivers) {
echo '<script type="text/javascript">
<!-- Begin
function formHandler1(form1){
var URL = document.form1.site.options[document.form1.site.selectedIndex].value;
window.location.href = URL;
}
// End -->
</script>

<script type="text/javascript">
<!-- Begin
function formHandler2(form2){
var URL = document.form2.site.options[document.form2.site.selectedIndex].value;
window.location.href = URL;
}
// End -->
</script>'};
?>
</head>
Toby B.
Lead Mobo-fu Master
Lead Mobo-fu Master
 
Posts: 14277
Joined: Sun Dec 16, 2001 1:01 am
Location: Maine

Postby olly » Sun Mar 18, 2007 6:07 am

Hmm never seen php embedded in the <head> section before interesting. I see no reason why it shouldn't work though.

Have you tried putting everything in betweeen the echo single quotes ('') on one line? Its not very pretty to read but I've had to do this before to get something to work.

Could be that Support & Drivers has spaces and an & sign.

Maybe supportAndDrivers may be a better naming convention?

Also you may need to use escape characters for some of the javascript being printed.
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Postby Toby B. » Sun Mar 18, 2007 4:46 pm

olly wrote:Hmm never seen php embedded in the <head> section before interesting. I see no reason why it shouldn't work though.

Have you tried putting everything in betweeen the echo single quotes ('') on one line? Its not very pretty to read but I've had to do this before to get something to work.

Could be that Support & Drivers has spaces and an & sign.

Maybe supportAndDrivers may be a better naming convention?

Also you may need to use escape characters for some of the javascript being printed.
I've tried encasing it in single quotes, double quotes, tried replacing echo with print (which sounds retarted as they do the same thing). As far as the seperated "support & drivers" I dont believe that would cause a problem, but then agian I am having a prolem somehere though... :o
Toby B.
Lead Mobo-fu Master
Lead Mobo-fu Master
 
Posts: 14277
Joined: Sun Dec 16, 2001 1:01 am
Location: Maine

Postby olly » Mon Mar 19, 2007 11:19 am

What kind of an error are you getting Toby, is there gibberish text being displayed, or a blank page etc? Is $page_name being posted to this page?

IIRC I normally receive posted variables to a page like so:

$filename=$_POST["url"];
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Postby Toby B. » Mon Mar 19, 2007 11:30 am

Different parse errors are getting outputted but the remainder of the page does get outputted as normal...

Is $page_name being posted to this page?
Yes and it works as it should...
Toby B.
Lead Mobo-fu Master
Lead Mobo-fu Master
 
Posts: 14277
Joined: Sun Dec 16, 2001 1:01 am
Location: Maine

Postby olly » Mon Mar 19, 2007 11:41 am

Did you try using escape characters for the javascript being printed e.g:

echo '<script type=\"text
olly
Green Belt
Green Belt
 
Posts: 222
Joined: Thu Jun 27, 2002 8:11 pm

Postby Toby B. » Mon Mar 19, 2007 4:57 pm

olly wrote:Did you try using escape characters for the javascript being printed e.g:

echo '<script type="text
hmm no I had not thought of that. will try it whem I get home in the morning...
Toby B.
Lead Mobo-fu Master
Lead Mobo-fu Master
 
Posts: 14277
Joined: Sun Dec 16, 2001 1:01 am
Location: Maine

Postby Toby B. » Tue Mar 20, 2007 2:50 am

olly wrote:Did you try using escape characters for the javascript being printed e.g:

echo '<script type="text


Results:
Parse error: parse error, unexpected '}', expecting ',' or ';' in D:\www1\ctechinfo\includes\header.inc on line 29
Toby B.
Lead Mobo-fu Master
Lead Mobo-fu Master
 
Posts: 14277
Joined: Sun Dec 16, 2001 1:01 am
Location: Maine

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron