I am a noob to PHP just wondering why are we writing the php script in the body tag of HTML.
I am just taking a word on one page and directing it to another page using the post method. below is the code.
page1.html
<form action="disp.php" method="post">
Enter a word: < input type="text" name="word" />
disp.php
this goes inside the body tag
$word=$_POST['word'];
echo $word;
All your help is highly appreciated.
One way to think of it is that the server dynamically creates an HTML page based on the result of your PHP script. For it to be valid html, you need to write the appropriate tags such as
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body>
...
</body>
</html>
The script can actually go anywhere, but the echo statement is like saying "write something here." If you had it echo something between <title></title> tags, then the result of your PHP statement, in this case $word would appear in the page title. You want it to appear in the body of the page, so you have it echo the variable between <body></body> tags.
Does that answer your question?
Related
I have a signin.php file that can be used in multiple web pages where users can sign in via the pop up window. This file includes
<?php
//some code to process submitted sign up form
?>
<div>
// some html code for the sign in pop up window
</div>
I want to include signin.php in other html page files. Do I need to wrap the html code above with !DOCTYPE html, html, head, or body tag? I tested it seems it works without these tags.
Thanks.
No, if your going to just include the tags from another file then you don't need to put the <html> tag because if your code is
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<?php include("hello.php")?>
</div>
</body>
</html>
and in hello.php you just have an h1 tag with hello
it will get rendered as
<div>
<h1>Hello</h1>
</div>
I'm trying to make a simple php script that puts a list in the header, here is what I have
<html>
<head>
<?php
function menu1($link1, $button1) {
echo "<ul><li><a href = '$link1'>$button1</a></li></ul>";}
menu1('index.php','Home')
?>
</head>
<body>
<p>Home</p>
</body>
</html>
Yet for some reason when I run the script it outputs this
<html>
<head>
</head>
<body>
<ul><li>Home</li></ul>
<p>Home</p>
</body>
</html>
Is there any way I can get this to post in the head?
Thanks in advance!
The <head> element can only contain certain special elements, such as <title>, <link>, and <meta>, among others. It cannot contain any elements which are rendered on the page.
If you view source, you'll see that the markup is being output the way you requested. However, this markup is invalid, and is being "repaired" by the browser by moving these elements into the <body>.
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 have a php file with multiple forms.. for simplicity's sake I have created multiple html file and each of them has a form and starts from <html> and ends with </html>.
the php file is something like this:
<?php
include('f1.html');
include('f2.html');
...
?>
The result of this seems okay.. my question is, it is okay if I keep it like this or I have to create head.html and end.html then includes forms in between??
what is the differences??
Any help is appreciated
Thanks
Yes, you can include as many as you want. But each html file should not contain <html /> enclosed. You should put it in your main file and the file which you want to include - in that file just use div wrapper to include your content. I mean you can use any html tag that you use the tag inside body tag.
But as per google page speed guide, you should maintain least html size as far as possible.
Here's an example:
main.php:
<!DOCTYPE html>
<html>
<head>
<title>Example page</title>
</head>
<body>
<?php
include('f1.html');
include('f2.html');
?>
</body>
</html>
f1.html and f2.html:
<div>
your content
</div>
Here is the structure of the web site:
PHP index file
//my class for analyzing the PHP query
$parameter = new LoadParameters();
//what this does is it accesses the database
//and according to the query, figures out what should be
//loaded on the page
//some of the things it sets are:
// $parameter->design - PHP file which contains the design
// HTML code of the page
// $parameter->content - Different PHP file which should be loaded
// inside the design file
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']);
//load the design file
include($parameter->design);
PHP design file
Just the generic structure. Obviously it has a lot more design elements.
<html>
<head>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Question
So here is the problem I experience. The $parameter->content file is a dynamic PHP file, meaning the content also changes according to the query.
For instance if I have a image pages with queries like ?img=1 and ?img=2, my LoadParameter class will only look at the img part of the query and will know that the content of the page should be image.php. image.php however will look at the query again and figure out exactly what image to load.
This causes issues for me because I want to have a different <title></title> for different images. So my solution was just to set the <title></title> element in the content page. This works but it breaks the XHTML markup validation at W3C because it makes the structure of the site to be the following:
<html>
<head>
...
</head>
<body>
...
<title>sometitle</title>
...
</body>
</html>
And having <title></title> within <body></body> is not allowed.
So how can I change the title without breaking the XHTML markup validation?
Note: I can't use javascript because then Search engines would not be able to see the title of the page. I need to do it directly in PHP.
Thanx in advance.
why not do a second include to perform the title in the proper place?
<html>
<head>
<?php
inlcude($parameter->title);
?>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Can't you just change the PHP code so that you can do something like:
<html>
<head>
<title><? print($parameter->title); ?></title>
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
I'd move all of the <head> code into a 'common function' called something like html_head($title) and then have it put the title where it belongs.
Then simply call that function from within the pages and it's fixed.
Don't forget to include the <body> tag in that function, otherwise it won't work!
Elaborating ;)
function html_head($title) {?>
<html>
<head>
<title><?=$title?></title>
<!-- Put whatever you want... here! -->
</head>
<body>
<?}
Then in $parameter->content, call html_head("Title")
It would be easier if $parameter->content could be included without displaying its HTML code, but instead have a $parameter->display (or similar) function that displays the HTML code. That way, you can include the PHP code at the beginning of the file and not worry about being unable to access the title.
<?php
require_once($parameter->content);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title><?php echo $parameter->title; ?></title>
</head>
<body>
<?php
echo $parameter->display;
?>
</body>
</html>
This is how I solved the issue.
I changed the PHP design to something like:
//get the content PHP file
//inside the file I set the following variables
//which are used below:
//$parameter->title - the string which contains the title
//$parameter->html - the string which contains the HTML content
include($parameter->content);
//string which will contain the html code of the whole page
$html = <<<EndHere
<html>
<head>
<title>
EndHere;
//add title
$html .= $parameter->title;
$html .= <<<EndHere
</title>
</head>
<body>
EndHere;
//add the content of the page
$html .= $parameter->html;
$html .= <<<EndHere
</body>
</html>
EndHere;
//output the html
echo $html;
And here is the basic structure of the Content PHP file. Since the only page which can possibly include the file is the my design page, I can reference $parameter in it.
//set the title
$parameter->title = "sometitle";
//set the content HTML
$parameter->html = "some HTML here";
It's not a very clean solution but it works fine.