Page 1 of 1

Get Div id

Posted: Thu May 14, 2009 8:36 am
by manRay
This there a way I can get the id of a div, much like the javascript version getElementById()?

Re: Get Div id

Posted: Thu May 14, 2009 8:41 am
by crazycoders
Are you asking from PHP?

If so, note that PHP is a server side language that works with variables, functions and classes. It is not aware of HTML. PHP generates HTML that is sent to the user, only javascript can manipulate the HTML that the client is seeing because that is what the browser interprets.

Server -> PHP -> GenerateHTML() -> Web -> ClientBrowser -> RenderVisually() -> Client!

You can't go the other way around!

Re: Get Div id

Posted: Thu May 14, 2009 8:51 am
by manRay
Well, I have a function that I want to output into a div, everytime it is executed...So this ondblclick='openFolder($dir$folder/); can not be done. The function is a php function.

Re: Get Div id

Posted: Thu May 14, 2009 8:54 am
by crazycoders
The you have to do this:

Code: Select all

 
<html>
<body>
 
...some html...
 
<div id="myidsearched"><?php echo theFunctionIWantToOutputHere(); ?></div>
 
...some more html...
 
</body>
</html>
 
 

Re: Get Div id

Posted: Thu May 14, 2009 8:55 am
by crazycoders
But i just saw that you wish to do this function on a double click...
i think what you MAY need is an AJAX request/response, but this is out of my usualy procedure and if you are new to php, i think this may be out of your league although i don't know you!

Re: Get Div id

Posted: Thu May 14, 2009 9:02 am
by manRay
This is what I have.

Code: Select all

<?php
$dir = "../users/$email/";
 
$dh = opendir($dir);
 
while (false != ($folder = readdir($dh))) {
        if ($folder=='audio' & ($folder != '.' || $folder != '..'))
        $pic2 = '../images/audio.gif';
        else if ($folder=='desktop' & ($folder != '.' || $folder != '..'))
        $pic2 = '../images/apps.gif';
        else if ($folder=='documents' & ($folder != '.' || $folder != '..'))
        $pic2 = '../images/docs.gif';
        else if ($folder=='photos' & ($folder != '.' || $folder != '..'))
        $pic2 = '../images/photos.gif';
        else if ($folder=='video' & ($folder != '.' || $folder != '..'))
        $pic2 = '../images/video.gif';
        
        echo "<img src=$pic2 ondblclick='openFolder($dir$folder/);' class='draggable'/>";
        }
        
        closedir($dh);
?>
 
Which searches for folders on my server. Than for every folder it finds it sets an image along with a double click feature, that executes a php function, but considering php is server side. It will be looking for a javascript function.

Heres the function..this is the php function

Code: Select all

function openFolder($dir) {
 
$dh = opendir($dir);
 
while (false != ($file = readdir($dh))) {
        echo "<img src='$pic' onclick='openFile($dir$file);'>";
}
        closedir($dh);
}
 

Re: Get Div id

Posted: Thu May 14, 2009 9:26 am
by crazycoders
Ok you really need to understand the difference between PHP/ServerSide and Javascript/HTML/ClientSide.

PHP occurs when the browser asks for a page which occurs when a user clicks on a link or when a submit button is pressed. At that moment, the page is cleared and the browser is waiting for a response from the server.

The server processes the request usually by reading the $_GET or $_POST and then returns a complete new page. You cannot call PHP from javascript per se. So when the request completes, the buffer of information containing the HTML to display is sent back to the browser and the browser "renders" an image of it to display to the user. It is not an image per se but let's imagine it that way. If a user clicks on a link in that page, the browser sends another request to the server and the wheel starts over again.

If you wish, you can use AJAX to change the page on the fly without having to redraw everything... Javascript (the JA in AJAX) can be used to act like a browser by triggering a request on the server and then it can read the information returned by the server and change the page. No page flicker, the information changes and thats that.

But it's not easy to implement that and since you have a hard time understanding the difference between HTML and PHP, i think you should not try this option yet and understand the mechanism of posting a request to the server, process it, return new HTML that matches the content of the user.

Re: Get Div id

Posted: Thu May 14, 2009 9:54 am
by manRay
Whoa! Wait a sec, before you say I don't understand the difference between the two. I made an error, but don't say I don't understand. I realized the error and now looking for a way around it. So if you do than help me out, but dont judge.

Re: Get Div id

Posted: Thu May 14, 2009 10:05 am
by crazycoders
Well thats what i saw, sorry if you felt attacked, it was not my intent! I'm willing to help you solve the problem but you'll have to understand those basics first. When you do, the solution shouldnt be too hard to come to you.

:)

Re: Get Div id

Posted: Thu May 14, 2009 10:08 am
by manRay
This is the current code I am using ..which include AJAX and php.
This is php.

Code: Select all

<?php
$dir = "../users/$email/";
 
$dh = opendir($dir);
 
while (false != ($folder = readdir($dh))) {
        if ($folder=='audio')
        $pic2 = '../images/audio.gif';
        else if ($folder=='desktop')
        $pic2 = '../images/apps.gif';
        else if ($folder=='documents')
        $pic2 = '../images/docs.gif';
        else if ($folder=='photos')
        $pic2 = '../images/photos.gif';
        else if ($folder=='video')
        $pic2 = '../images/video.gif';
        else if ($folder=='.')
        continue;
        else if ($folder=='..')
        continue;
        
        echo "<img src=$pic2 ondblclick='openFolder($dir$folder/);' class='draggable'/>";
        }
        
        closedir($dh);
?>
 
this is ajax function

Code: Select all

function openFolder(dir){
 
    var http = getHTTP();
 
    // just in case that users browser is out of date and does not support ajax, we have to submit forms the old fashioned way.
 
    if (http == null){
 
        document.register_form.submit();
 
    }
 
    // define form vars
 
    var directory = dir;
 
 
    // avoid caching
 
    var cache = Math.random();
 
    // define php file that handles the response
 
    var url = "../php/openfolder.php";
 
    // define request
 
    var request = "dir="+directory;
 
    // open connection
 
    http.open("POST", url, true);
 
    // set headers
 
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 
    http.setRequestHeader("Content-length", request.length);
 
    http.setRequestHeader("Connection", "close");
 
    http.onreadystatechange = function(){
 
        if (http.readyState == 4 || http.readyState == "complete"){
 
            var result = http.responseText;
 
            // this is the information portal
 
            // that variable RESULT will contain anything thats outputed by php
 
            // which you can use to do what ever
 
            // in this case im just alerting it out
 
            var k = document.getElementById('folderItems');
            k.innerHTML=result;
        }
 
    }
 
    http.send(request);
 
}
and this is openfolder.php

Code: Select all

<?php
 
$directory = $_POST['dir'];
 
$dh = opendir($directory);
 
while (false != ($file = readdir($dh))) {
        echo "<img src='../images/audio.gif' onclick='openFile($dir$file);'>";
}
        closedir($dh);
 
?>