addiosamigo
addiosamigo

Reputation: 23

divs in PHP, not working

I'm trying to use my css in my php script, this is what I tried:

    ....
    else 
    {
     echo"<html>
    <head>
    <link href='styles.css' rel='stylesheet' type='text/css' />
    </head>
    <body>
    <div id="main">
    <div id="header">
    </div>
    <div id="contentWrapper">
    <div id="leftBox">
    <div id="leftBoxContent">
    <div id="lightBox">
    </div>
    </div>
    </div>
    <div id="rightBox">
</div>
    <div id="content">
    <p>Wrong Username or Bad Password.</p>
    <p><a href='./index.php'>Click Here</a> to try again.</p>
    </div>
   <br class="clearFloat" />
   </div>
   <div id="footer"></div>
   </div>
   </body>
   </html>";
    }
   ?>

its my else statement of my login script, when I take away the divs the html shows fine, but leaving them in causes an error. what am I doing wrong? do I put my css on the page??

thanks

Upvotes: 0

Views: 2056

Answers (12)

Gaurav
Gaurav

Reputation: 28755

else 
    {
     echo '<html>
           <head>
             <link href="styles.css" rel="stylesheet" type="text/css" />
           </head>
           <body>
            <div id="main">   
                 ....
          </html>';
    }
   ?>

But I would suggest the answer of @Col. Shrapnel.

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157828

It's PHP, dude. No need to use echo at all

<?
else 
{
?>
<html>
  <head>
    <link href='styles.css' rel='stylesheet' type='text/css' />
  </head>
  <body>
    <div id="main">
      <div id="header"></div>
      <div id="contentWrapper">
        <div id="leftBox">
          <div id="leftBoxContent">
            <div id="lightBox"></div>
          </div>
        </div>
        <div id="rightBox"></div>
        <div id="content">
          <p>Wrong Username or Bad Password.</p>
          <p><a href='./index.php'>Click Here</a> to try again.</p>
        </div>
        <br class="clearFloat" />
      </div>
      <div id="footer"></div>
    </div>
  </body>
</html>
<? } ?>

Upvotes: 3

philonous
philonous

Reputation: 4061

Besides using another type of quotation marks, escaping and so on, I would highly recommend to separate your concerns: Why not using a html-file (or a template)?

Upvotes: 1

mario
mario

Reputation: 145482

You already got all the good answers. But for completeness: Another option is to keep lengthy HTML snippets in separate files. Then just do this instead of the echo:

else
{
  include("template/head+div+block.html");
}

If you have lots of html output like this, you will thank yourself later for making it manageable.

Upvotes: 1

chriso
chriso

Reputation: 2552

The syntax highlighting in your question gives it away, there's a conflict with the double quotes. The solution? echo with single quotes!

echo '<html>
    <head>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <div id="main">
    <div id="header">
    </div>
    <div id="contentWrapper">
    <div id="leftBox">
    <div id="leftBoxContent">
    <div id="lightBox">
    </div>
    </div>
    </div>
    <div id="rightBox">
</div>
    <div id="content">
    <p>Wrong Username or Bad Password.</p>
    <p><a href="./index.php">Click Here</a> to try again.</p>
    </div>
   <br class="clearFloat" />
   </div>
   <div id="footer"></div>
   </div>
   </body>
   </html>';

You could also use the heredoc syntax:

echo <<<HTMLBLOCK
<a href="#">Double quotes</a>
<a href='#'>..and single quotes!</a>
HTMLBLOCK;

Upvotes: 1

Emmerman
Emmerman

Reputation: 2343

Old fun method

echo <<< OUT
<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
...
OUT;

everyone forgot it, right? :)

Upvotes: 1

Gary Green
Gary Green

Reputation: 22395

It's bad practise to do these kinds of echos full of HTML (let's let it slip that even the below isn't MVC--that's another topic entirely). You should come out of PHP and then echo your HTML like so:

<?php function echo_menu() { ?>
  <div id="menu">
    <a href="/home">Home</a>
  </div>
<?php }

Upvotes: 2

vbence
vbence

Reputation: 20333

Use quotation marks consistently. Now you use single quotes with link but double quotes with all the other tags. Change your link tag to

<link href="styles.css" rel="stylesheet" type="text/css" />

Then you can use single quotes around the whole string like:

....
else 
{
 echo '<html>
<head>
...
</body>
</html>';
}

Upvotes: 1

oezi
oezi

Reputation: 51797

yau'll have to correct your quotation-signs as you cant use " insite of "" without escaping it. do one of these:

echo "this is a \"test\" ... ";
echo "this is a 'test' ... ";
echo 'this is a "test" ... ';
echo 'this is a \'test\' ... ';

Upvotes: 3

Arc
Arc

Reputation: 11286

You are having quotes in there that end your echoed string.

Either escape them with a backslash, or use one single quotes to quote the string and double quotes in it, or vice versa.

However, if you do not use any PHP variables or code in the block, you could also enter non-PHP mode by using ?> before the HTML and reenter PHP mode afterwards with <?php.
This has the benefit that many editors can syntax-highlight both PHP and HTML properly.

Upvotes: 0

Headshota
Headshota

Reputation: 21449

escape double quotes in div attributes

<div id=\"main\">

like this

Upvotes: 0

anaconda
anaconda

Reputation: 1092

<div id="yourid" ..>

change to

<div id=\"yourid\" ..>

Upvotes: 1

Related Questions