Jump to content



Photo

Is it Standards Compliant to mix HTML and PHP?


  • Please log in to reply
21 replies to this topic

#1 winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 28 November 2012 - 19:06

Can anybody tell me if it is standards compliant to mix php and html?


#2 +Nik L

Nik L

    Where's my pants?

  • Tech Issues Solved: 2
  • Joined: 14-January 03

Posted 28 November 2012 - 19:20

PHP will just render out HTML surely, so it depends what you render out?

#3 +virtorio

virtorio

    4089 III

  • Tech Issues Solved: 11
  • Joined: 28-April 03
  • Location: New Zealand
  • OS: OSX 10.9, Windows 8.1
  • Phone: Samsung Galaxy SIII

Posted 28 November 2012 - 19:22

What standard are you talking about? If you're asking about HTML standards then it doesn't matter what server side technology (PHP, ASP.net, Python) is used because the clients browser doesn't receive it, only the output of those programs/scripts.

#4 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 28 November 2012 - 19:27

What standard are you talking about? If you're asking about HTML standards then it doesn't matter what server side technology (PHP, ASP.net, Python) is used because the clients browser doesn't receive it, only the output of those programs/scripts.


I am talking about HTML standards. I just want to make sure that it remains fully compliant. I am working on a CMS and being able to mix the two will make it much easier to create the system. Also, would you recommend that route or maybe using AJAX to implement it? It will be a basic CMS system, but I want it to be able to refresh content on the page without redirecting the user.

#5 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 28 November 2012 - 19:36

Me thinks you've got your terms a bit mixed up. PHP will generate HTML, you won't see the PHP in the final markup. HTML standards compliance is a bit of a mixed bag these days, in general people are more concerned about making sure the markup works cross browser than strict adherence to standard, mostly because a lot of HTML 5 features aren't standardized yet.

The main thing to do when using PHP to generate HTML is to separate out the logic from the markup, so that if someone else wanted to redesign or reuse the HTML code, they wouldn't have to sort out all of the PHP to do so.

#6 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 28 November 2012 - 19:41

Me thinks you've got your terms a bit mixed up. PHP will generate HTML, you won't see the PHP in the final markup. HTML standards compliance is a bit of a mixed bag these days, in general people are more concerned about making sure the markup works cross browser than strict adherence to standard, mostly because a lot of HTML 5 features aren't standardized yet.

The main thing to do when using PHP to generate HTML is to separate out the logic from the markup, so that if someone else wanted to redesign or reuse the HTML code, they wouldn't have to sort out all of the PHP to do so.


Ok, so something like:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	    <meta charset="utf-8" />
	    <title>PHP Standards Compliant Test</title>
    </head>
    <body>
	    <?php require 'form.php';?>
    </body>
</html>

Is that what you mean?

#7 exotoxic

exotoxic

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 04-April 04
  • Location: England

Posted 28 November 2012 - 19:41

Its no problem, but its considered "bad practice" to mix large block of code.

#8 LaP

LaP

    Forget about it

  • Tech Issues Solved: 3
  • Joined: 10-July 06
  • Location: Quebec City, Canada
  • OS: Windows 8.1 Pro Update 1

Posted 28 November 2012 - 19:44

Ok, so something like:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title>PHP Standards Compliant Test</title>
	</head>
	<body>
		<?php require 'form.php';?>
	</body>
</html>

Is that what you mean?


We can't tell you if it's standard or not cause we don't know what html code the form.php page will generate.

To know if it's standard load the page in Firefox like a client would do and Validate it using a validator.

#9 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 28 November 2012 - 19:45

Ok, so something like:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title>PHP Standards Compliant Test</title>
	</head>
	<body>
		<?php require 'form.php';?>
	</body>
</html>

Is that what you mean?


More likely you'd include the form in the HTML, do simple validation with javascript, and use AJAX to send the results to a php file which handles it, then sends some information back which is processed through javascript, with a fallback to do that without javascript.

The way you have it there's probably a form with HTML markup stuck with PHP code.

#10 Anibal P

Anibal P

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 11-June 02
  • Location: Waterbury CT
  • OS: Win 8.1
  • Phone: Android

Posted 28 November 2012 - 19:45

Just about all the CMS out there that use PHP, Python and all the other scripting languages also generally have the badge that it passed HTML and/or CSS compliant

#11 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 28 November 2012 - 19:51

More likely you'd include the form in the HTML, do simple validation with javascript, and use AJAX to send the results to a php file which handles it, then sends some information back which is processed through javascript, with a fallback to do that without javascript.

The way you have it there's probably a form with HTML markup stuck with PHP code.


Ok, so the way that I originally wrote my code would probably be best then? Here is what I had:

news.html:

http://pastebin.com/rB9WgnD2[

ajax.js:

http://pastebin.com/9k1FJq5G

news.php:

http://pastebin.com/dfPtPL6h

#12 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 28 November 2012 - 20:08

Just an FYI, alternative syntax makes mixing PHP and HTML easier:



<?php while($a == 5): ?>
A is equal to 5
<?php endwhile;?>

also mysql_connect is not a good start... look into mysqli and prepared statements.

#13 OP winlonghorn

winlonghorn

    Neowinian

  • Joined: 17-March 05
  • Location: Erie, PA

Posted 28 November 2012 - 20:11

Just an FYI, alternative syntax makes mixing PHP and HTML easier:



[color=#000000][color=#0000BB]<?php while[/color][color=#007700]([/color][color=#0000BB]$a [/color][color=#007700]== [/color][color=#0000BB]5[/color][color=#007700]): [/color][color=#0000BB]?>[/color]
A is equal to 5
[color=#0000BB]<?php endwhile[/color][color=#007700]; [/color][color=#0000BB]?>[/color] [/color]

also mysql_connect is not a good start... look into mysqli and prepared statements.


Ok, thank you!

#14 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 28 November 2012 - 20:17

Also, if you want to save yourself a few hundred hours, check out Drupal 7 and Views. Based on what I've seen here, it's what you're heading towards anyway.

#15 Shadrack

Shadrack

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 20-December 01

Posted 28 November 2012 - 20:46

So long as the output is standards compliance, I don't see why not. PHP is just a server side scripting language and is not a standard.



Click here to login or here to register to remove this ad, it's free!