11 Replies - 7846 Views - Last Post: 08 July 2012 - 05:39 AM Rate Topic: -----

#1 Redbird5   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 07-July 12

Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 10:43 AM

I'm building my portfolio for an interview on Monday and I'm looking to add PHP to the site. I have tested the page in PHP and everything works fine, but when I put the PHP into my HTML page I all I see is the code and not the text. What do I need to do to connect both of them?
Is This A Good Question/Topic? 0
  • +

Replies To: Problem Connecting PHP to an HTML page

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 11:36 AM

PHP is server side, so it's never going to run in your HTML. HTML is processed by the browser, and that's on the client's computer. You need to call the server and let it run the PHP code, then send the data (HTML) back to the browser.
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 11:53 AM

In other words: A PHP enabled server executes PHP code within .php files on the server and returns the output to the browser. If you use .html files, however, they are just send directly to the browser without being executed. So if you want to put PHP code into a HTML file, just change the extension to .php and then add the PHP code.
Was This Post Helpful? 0
  • +
  • -

#4 Redbird5   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 07-July 12

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 12:01 PM

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>About Me</title>
<link href="portfolio.css" rel="stylesheet" type="text/css" media="all">
</head>

<body>
<div id="wrapper">
<div id="menu">
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li><a href="aboutme.html">About Me</a></li>
<li><a href="resume.html">Resume</a></li>
<li><a href="portfolio.html">Portfolio</a>
	<ul>
    <li><a href="websites.html">Websites</a></li>
    <li><a href="programming.html">Programming</a></li>
    <li><a href="graphics.html">Graphics</a></li>
    </ul>
</li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
<div id="banner"><img src="image/bannertext2.png" alt="N99design" title="N99design"/></div>
<div id="container">
<div id="content">
<!--PHP-->
<?php
$fp = fopen("aboutme.txt", "r");

$line = fgets($fp)){
print <p>$line</div>\n";

while(!feof($fp)){
$line = fgets($fp);
print "$line <br />\n";
}
fclose($fp);
?>

</div><!--End content-->
</div><!--End container-->
</div><!--End wrapper-->
</body>
</html>



Do I need to make the whole site PHP?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 12:28 PM

You need to start over. Stop thinking about how to get your code to work and start thinking about how to write code correctly.

Start with a hello world script and when you understand how and why that works, add a little to it, like maybe a form to input your name and then have the server respond to it. Don't proceed any further until you understand why your code does what it does. EVER.
Was This Post Helpful? 1
  • +
  • -

#6 Redbird5   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 07-July 12

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 01:15 PM

I have studied PHP for several months. I understand how the code works. What I don't understand is how can I put PHP into an HTML page. When I put the HTML code into the PHP everything worked fine. What I'm wanting to do is put PHP into a HTML page.
Was This Post Helpful? 0
  • +
  • -

#7 Redbird5   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 07-July 12

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 01:21 PM

Solved my own problem. I made my page into PHP and linked the HTML page to the PHP page. Everything works great.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 01:57 PM

No, you haven't solved anything. All you've done is push back the learning you need to do to a later date when you'll be trying to do something even bigger and probably more important to you. Then you'll be running around asking for help again, and you'll wish you had spent the time now to learn the difference* between server and client.


* I know that you don't know the difference because you think that you've managed to put your PHP into an HTML page. You haven't.
Was This Post Helpful? 1
  • +
  • -

#9 Redbird5   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 07-July 12

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 02:12 PM

I rearranged things, I put my HTML into my PHP code and things started working. What's the difference between a client side language and a server language? A client side language is a handled by the client's computer i.e HTML, CSS, Javascript. A server language is on the server which means the server has to talk to the client's computers i.e ASP.net, PHP, MySQL.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Problem Connecting PHP to an HTML page

Posted 07 July 2012 - 02:18 PM

View PostRedbird5, on 07 July 2012 - 05:12 PM, said:

A server language is on the server which means the server has to talk to the client's computers i.e ASP.net, PHP, MySQL.

No, the server side language only "talks" to the server.

  • Client takes an action.
  • Browser sends a request to server.
  • Server runs PHP script.
  • PHP script hands off its results to the server.
  • Server sends results to browser.
  • Browser renders the results.
  • Got to step 1.

Failure to understand these basic facts leads many to much trouble.

Mixing your languages makes things seem easier, but it actually hurts your progress.
Was This Post Helpful? 3
  • +
  • -

#11 calebjonasson   User is offline

  • Enter Title Here
  • member icon

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Re: Problem Connecting PHP to an HTML page

Posted 08 July 2012 - 12:17 AM

View PostRedbird5, on 07 July 2012 - 06:43 PM, said:

I'm building my portfolio for an interview on Monday and I'm looking to add PHP to the site. I have tested the page in PHP and everything works fine, but when I put the PHP into my HTML page I all I see is the code and not the text. What do I need to do to connect both of them?


I hope that the interview on monday does not relate to a job in computing, engineering or developing. At first I thought that you were just a troll trying to get a rise out of the clever people of this forum but it turns out that you managed to hack together a one page html/php page. Work on actually learning the language and going though the code. It also helps to have your error reporting configured to view everything. I only bring this up because the code you posted is syntactically flawed.
Was This Post Helpful? 0
  • +
  • -

#12 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Problem Connecting PHP to an HTML page

Posted 08 July 2012 - 05:39 AM

View PostRedbird5, on 07 July 2012 - 09:12 PM, said:

A server language is on the server which means the server has to talk to the client's computers i.e ASP.net, PHP, MySQL.

By the way, MySQL is a database server, not a server-side language.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1