I have a PHP include that inserts an html form into another html form. When the form gets included I now have two form headers. Is there a php tag I could use that would allow me to...
<form id="orderform">
<!-- <?php for the include FROM here?> -->
PROD:<input class="ProductName" type="text" size="75">|
Discount:<input class="Discount" type="text" size="3">|
QTY:<input class="qty" type="text" size="6">|
Line/Total:<input class="LineTotal" type="text" size="9" disabled>
<!-- <?php for the include TO here?> -->
</form>
So the include would go into that file with the form in it and get the specified HTML?
Is this possible?
EDIT
<?php
$dom = new DOMDocument();
$html = 'phptest3.php';
$dom->loadHTMLFile($html);
$div = $dom->getElementById("divtagid");
echo $div->nodeValue;
?>
This only returns the text. How do I get the HTML form elements in the divtagid?
You can structure your include file like an XML file with processing instructionsXML (
DOMProcessingInstruction
Docs). Such a file would only miss the XML root element, which could be added on the fly.The benefit of this is, that it is compatible with both XHTML and PHP as long as you write PHP with the standard tags like
<?php ... ?>
because those are a valid XML processing instruction. Let's start with a simple, XHTML only example, the principle with PHP code is actually the same, but why make it complicated?include.php:
To create an include function that can deal with this, it only needs to parse the XML file and return the fragment in question, e.g. everything inside the
<form>
tag. XML parsing ships with PHP, so everything is already ready to action.Usage:
You don't need to change any of your template files as long as they contain XHTML/PHP in the end with such a method.
You should best look into php functions for this.
You will use this as:
Or, as a oneliner:
No, there isn't an alternate way to include a portion of a file. Consider: how would you describe to such a function where to start and end inclusion? How would the function know what you want?
As suggested, the best approach would be to refactor the included file. If that isn't an option (I can't imagine why it wouldn't be), another route is to use variables or constants in the included file to denote which portions should be output. Something like this:
Now, when you want to stop the form header from being output:
This is nasty, IMO. When someone else (or the future you) edits
form_template.php
, it may not be apparent when or why$include_form_header
would be set. This kind of reliance on variables declared in external files can lead to spaghetti code.You're far better building separate templates for different purposes (or directly echoing trivial output, like one line of html to open or close a form), for instance:
Last option, if you have absolutely no access to the template, can't modify it, can only include it, then you could use output buffering to include the file, load the resulting HTML into DOMDocument, then peal off the wrapping form tags. Take a look at the code... this isn't exactly "neat" either: