How to use <a> tag and pass one parametar?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
бишкебишке
Forum Newbie
Posts: 2
Joined: Fri Aug 27, 2010 3:12 am

How to use <a> tag and pass one parametar?

Post by бишкебишке »

I have one page where i list directories and files in xampp directory. But now i want to put all files in <a> tag so i could click on them and to open them for editing. But i don't know how to pass parametar in a tag. Here is the code:

Code: Select all

<?php
$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
function get_dirs($dir){ 
    global $dirs; 
    if (!isset($dirs)){$dirs = '';} 
    if(substr($dir,-1) !== '\\'){$dir .= '\\';} 
    if ($handle = opendir($dir)){ 
	$dirs .="<ul>";
        while (false !== ($file = readdir($handle))){ 
            if ($file != "." && $file != ".."){ 
                if(filetype($dir.$file) === 'dir')
				{
					clearstatcache(); 
					$dirs .= "<li>" . $file . "</li>"; 
                	get_dirs($dir . $file); 
				}
				else
				{
					//$dirs .= "<li>" . $file . "</li>";
					$dirs .= "<li><a href=\"otvaranje_fajla.php?naziv=\"" . $file  . "\">" . $file . "</a></li>";
					
				}
            } 
        }
	$dirs .="</ul>";	
    closedir($handle); 
    } 
    return $dirs; 
} 
//C:\\xampp\\htdocs
$direktorijumi=get_dirs("$DOCUMENT_ROOT");
echo "$direktorijumi";
?>

So when i click on one file it opens file:

Code: Select all

<?php
$nazivFajla=$_GET['naziv'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
echo "Naziv fajla je $nazivFajla";
?>
<body>
</body>
</html>

For now i just want to pass parametar i show it on the screen.
When i click on one of files i get in the address bar of browser

Code: Select all

http://localhost/otvaranje_fajla.php?naziv=
so parametar is blank. Also screen is blank.

Obviously problem is:

Code: Select all

$dirs .= "<li><a href=\"otvaranje_fajla.php?naziv=\"" . $file  . "\">" . $file . "</a></li>";


P.S. I am beginer in php programming.
бишкебишке
Forum Newbie
Posts: 2
Joined: Fri Aug 27, 2010 3:12 am

Re: How to use <a> tag and pass one parametar?

Post by бишкебишке »

I changed it to:

Code: Select all

$dirs .= '<li><a href=\"otvaranje_fajla.php?naziv=\"' . $file  . '\">' . $file . '</a></li>';

But now in address bar there is:
and got page:
Access forbidden!

You don't have permission to access the requested object. It is either read-protected or not readable by the server.

If you think this is a server error, please contact the webmaster.
Error 403
localhost
8/27/2010 10:54:51 AM
Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to use <a> tag and pass one parametar?

Post by Jonah Bron »

If you view the source of the page in your browser, what does the url look like there?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to use <a> tag and pass one parametar?

Post by McInfo »

бишкебишке wrote:I changed it to:

Code: Select all

$dirs .= '<li><a href=\"otvaranje_fajla.php?naziv=\"' . $file  . '\">' . $file . '</a></li>';
There should be no double-quote immediately after "naziv=". Also, within single-quoted strings, do not escape double-quotes.

Correct:

Code: Select all

$dirs .= '<li><a href="otvaranje_fajla.php?naziv=' . $dir . $file  . '">' . $file . '</a></li>';
Post Reply