How does one properly mix html and php.

Recommended Answers

All 4 Replies

Your question is vague and might be interpreted in different ways depending on how much importance that you put on 'properly'. You can easily mix php and html in a PHP module. Just end the PHP section with a ?> and start your html. You can go back and forth as many times as needed in a module. You can also use PHP to echo html statements. The purists don't like to see things mixed together so they would probably prefer the first approach. You can also put your html in separate modules and include them at the appropriate place(e) in your PHP module.

<?php
echo "<form>
<input type='text' name='txtsample' value='' />
<input type='submit' name='btnsubmit' value='submit' />
</form>";
?>

or

<form>
<input type='text' name='txtsample' value="<?=$_GET['txtsample']?>" />
<input type='submit' name='btnsubmit' value='submit' />
</form>"
Member Avatar for rajarajan2017

Below is the sample of mixing html and php:

<html>
<head></head>
<body>

<?php
if (!isset($_POST['submit'])) {
?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Enter your age: <input name="age" size="2">
    <input type="submit" name="submit" value="Go">
    </form>

<?php
    }
else {
echo "Else part";
}
?>

</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.