Page 1 of 1

Where to put php code inside html code

Posted: Thu Jun 18, 2009 5:09 am
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>

Re: Where to put php code inside html code

Posted: Thu Jun 18, 2009 11:29 am
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! :)