Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming technique question

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I have a complex form that is processed by php when submitted. At that point I don't want that form displayed but rather some other stuff. In order to put all the php code on one page, I have to put the complex form in an 'echo' or 'print' statement. This creates a problem editing the form using an HTML editor among other things. Alternatively I supposed I could put the form processing on a different page but that is more complicated to manage.

If I was able to explain it clearly, what is the "best practice" for handling this? TIA.
 
You could place the php code just in the value attribute of the form elements

Code:
<input type='text' name='fname' value='<?php echo $fname; ?>'>

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
You can use simple template file

form.htm - here is your form

php:
Code:
<?php
... your code...
...and now you want echo form, so:

echo implode ('', file ('form.htm'));
..other stuff...
?>

gry online
 
This is partially a philosophical discussion. I, for example, am used to work with external HTML templates which are processed by PHP code that is completely free of any presentational code - no echo, no print etc.
The 'drawback' is supposedly speed, which of course depends on your host. Loading the template and processing it will take longer than just echoing out HTML code.
In my case the servers are powerful enough that templating has no adverse effect on the performance, it's still fast.

It also has been suggested that the PHP interpreter processes code which has context switching less efficient (sleipnir214 profiled that somwhere...). Putting PHP command interspersed with HTML will slow the whole thing also.

On option, however, is to use large chunks of HTML with a echo/print statement using the HEREDOC syntax. It allows for 'plain' HTML, no escaping of quotes etc. necessary but variable interpolation is still available. For example what bastien suggested could be part of a heredoc portion:
Code:
print <<<END
<form name="myform" method="post" action="{$_SERVER['PHP_SELF']}">
<input type="text" name="fname" value="$fname">
<!-- etc. -->
END;
# PHP code continues, the context has not been switched ...
$whatever = function($arg);

In terms of maintenance I fully subscribe to the template/code separation philosophy.
 
Thanks to all those that replied. I knew this was a philosophical question but I don't have enough experience myself yet decide on my own. I also see some possibilities which hadn't occurred to me. You've given me a good start so I can evaluate what is best for my situation.

I like the simplicity of the HEREDOC syntax but I need to see if my HTML editor has a problem properly handling HTML tags between '<?' and '?>'.
 
I guess I am not understanding the HEREDOC syntax. Making it part of the PHP context is what I want to avoid. My editor will not look at anything but the source in that context.

It looks like the template is the only way. I was trying to avoid multiple files but that may be a necessary compromise to acheive what I want.
 
Multiple files are not bad.
Once you get into building larger applications you'll have to deal with modules anyway. Learning good housekeeping and naming conventions etc. is valuable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top