PHP Javascript & HTML frames

Posted on 2005-04-16
Last Modified: 2013-12-16
I have this special requirement.  Using an existing PHP script, a user select a variable number of files for editing, the best presentation depends a lot according to the number of files selected.

With the knowledge I have I could spend days in trial and error with this, I'm sure an efficient script could be rather short, so here is what I need to do, probably using a mixture of PHP/Javascript:

1) build a HTML frameset according to this number (from 2 to 6 frames, frames are not used for other numbers)
2) display in each frame the contents of one selected file (filenames are held in variables in script mentioned above)
3) present these contents in "textarea" for editing and then save if changed

I can probably adapt your solution script to fit the "frameset" but if you need to know, the division of the screen is as follows:
- one bottom line kept for navigation in all cases
- rest of screen divided in 2 columns for 2 files or 3 rows for 3 files or 2rows of 2 columns for 4 files or 2 rows of 3 columns for 5 and 6 files
(If of any use, I have these framesets in working HTML files as 2frams.htm, 3frams.htm, 4frams.htm and 6frams.htm)

Thank you for your help.
Question by:rblampain
    LVL 7

    Accepted Solution

    I created a scenario with an <iframe> element instead <framesets> but you can adapt that.

    So, you will have two files: one for your main frame (file_edit_main.php) and your editing gallery frame (file_edit_iframe.php). Plus, a lot of files stored in a subdirectory named "myfiles".


      <title>Text Editing Gallery</title>

    <form name="myForm" method="post" target="myImageGallery">

          <input name="selectedFiles" type="hidden" value=""/>


    $editableFiles = array("file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt",
                      "file6.txt", "file7.txt", "file8.txt", "file9.txt");

    for ($i=0; $i<count($editableFiles); $i++) {
          $filename = $editableFiles[$i];
          print '<input id="editableFiles" type="checkbox" value="'.$filename.'">File #'.($i+1).'<br>';


          <button type="button" onclick="editSelectedFiles();">Edit</button>

    <script language="javascript">
    var theForm = document.forms['myForm'];

    function getSelectedFiles()
          var sel = "", obj = null;
          for (var i=0; i<theForm.editableFiles.length; i++) {
                obj = theForm.editableFiles[i];
                if (obj.checked==true) {
                      if (sel!="")
                            sel += ",";
                      sel += obj.value;
          return sel;

    function editSelectedFiles()
          window.frames['myImageGallery'].location = "file_edit_iframe.php?selectedFiles="+getSelectedFiles();

    <iframe id="myImageGallery" name="myImageGallery" src="file_edit_iframe.php" width="700" height="300"></iframe>


    $selectedFiles = $_GET['selectedFiles'];
    if ($selectedFiles!="")
        $selectedFiles = explode(",", $selectedFiles);

    if (!is_array($selectedFiles))
        die ("No files were selected.");

    $basedir = "myfiles/"; //directory where your files are stored
    $fileCount = count($selectedFiles);
    $maxCols = 3; //max number of columns you wish to display
    $colLimit = min($maxCols, ceil(sqrt($fileCount))); //actual column limit
    $colCount = 0;

    print "<body><table border='0' cellspacing='0' width='90%'>";

    for ($i=0; $i<$fileCount; $i++) {
        $filename = $selectedFiles[$i];
        $fileText = file_get_contents($basedir.$filename);

        // if cell goes in left-most column, open table row...
        if ($colCount == 0)
            $cellHtml = "<tr>";
        // ...otherwise, initialize cell's HTML variable
            $cellHtml = "";

        // add cell's own html
        $cellHtml .= "<td align='center'>".$filename."<br>"."<textarea>".$fileText."</textarea></td>\n";

        // if cell goes in right-most column, close table row
        if ($colCount == ($colLimit-1))
            $cellHtml .= "</tr>\n";

        // output cell's complete HTML to browser
        echo $cellHtml;

        // increment column index
        $colCount = ++$colCount % $colLimit;
    // if last cell rendered was not in last column, then row is still open, so close it!
    if ($colCount != 0)
        echo "</tr>";
    // close table
    echo "</table></body>";
    LVL 7

    Expert Comment

    Note that in file_edit_main.php I hard-coded file names into an array. I don't know where you're actually planning to retrieve them from (a database, a filesystem function that reads from a directory, etc).

    Author Comment

    Thanks to lexlythius. I realize now that my definition of what I need is not good at all. I only have a basic understanding of PHP so please correct me if I'm wrong.

    It looks like your script divides the screen into 2 windows but the users may want to edit an undefined number of files at the same time,  I failed to specify that.

    What I wanted to do is divide the screen in 2 if they want to edit 2 files at the same time, divide the screen in 3 if they want to edit 3 files at the same time, divide the screen in 4.... or 6 which is the maximum that can fit for legibility. If the user wants to edit only one file then the whole file is displayed, if the user wants to edit more than 6 files at the same time then each file is presented in a fixed size "textarea" which will toghether occupy more than the screen space and scroll.

    I already have  tested "HTML frameset" for the purpose and what I need is  a PHP script that creates these framesets and displays in the frames the contents of the files to be edited.

    The files to be edited are already selected and the full paths are retrieved from an array from a previous PHP script, this script is taylored so that the selected files can have the (linux) permissions changed from read-only to read/write for saving after editing. So this array is to be passed between scripts with the "PHP session".

    You deserve the points for your work because my description was confusing.

    Author Comment

    I've re-introduced the question under "PHP, javascript & frames"

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
    The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now