How do I modify and insert form elements using PHP?
I have a form assigned to a variable in PHP like so:
$the_original_form = '
<form method="post" action="whatever.php">
<input type="text" name="iamtextfield" id="textisme">
<input type="file" name="iamfilefield" id="fileisme">
<input type="hidden" name="hideme" value="nope">
</form>
';
I have a second variable:
$put_me_on_the_top = '<div class="getinme">';
And a third variable:
$put_me_on_the_bottom = '</div><div class="justsomethingelse">hi there</div>';
And a modified form element of:
$i_have_changed = '<input type="file" name="filepartdeux" id="iamabetterfile">';
And I want to end up with:
$the_modified_form = '
<form method="post" action="whatever.php">
<input type="text" name="iamtextfield" id="textisme">
<div class="getinme">
<input type="file" name="filepartdeux" id="iamabetterfile">
</div>
<div class="justsomethingelse">hi there</div>
<input type="hidden" name="hideme" value="nope">
</form>
';
You can load a mal-formed or well formed HTML document into a new DOMDocument and manipulate it as you would XML.