I have written a function where code related to the headers of a site are included. This is the function:
First of all, is there any problem with a coding such as the above-anything at all?
Secondly, Netbeans issues a warning in the html tag: Stray start html tag here.
I suppose this happens because the html tag is enclosed in a function and this functions does not contain the end tag-I assume.
function output_headers()
{?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Appointmetns24x7</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/admingeneral.css"/>
script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" ></script>
</head>
<body> <?php
}
If there is nothing wrong with this coding scheme then I will just ignore netbeans warning and continue.
The actual error message is due to the <body> tag not being closed. Netbeans is picking this up and warning you that the HTML may be invalid... because it is invalid. Netbeans has no way of knowing just by looking at this function that it shouldn't be a complete HTML document.
If you must do things this way, you should avoid splitting individual HTML tags between different code blocks. Best practice would be to make sure that any function that outputs an HTML tag also outputs the corresponding closing tag.
However the way you're doing things is not ideal in any case.
You've split your template into a 'header' and (presumably) a 'footer' function. This sort of technique was quite common years ago, but these days it's not considered particularly good practice.
A better technique would be to have a separate template file, which contains all your HTML -- ie the header and the footer, with placeholders where you want the dynamic content to go. You then build the dynamic content bits as strings, and feed them into the template.
In its simplest form, this just means that the template is a plain HTML file with PHP blocks, for placeholders like <?php echo $mainBodyCode; ?> in the appropriate places. You then just need to make sure the placeholder variables are populated, and include it when you want to output the page.
Hope that helps.
I would sat the stray tag is the
xmlns="http://www.w3.org/1999/xhtml"
because you called <!DOCTYPE html> which isn't xhtml, so calling the xml namespace is invalid.
It seems that if you enclose root type html(html tag) elements inside a function and then close them in a place on the script outside the function the beginning tag was set-then this interpreted as error in Netbeans.
Someone can just choose to ignore it, else he should avoid coding this way, putting html header info in a function.
Related
If we cannot use html code in php due to the php engine's inability to parse html code, then why can we include an open ended (no closing html tag) html script within php tags?
I've tried replacing the include call with the bare contents of the included file, but this triggers an unexpected end of file error (which makes sense, since php isn't able to parse the html).
register.php:
<?php
$page_title = 'Register';
// the following script echoes $page_title as title, links to stylesheet, and opens body
include ('includes/header.html');
header.html:
<!DOCTYPE HTML>
<html lang = "en">
<head>
<meta charset="UTF-8">
<title> <?php echo $page_title ; ?> </title>
<link rel = "stylesheet" href="includes/style.css">
</head>
<body>
<header> <h1>Page Header</h1></header>
I expected consistency between:
a) include('includes/header.html')
and
b) simply inserting the header.html code.
Error message from b) was standard for when you insert html code within php:
Parse error: syntax error, unexpected '<', expecting end of file in C:\Abyss Web Server\htdocs\register.php on line 6
include is not just some stupid preprocessor macro. It will not simply paste the contents of one file into another. It is a language construct, which will "move into" the other file, process it as it was a file of it's own, while maintaining the context of the parent file.
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags. - https://www.php.net/manual/en/function.include.php
This is also a reason, why you can catch parse errors in the outer file, if the inner file cannot be parsed properly.
Another important nuance is this:
It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.
This however does not apply to function definitions, which will be processed irregardless of any return statements.
The closing PHP tag is only needed if you want to exit PHP mode into HTML in the same file. PHP will exit the PHP mode automatically at the end of every file, even the included ones.
You need to "turn off" PHP when you want to simply output HTML otherwise PHP is going to try and treat it as PHP code and it will fail as you've seen. For the situation you describe the simplest answer is to simply leave PHP while the contents of what were in header.html are output.
<?php
$page_title = 'Register';
?>
<!DOCTYPE HTML>
<html lang = "en">
<head>
<meta charset="UTF-8">
<title> <?php echo $page_title ; ?> </title>
<link rel = "stylesheet" href="includes/style.css">
</head>
<body>
<header> <h1>Page Header</h1></header>
<?php # Turn PHP back on for whatever else is in register.php
I want to show a html email inside my html page
Thats is my simplified index.html
<!DOCTYPE html>
<html class="no-js" lang="">
<head></head>
<body>
<div id='email_goes_here'></div>
</body>
</html>
And that the beginning of my email.html
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
....
I want to put the code from email.html inside the div#email_goes_here from index.html.
Is it valid to just copy and paste the code in the div? I am not sure if its valid html to have 2 <!DOCTYPE html> and 2 <html> tags. If its not valid, how could I display the html page inside another html page? Any approach using PHP or jQuery would be fine.
I was looking for this problem, but I didnt find anything. I only found the question how to include some html content in anoter html file, but not how to include a complete html page. Here are the links that I found:
Include another HTML file in a HTML file
view html page inside another html
How do I load an HTML page in a <div> using JavaScript?
How to include an HTML page into another HTML page without frame/iframe?
Your simplest approach will be to use <iframe>.
If you use the srcdoc attribute, you will avoid having to set up any external html documents.
Working Example:
<iframe srcdoc="
<html>
<head>
<style>p{color: rgb(255,0,0);}</style>
</head>
<body>
<p>This is an example of an <code>iframe</code> which uses the <code>srcdoc</code> attribute.</p>
</body>
</html>
">
</iframe>
Further Reading: https://developer.mozilla.org/en/docs/Web/HTML/Element/iframe
Short answer: Use PHP.
There are many ways to include your pages inside others. Copy-paste won't be a good thing to do as, you guessed it, two html declarations can't be in the same page.
You could use an <iframe>, but looking at how browsers are starting to drop those, a more viable way is using PHP include or require. Of course, even with this method, two html declarations aren't allowed, so you'll have to "clean" your email.html file.
EDIT: Forgot to point it that iframes aren't allowed in mobile browsers, so you will lose those if you use iframes.
You can try to use iframe :
https://developer.mozilla.org/fr/docs/Web/HTML/Element/iframe
It permit to have one page inside another, but have some limitations.
Another way to do this more properly would be to use some javascript framework that are made to do reusable components (like Angular.js or React.js). And use your email page as a component.
It will be longer to learn though.
You can use HTML tag to embed another document within the current HTML document.
Read more: https://www.w3schools.com/TAGs/tag_iframe.asp
I found a Webpage saved as something.php. But the source code tells me <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I also found out that PHP code does not work on the webpage.
What is the need for making the file extension PHP if HTML is used?
(Not exactly HTML, but XHTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
What is the need for making the file extension PHP if HTML is used?
(Not exactly HTML, but XHTML)
Considering your comments so far, particularly you stated there is no PHP; you can just change the file extension to XHTML. You can always change it back.
I wonder what other PHP files exists where you "found" this page and why. Assuming someone before you developed the site, there is probably a reason they used PHP file extensions.
Unless your host doesn't support PHP, then you should be able to run php code anywhere on that page by placing it inside "" tags. The 'Content-type' isn't relevant to whether PHP can run or not. Try adding the following code somewhere in your page:
<?php echo "Hey there, I'm a friendly PHP string!"; ?>
add this <?php echo "Hello!"; ?> in your page to test, and make sure that your server is running, and normally it works
Are you using a wamp/mamp server? Have you tried to turn it on?
These code are meta tags
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and it has nothing to do with php unless you have included a php script to it.
Html/XHtml will run even though you are not using a php server. All php files has a .php file extension and will run only if you use a server like wamp for windows or mamp for mac.
You can still use html/xhtml code in a .php file.
For example I have an <h1>This is h1</h1> tag and you want to make it dynamic, you can put <?php ?> inside the tag and echo it out to display, <h1><?php echo "This is h1"; ?></h1>.
In case you want to put html code inside a php script, you can do it like this
<?php echo "<h1>This is h1</h1>"; ?>
You can learn more about php and other programming languages by the help of google. Just take your time, relax and enjoy learning. Don't pressure yourself, remember learning is not a medicine that when you take it, it will work in a few minutes. Learning takes time and practice. Enjoy coding.
I am wondering why php does certain things twice, instead of once, when a certain meta tag is in the html portion of the file and the file is browsed by Firefox.
The code is like this:
<? /*...normal php code, including writing record to MySQL...*/
send('dan#example.com',$subject,$body);
?>
<!DOCTYPE html><html>
<!--PROBLEM on next line-->
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<title><?= $thisPage?></title>
<link href="<?= $cssURL?>css.freedom-and-purpose.css" rel="stylesheet" type="text/css" media="screen, projection" />
<?
include $dataPath . 'data.php';
?>
The result is TWO records written the database and TWO emails sent, whenever the page is called by FIREFOX. IE and Chrome not producing the problem.
There is a lot of other code in the program, but the reason I showed the portion above is that removing the line that starts with
<META...
solves the problem.
That meta tag is in there because one of the packages I run included it in their code sample.
So, what is that meta tag causes php to double do on DB-writes? And same thing on sending email?
Chances are this is actually a request for favicon.ico being caught by your main PHP file. Putting an empty file in favicon.ico or preventing your PHP from handling that URL should do the trick
enter code hereI would suggest you go through your code in some details and check its formatting.
Phil mentioned the meta tag about which I agree with. His suggestion of <meta charset="utf-8"> would be my preference.
Secondly the line sending the email looks odd. Single quotes aren't an option in php for data replacement, so the line send('dan#example.com','$subject','$body); would result in an email with the subject "$subject" and body just "$body".
Additionally send('dan#example.com','$subject','$body); appears to be missing a quote after $body.
I would advise you to move away from short php tags for opening and closing chunks of php <? ?> and get in the habit of <?php for clarity and to ensure the server you're using processes the code correctly.
Finally, I hope include $dataPath . 'data.php'; adds a </head> and a <body> to the html, as you're currently missing those too.
I don't know why every time I try to include my header using PHP’s include there's a top margin. I checked it using Firebug and it says there's a 22px offset margin on the top. Is anybody experiencing this problem? I think it's a CSS propiety: top: 22px. But nothing can change it even if I write h1 style="top: 0px; margin-top: 0px;". I think it's a php-CSS mystery that will never be solved.
edit: The only way to get rid of that top margin offset or whatever it is, is to add the follow properties to the H1: top: 0px;
position: absolute;
Will those properties generate more problems in the future?
is there a better way to solve this top margin-offset problem?
edit2: I think there's a problem with the encoding. Is there a conflict between the encoding of the included file (header.html) and the index file?
My index goes like this:
<!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>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<div id="page-wrap">
<?php include_once("header2.html"); ?>
</div>
</body>
</html>
With this CSS:
* {
padding: 0px;
margin: 0px;
}
My header.html (the one that’s being included):
<h1>Header 2</h1>
And that’s the output:
<!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>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<div id="page-wrap">
<h1>Header 2</h1> </div>
</body>
</html>
God its so simple that I really dont know where the top margin is coming from (in all browsers).
It only happens when I use php includes.
The source code looks the same as when I dont use php include.
This is pretty weird, but I copied & pasted your output HTML into Notepad++, and there was a strange character just prior to the h1. When pasting it into Notepad, the h in the h1 was subscripted.
Therefore, it looks to me like you may have an erroneous (or unexpected for PHP) character in your included HTML (or in the PHP including it). My suspicion? The dreaded UTF-8 BOM. Try to change the encoding of your included HTML file to eliminate the BOM (which I've always had problems with when dealing with PHP).
Edit: Yep. Just verified with a hex editor. There's a byte-order mark at the beginning of your included HTML. I don't know which editor you use, but you should have the option to change your text encoding settings.
See this for more info about PHP & the BOM.
You ought to post your generated HTML, like by copying what you see when you select "View Source" from your web browser. Oftentimes, I've seen mysterious blank lines in PHP-generated HTML because PHP is configured to output errors and warnings. Even if this is not caused by that, we can more-easily diagnose the problem if we see the outputted HTML.
I experienced the same problem...so what I did to fix it was to add an html opening comment at the beggining of the first file and an html closing comment at the beginning of the second file. This encloses the BOM (or whatever is appearing in between the files) inside a comment so it's not outputted in html.
For example:
#header.php
<?php ?>
<html>
<!--
#body.php
<?php ?>
-->
<body>
...
I tried to fix the problem converting between encoding formats in notepad++ to no avail. So this is only a temporary fix til I find a better solution to the problem.
well, without knowing what's in your php include, it's hard to tell, but make sure there's no CSS inside of it. also make sure that CSS you're loading is getting found and loaded. i debug by changing the background color or something else visual so u can be sure it's loading.
i'd bet anything that it has nothing to do with the php include it's self. like previously mentioned, there may be some css in the included file that is doing it, but i've never seen php anything like what you are describing.
i would look at the doctype declaration [temporarily remove it to see what happens], and remove the current html declaration tag and replace it with a plain <html> tag [again just temporarily, to debug]
A lot of times an extra new line will be at the end of a PHP script file you are including after the ?> tag. This will cause an extra character to be included in the generated output. You can remedy this problem by not having a closing ?> tag at the very end of your file. PHP knows to "assume" that it's closed.
Do you have the option to give us a link? I just tried locally a couple of things and you may get this "unwanted" new line for apparently "no reason" at all between your content and the "doctype" declaration, if there is a nonprintable character. Try to delete all characters after the ">" and take a look at it with a hexeditor to make certain of it. Please provide us more info.
Thats very interesting, because I just used your markup and your css. I also put it into an php file and included a html file. But there is no margin. Nowhere.
So make sure your browser is able to find the css file with
* { margin: 0; padding: 0 }
And I suggest you to use a reset sheet like this one - http://meyerweb.com/eric/tools/css/reset/index.html.
p.s. if you own some webspace, put it online and send us a link.
Dreamweaver drives me nuts with it's automatic BOM additions. Here's the fix:
http://www.adobe.com/support/documentation/en/dreamweaver/mx2004/dwusing_errata/dwusing_errata2.html
I am using notepad and I selected from the menu -> Encoding->Encoding with UTF-8 without BOM and it worked just fine. This should be done for every included file. I think this is the solution to your problem. Have a nice evening.
I almost killed my keyobard trying to solve to problem.
Then I tried PSPad editor, change explicitly .inc or .php to UTF-8 and IT WORKED !!
I had one time this problem and here is the way to fix it - its really problem in charset.I have spent 10 hours to find it. In my situation I change at all php files(all php files, all tpl(if you have) files) charset from UTF-8 to UTF-8 without bom and save it and try then. After that you can also make at .htaccess string "AddDefaultCharset UTF-8".