View Single Post
Old 03-09-2011, 03:37 PM   #2 (permalink)
tony
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

if you want to combine this 2 files in 1, you would need to add an if statement checking for the submition of the form.
first change the form's action to your current file (and maybe closing the php tag before the html, that way you can take advantage of html highlighting instead of a string):
php Code:
/** edit-text.php **/
<?php 
$myFile = "../includes/docs/sdata.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
?>
<form action="<?php echo __FILE__; ?>" method="post" enctype="multipart/form-data">
    <label for="templateedit">Edit template script</label><br /
    <textarea name="templateedit" cols="90" rows="20" id="templateedit">' . $theData . '</textarea><br><br>
    <input type="submit" name="save" value="Save Template script" />
    <!--changed the button for an actual submit button-->
</form>
Then what you would need is have a big if statement making sure if the submit button value is empty or not, if it is not. If it is not empty (or not set) then the form hasn't been submitted. something like this:
php Code:
<?php
if(isset($_POST['save']) && !empty($_POST['save'])) {
    //put here what would you need to do to process the submitted info
    //in this case, what you have in your second file in your example, without the header() call
}
$myFile = "../includes/docs/sdata.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
?>
<form action="<?php echo __FILE__; ?>" method="post" enctype="multipart/form-data">
    <label for="templateedit">Edit template script</label><br /
    <textarea name="templateedit" cols="90" rows="20" id="templateedit">' . $theData . '</textarea><br><br>
    <input type="submit" name="save" value="Save Template script" />
    <!--changed the button for an actual submit button-->
</form>

To close I would suggest using $_POST instead of $_REQUEST to access the form info. Request mangles all types of request together, which IMO brings an obscurity when handling requests.
Also take a look to the functions file_get_contents and file_put_contents, they might save you some file handling coding.
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
Tim Dobson (03-14-2011)