Need help with columns in HTML/PHP

Discussion in 'Web Design and Development' started by tnsmart, Aug 8, 2009.

  1. tnsmart macrumors 6502

    Joined:
    Aug 23, 2008
    #1
    I'm trying to create two columns in HTML, inside of a PHP file.

    This code runs fine by itself:
    Code:
    <center>
    <table border="0" width="60%" cellpadding="0">
    <tr>
    
    <td width="50%" valign="top"><center>
    This is some text. </td>
    
    <td width="50%" valign="top"><center>
    More text. 
    </td>
    
    </tr>
    </table>
    
    But when I add it to the PHP file like this:

    Code:
    <?php
    
    echo "<html>
      <head>
        <title>Columns</title>
      </head>
      <center>
    <table border="0" width="60%" cellpadding="10">
    <tr>
    <td width="50%" valign="top"><center>
    This is some text. </td>
    <td width="50%" valign="top"><center>
    Another text. 
    </td>
    </tr>
    </table>
      
    </html>";
    
    ?>
    I get: Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in /- on line 8

    Can anyone help me to get the columns to work correctly? Thank you.
     
  2. ergdegdeg Moderator emeritus

    ergdegdeg

    Joined:
    Oct 13, 2007
    #2
    You need to change the PHP code to:
    PHP:
    <?php

    echo '<html>
      <head>
        <title>Columns</title>
      </head>
      <center>
    <table border="0" width="60%" cellpadding="10">
    <tr>
    <td width="50%" valign="top"><center>
    This is some text. </td>
    <td width="50%" valign="top"><center>
    Another text. 
    </td>
    </tr>
    </table>
      
    </html>'
    ;

    ?>
    You can't use double quotes for PHP and HTML.
     
  3. tnsmart thread starter macrumors 6502

    Joined:
    Aug 23, 2008
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    An alternative when doing multiline HTML from PHP is using heredoc syntax,

    PHP:
    echo <<<EOF
    <html>
      <head>
        <title>Columns</title>
      </head>
      <center>
    <table border="0" width="60%" cellpadding="10">
    <tr>
    <td width="50%" valign="top"><center>
    This is some text. </td>
    <td width="50%" valign="top"><center>
    Another text. 
    </td>
    </tr>
    </table>
      
    </html>
    EOF;
     
  5. tnsmart thread starter macrumors 6502

    Joined:
    Aug 23, 2008
    #5
    ergdegdeg's suggestion worked for the code in my first post, but that wasn't the whole thing. This is the whole thing, with the suggested corrections:
    Code:
    <?php
    
    echo '<html>
      <head>
        <script src='verify.js'></script>
        <title>CASA Login</title>
        <link rel='stylesheet' type='text/css' href='/ref/main.css' />
        <link rel='stylesheet' type='text/css' href='/ref/maincolor.css' />
      </head>
      <center>
    <table border="0" width="60%" cellpadding="10">
    <tr>
    
    <td width="50%" valign="top"><center>
    This is some text. </td>
    
    <td width="50%" valign="top"><center>
    Another text. 
    </td>
    </tr>
    </table>
    </html>';
    
    ?>
    Unfortunately, I now get:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /- on line 5

    Do you know how I can get it all to work together?
     
  6. ergdegdeg Moderator emeritus

    ergdegdeg

    Joined:
    Oct 13, 2007
    #6
    Just always use double quotes inside the HTML part and single quotes around that (at the beginning and end of the string).
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Give my technique a try in post 4, it's more forgiving of using different quote types.

    Though, since you're outputting pure HTML, just close the PHP tag and simply have your HTML.
     
  8. trule macrumors 6502

    Joined:
    Mar 16, 2007
    #8

    Why are you doing it this way??? Just do this with PHP tags only where you need them...

    somephpfile.php
    <html>
    ...
    <?php echo 'hello world'; ?>
    ...
    </html>

    In fact, just take your original file and change the extension to .php and it will work fine
     
  9. Joined:
    Jun 30, 2009
    #9
    Code:
    <?php
    
    echo '<html>
      <head>
        <script src="verify.js"></script>
        <title>CASA Login</title>
        <link rel="stylesheet" type="text/css" href="/ref/main.css" />
        <link rel="stylesheet" type="text/css" href="/ref/maincolor.css" />
      </head>
      <center>
    <table border="0" width="60%" cellpadding="10">
    <tr>
    
    <td width="50%" valign="top"><center>
    This is some text. </td>
    
    <td width="50%" valign="top"><center>
    Another text. 
    </td>
    </tr>
    </table>
    </html>';
    
    ?>
    You've got to apply what he said to the entire document. I corrected your code above.
     
  10. tnsmart thread starter macrumors 6502

    Joined:
    Aug 23, 2008
    #10
    I have to say I'm becoming a bit confused by all of this. I just started teaching myself these web languages about a week ago. Anyways, what I am doing, is editing someone else's site.

    It displays exactly how I want it to with this:
    Code:
    <?php
    
    echo '<html>
      <head>
        <scripst src="verify.js"></script>
        <title>Title</title>
        <link rel="stylesheet" type="text/css" href="/ref/main.css" />
        <link rel="stylesheet" type="text/css" href="/ref/maincolor.css" />
      </head>
     
    <center>
    
    <img src="/ref/Logo.jpg" class="logo" />
    
    <table border="0" width="400px" cellpadding="0">
    
    <td><align=left>
    	<div id="left">
    		<label>User Name</label>
    		<br><input type="text" size="12" id="name"/>
    	</div>
    	</td>
    
    <td><align=right>
    	<div id="left">
    		<label>Password</label>
    		<br><input type="text" size="12" id="pass"/>
    	</div>
    	</td>
    
    </table>
    
          <input type="button" value="Login" onclick="verify()" id="submit" />
          <span id="message"></span>
      </body>
    </html>';
    
    ?>
    However, the button does not do anything and the password input is not dots like it should be.

    But the button and password input works fine with the old code:
    Code:
    <?php
    
    echo "<html>
      <head>
        <script src='verify.js'></script>
        <title>Title</title>
        <link rel='stylesheet' type='text/css' href='/ref/main.css' />
        <link rel='stylesheet' type='text/css' href='/ref/maincolor.css' />
      </head>
      <body class='center'>
        <img src='/ref/Logo.jpg' class='logo' />
        <div id='form'>
          <div id='left'>
            <label for='name'>User Name</label> 
            <input type='text' size='20' id='name'/>
          </div>
          <div id='right'>
            <label for='pass'>Password</label>
            <input type='password' size='20' id='pass'/>
          </div>
          <input type='button' value='Login' onclick='verify()' id='submit' />
          <span id='message'></span>
        </div>
      </body>
    </html>";
    
    ?>
    
    How can I get it to display, like the first code, but the button and password input (dots instead of text) to work like the second code? Thanks for all the help.
     
  11. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #11
    For passwords, you have to use the password type,
    HTML:
    <input type="password" name="pass" id="pass" size="12" />
    As for clicking the button not doing anything, that could be related to a JavaScript issue. We would need to see the contents of the JavaScript file in order to debug that.

    Also,
    HTML:
    <align=left>
    isn't valid HTML. There's actually various issue with your code. Missing the for attribute on your labels. Missing tr tags in your table, etc.
     
  12. tnsmart thread starter macrumors 6502

    Joined:
    Aug 23, 2008
    #12
    Fixed the password. Thank you for that.
    Took out align=left and right, guess I don't need them even if the did work. As for the rest of the issues, hopefully I'll sort them out eventually.

    Here's the verify.js page. This works just fine with the old code, in my last post, so I'm guessing it's a problem with my new code, but there's a good chance I'm wrong.
    Code:
    var xmlHttp;
    var username;
    var username2;
    var named;
    var passed;
    
    function userretry()
      {
      var username=prompt("Username already exists. Please select a different username","");
      if (username==null || username=="" || username==" ")
        {
        document.getElementById('message').innerHTML="Username invalid!";
        }
      else
        {
        var url="verify.php";
        url=url + "?nam=" + named;
        url=url + "&user=" + username;
        xmlHttp.onreadystatechange=stateuserChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
        }
      }
    
    function user()
      {
      var username=prompt("Please enter your name","");
      if (username==null || username=="" || username==" ")
        {
        document.getElementById('message').innerHTML="Username invalid!";
        }
      else
        {
        return username;
        }
      }
    
    function verify()
      {
      var nam=document.getElementById('name').value;
      var pass=document.getElementById('pass').value;
      if ((nam=="ninja" && pass=="ninja") || (nam=="mctech" && pass=="mission") || (nam=="station" && pass=="station") || (nam=="master" && pass=="control"))
        {
        named=nam;
        passed=pass;
        username=user();
        if (username!=null)
          {
          xmlHttp=GetXmlHttpObject();
          if (xmlHttp==null)
            {
            alert ("Browser does not support HTTP Request");
            return;
            } 
          var url="verify.php";
          url=url + "?nam=" + nam;
          url=url + "&user=" + username;
          xmlHttp.onreadystatechange=stateuserChanged;
          xmlHttp.open("GET",url,true);
          xmlHttp.send(null);
          } 
        }
      else
        {
        document.getElementById('message').innerHTML="Username or password incorrect!";
        }
      }
    
    function stateuserChanged()
      { 
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        { 
        if (xmlHttp.responseText=="Back")
          {
          userretry();
          }
        else if (named=="ninja" && xmlHttp.responseText=="1")
          {
          window.location="/ninja/";
          }
        else if (named=="mctech" && xmlHttp.responseText=="1")
          {
          window.location="/mission/";
          }
        else if (named=="station" && xmlHttp.responseText=="1")
          {
          window.location="/station/";
          }
        else if (named=="master" && xmlHttp.responseText=="1")
          {
          window.location="/master/";
          }
        else
          {
          document.getElementById('message').innerHTML="An error was detected: <br/>" + xmlHttp.responseText;
          }
        } 
      }
    
    
    function GetXmlHttpObject()
      {
      var xmlHttp=null;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        }
      return xmlHttp;
      }
     
  13. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #13
    Looks like a typo in your HTML
    Code:
    <scrip[B][COLOR="Red"]s[/COLOR][/B]t src="verify.js"></script>
     
  14. tnsmart thread starter macrumors 6502

    Joined:
    Aug 23, 2008
    #14
    Thank you for finding that. The page works great now. Thanks for all the help.
     
  15. dlewis23 macrumors 6502a

    Joined:
    Oct 23, 2007
    #15
    Why are things like this inside the php code it self?

    PHP:
    <html>
      <
    head>
        <
    script src='verify.js'></script>
        <title>Title</title>
        <link rel='stylesheet' type='text/css' href='/ref/main.css' />
        <link rel='stylesheet' type='text/css' href='/ref/maincolor.css' />
      </head>
     
  16. Joined:
    Jun 30, 2009
    #16
    Because that's the way most of the books start teaching PHP: echo everywhere. Then, the topic gets discussed a little bit more, and you also get your foot wet with inline PHP...
     

Share This Page