Where to put php code inside html code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dhamaris
Forum Newbie
Posts: 1
Joined: Thu Jun 18, 2009 5:03 am

Where to put php code inside html code

Post by dhamaris »

Hi, i'm a newbie on this, i'm working with LDAP, and need to create an instance of a Class A that allows me to show or hide a field in an html form.

My problem is quite simple: i don't know where to put the <?php $hd = new A(); ?>.
Should i put it in the header??

Thanks.

Dámaris.

This is the first part of my html code:

Code: Select all

 
<?php
require_once("setVisibility.php");
require_once("Hidden.php");
 
 
?>
 
<html>
<head> <title> Persona </title>
 
     <link rel="stylesheet" type="text/css"  href="estilo_formulario.css" />
   
     
</head>
 
<body>
 
<form  action="tratarPersona.php"  method="post" type="text" >
<fieldset><legend align="left"> Datos personales </legend>
 
 
<div <?php  if($hd->getName() == true) {$tipo = "hide";} else {$tipo = "show";} ?> CLASS=<?php echo $tipo ?> id="div_name" >
                <label id="label_name" for="name"> Nombre </label>
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: Where to put php code inside html code

Post by jgadrow »

You should place it before you intend to utilize it. Additionally, you may not be aware of HEREDOC syntax. It tends to make pages such as this easier to present:

Code: Select all

<?php
require_once("setVisibility.php");
require_once("Hidden.php");
 
// create new instance of A and perform conditional logic for template display
$hd = new A ();
$tipo = ($hd->getName () === true) ? 'hide' : 'show';
 
// display template
echo <<<HTML_OUT
<html>
    <head>
        <title>
            Persona
        </title>
        <link rel="stylesheet" type="text/css"  href="estilo_formulario.css" />
    </head>
    <body>
        <form  action="tratarPersona.php"  method="post" type="text" >
            <fieldset>
                <legend align="left">
                    Datos personales
                </legend>
                <div class="$tipo" id="div_name">
                    <label id="label_name" for="name">
                        Nombre
                    </label>
                </div>
            </fieldset>
        </form>
    </body>
</html>
HTML_OUT;
?>
Hope that helps! :)
Post Reply