0

This is a bit hard to explain: I have a simple PHP file that outputs another PHP file with an include:

<?php include("file.php"); ?>

What I wanted to do is to have only part of it shown in the first page. So I used overflow to create a window with scrollbars:

<div style="height: 130px; width: 800px; border: 1px solid #CCCCCC; overflow: auto;">
 <?php include("file.php"); ?>
</div>

Everything works great. But now I want to add another functionality: When you click inside this overflowed area and press Ctrl+A, only the part in the overflowed area should be selected (not the whole page).

I tried using HTML frames, which gave me the function I wanted but the overflowed PHP file doesn't get parsed. Is there a way to accomplish this and still have the PHP file be parsed?

user2428118
  • 7,935
  • 4
  • 45
  • 72
noClue
  • 958
  • 1
  • 13
  • 34
  • Actually it does when it comes to the part with the frame. I was about to edit the question but then I read about the frame problem. – AndreKR Oct 11 '12 at 15:12
  • 1
    Frames (any clientside feature, for that matter) don't have any influence on what gets parsed or not by the server. Out of curiosity: what are the filenames of the including and the included php files exactly? – Roman Oct 11 '12 at 15:20
  • I'm sorry, I can't tell you the exact filenames so lets just say that the first php file is named "file1.php" and the file that's included in "file1.php" is "file2.php". "file2.php" has a simple echo statement. – noClue Oct 11 '12 at 15:28

2 Answers2

3

You have several options

Don't include, link through iframe

I don't know how you did it, but frames have no influence over what is parsed by the server. IFrames, or frames, just make a regular GET request, the server then decides whether to parse or not the requested file.

file1.php

<style>
    .document {
        height: 130px; 
        width: 800px;
        border: 1px solid #CCCCCC;
    }
</style>
<iframe src="file2.php" class="document" />

Include into an element that limits the "select all"

If you only want to include "text" (who knows, it could be...). Consider putting your included file into a textarea.

file1.php

<style>
    .document {
        height: 130px; 
        width: 800px;
        border: 1px solid #CCCCCC;
    }
</style>
<textarea class="document"><?php include('file2.php'); ?></textarea>

Quirky way: catch "ctrl+a" and apply contenteditable for a moment

ContentEditable fields limit their "select all". See demo here

file1.php

<style>
    .document {
        height: 130px; 
        width: 800px;
        border: 1px solid #CCCCCC;
    }
</style>
<div class="document"><?php include('file2.php'); ?></div>
$(window).bind('keydown', function(e) {
    if(event.ctrlKey && event.keyCode == 65) {
        var self = $('.document').attr('contenteditable', '');
        window.setTimeout(function() {
            self.removeAttr('contenteditable');
        }, 20);
    }
});

​Using contenteditable has 1 advantage over using textranges to select the contents: when clicking outside of your included document, all text on the page is selected (instead of "forcing" the "select only included file" feature).

Roman
  • 5,888
  • 26
  • 47
  • Very nice...but textareas are not an option for me. I want to output some html code for copying using the included file inside an xmp-tag (so the code doesn't get executed) and using textareas will cause some of the code to be executed, even if they are inside the xmp-tag (  will appear as empty spaces for example). Unless there is another way to make sure the whole code is displayed unformatted but that would be a different topic altogether. XD – noClue Oct 12 '12 at 07:00
  • But it actually works exactly the way I wanted it using divs instead of textareas. Many tnx! – noClue Oct 12 '12 at 08:03
  • @NoClue, yeah, I actually meant `div` in the last option. Just like the linked jsfiddle. – Roman Oct 18 '12 at 15:57
  • Yeah I realized it a bit too late, sorry about that. XD – noClue Oct 19 '12 at 04:21
0

It may be possible to intercept the key combination with javascript. If you manage to intercept it, then possibly you can highlight the part you require. See here for a more detailed analysis of what can and can't be done.

EDIT:

I've checked this in Firefox 13 and it seems to intercept the key combination Ctrl+A

$(window).keydown(function(event) {
  if(event.ctrlKey && event.keyCode == 65) { 
    console.log("Ctrl+A event captured!");
    event.preventDefault(); 
  }
});

Probably you can insert code in there to select the portion of text you wish to select.

EDIT 2: How to select the text:

I found this post which explains how to select text (highlight with mouse). I put it all together to get this:

$(window).keydown(function(event) {
  if(event.ctrlKey && event.keyCode == 65) { 
    console.log("Ctrl+A event captured!");
    selectText('test');
    event.preventDefault(); 
  }
});

function selectText(element) {
  var doc = document
      , text = doc.getElementById(element)
      , range, selection
  ;    
  if (doc.body.createTextRange) { //ms
      range = doc.body.createTextRange();
      range.moveToElementText(text);
      range.select();
  } else if (window.getSelection) { //all others
      selection = window.getSelection();        
      range = doc.createRange();
      range.selectNodeContents(text);
      selection.removeAllRanges();
      selection.addRange(range);
  }

Again, in Firefox 13, this seems to work. Put the ID of the div you need to select as a parameter to the selectText() call.

Community
  • 1
  • 1
GarethL
  • 1,473
  • 1
  • 15
  • 16
  • That post is only about Ctrl-N, -T and -W. For example it is also possible to Capture Ctrl-X, -C and -V. – AndreKR Oct 11 '12 at 15:20
  • The same principle may apply to Ctrl+A - I haven't checked, hence I wrote "may" in italics in my post. I was just noting that *some* Ctrl+key events can be captured – GarethL Oct 11 '12 at 15:22
  • In fact, I've just checked in Firefox 13.0.1 and it is possible to capture the Ctrl+A combination. I've updated my answer. – GarethL Oct 11 '12 at 15:36
  • I see what you're getting at but I still don't know how I could possibly implement this...maybe javascript can detect where I clicked and, depending on where I clicked, select a whole area with CTRL+A? Is that even possible? – noClue Oct 11 '12 at 15:37
  • I don't know - I was just trying to come up with a solution to your problem, and as far as I can see, intercepting the Ctrl+A event is one way. How to implement it, I don't know... you could set a variable in the onclick event of the div containing the stuff to be selected? – GarethL Oct 11 '12 at 15:41
  • Alright, tnx...I'll try. When I get a working example I'll update my post. – noClue Oct 11 '12 at 15:55