Open selected listbox item

Hi all,

I have the following script and would like to know where I would place code to open the item once it is selected from the listbox.
I'm planning on using the header('Location: ...;) method but have no idea where to place it in the code

I hope I made myself clear on what I need
Thanks a lot

<?php
session_start();

error_reporting(E_ALL);

// THE DESIRABLE FILE NAMES ARE KEPT IN AN ARRAY
//should be able to find 123*.xml
//$signals = Array($_SESSION['username'].'*.xml'=>'regex');
//$signals = Array($_SESSION['username'].'.*\.xml'=>'regex');
  $signals = Array( '^' . $_SESSION['username'].'.*\.xml'=>'regex');
  
// THE OPTION STATEMENTS ARE COLLECTED IN AN ARRAY
$options = Array();

// COLLECT THE FILE OBJECTS
$path = realpath(getcwd());
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// ITERATE OVER THE OBJECTS TO FIND THE DESIRABLE FILES
foreach($objs as $path => $obj)
{
    $name = $obj->getFileName();
    // this is looking for "exact" matches.
    if (in_array($name, $signals))
    {
        $options[]
        = '<option value="'
        . $path
        . '">'
        . $name
        . '</option>'
        ;
    }
	else
	{ //echo 'Searching for '.$name.PHP_EOL;
		foreach($signals as $k=>$v)
		{
            // is you set $signals = [ '123*.xml/'=>'regex', '456*.txt'=>'regex']
            //the if clause below will get executed
			if( $v=='regex' )
			{
				if( preg_match('/'. str_replace('/','\\/',$k) .'/',$name))
				{
					$options[]
					= '<option value="'
					. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
					. '">'
					. substr($name,strlen($_SESSION['username']),strlen($name)-4-strlen($_SESSION['username'])) //Only pass file name without ext
					. '</option>'
					;
				}
			}

            // is you set $signals = [ '123'], but you have the following files: '123.xml', 'xyz-123.txt'
            // the if clause below will get executed and will find both, because this is a "contains" filter
			elseif( stripos($name, $v)!==false )
			{
				$options[]
				= '<option value="'
				. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
				. '">'
				. $name
				. '</option>'
				;
			}
		}
	}
}

// GENERATE THE OPTION STATEMENTS
$opts = implode(PHP_EOL, $options);

// CREATE THE HTML USING HEREDOC NOTATION
$html = <<<EOD
<p>Leave History<br>
<select name="listbox" size="1">
<option value="">Open to view</option>
$opts
</select>
</p>
EOD;

// SHOW THE HTML
echo $html;

Open in new window

Pierre KAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try:
<?php
session_start();
error_reporting(E_ALL);

if( array_key_exists('request',$_POST) && array_key_exists('listbox',$_POST) && 'ajax'==$_POST['request'] && '.xml'==strtolower(substr($_POST['listbox'],-4)_ )
{
`	header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
	readfile($_POST['listbox']);
	exit;
}
// THE DESIRABLE FILE NAMES ARE KEPT IN AN ARRAY
//should be able to find 123*.xml
//$signals = Array($_SESSION['username'].'*.xml'=>'regex');
//$signals = Array($_SESSION['username'].'.*\.xml'=>'regex');
  $signals = Array( '^' . $_SESSION['username'].'.*\.xml'=>'regex');
  
// THE OPTION STATEMENTS ARE COLLECTED IN AN ARRAY
$options = Array();

// COLLECT THE FILE OBJECTS
$path = realpath(getcwd());
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// ITERATE OVER THE OBJECTS TO FIND THE DESIRABLE FILES
foreach($objs as $path => $obj)
{
    $name = $obj->getFileName();
    // this is looking for "exact" matches.
    if (in_array($name, $signals))
    {
        $options[]
        = '<option value="'
        . $path
        . '">'
        . $name
        . '</option>'
        ;
    }
	else
	{ //echo 'Searching for '.$name.PHP_EOL;
		foreach($signals as $k=>$v)
		{
            // is you set $signals = [ '123*.xml/'=>'regex', '456*.txt'=>'regex']
            //the if clause below will get executed
			if( $v=='regex' )
			{
				if( preg_match('/'. str_replace('/','\\/',$k) .'/',$name))
				{
					$options[]
					= '<option value="'
					. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
					. '">'
					. substr($name,strlen($_SESSION['username']),strlen($name)-4-strlen($_SESSION['username'])) //Only pass file name without ext
					. '</option>'
					;
				}
			}

            // is you set $signals = [ '123'], but you have the following files: '123.xml', 'xyz-123.txt'
            // the if clause below will get executed and will find both, because this is a "contains" filter
			elseif( stripos($name, $v)!==false )
			{
				$options[]
				= '<option value="'
				. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
				. '">'
				. $name
				. '</option>'
				;
			}
		}
	}
}

// GENERATE THE OPTION STATEMENTS
$opts = implode(PHP_EOL, $options);

// CREATE THE HTML USING HEREDOC NOTATION
$URL =$_SERVER['SCRIPT_NAME'];

$html = <<<EOD
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery('#fileList').on('change',function(){
		jQuery.ajax({'url':'{$URL}'
					, 'method':'POST'
					,'data': {'request':'ajax','listbox':jQuery(this).val()}
					}).done(function(data){
						//whatever you need to do upon success
					});
	});
});
</script>

<p>Leave History<br>
<select id="fileList" name="listbox" size="1">
<option value="">Open to view</option>
$opts
</select>
</p>
EOD;

// SHOW THE HTML
echo $html;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pierre KAuthor Commented:
Nothing happens when I run the script - the listbox doesn't appear anymore
0
hieloCommented:
>>  the listbox doesn't appear anymore
You mean when you first load the page? If so, take a look at your server log.  Most likely there is a runtime error that prevents the script from completing and hence the reason you don't even see the list.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Pierre KAuthor Commented:
@hielo - I cleared the cashe etc but it still doesn't appear.
I don't have enough pHp knowledge to spot a problem.
When I revert to the script before your changes it does appear again.
0
hieloCommented:
Do you have access to the command line?  If so, do a syntax check from the command line:
php -l your file.php

Line 5 seems to be missing a closing parenthesis at the very end, but I suggest you check the syntax from the command line if possible -- just to make sure all syntax problems are addressed first.
1
Pierre KAuthor Commented:
@hielo - thanks for tip about the syntax. I found another one and fixed it.
Now the listbox does appear so I'm very close to the goal

I replaced your line:
//whatever you need to do upon success

With this line:
header('Location: index.html');

Just to test but it did nothing. Any advice on that please?
0
Pierre KAuthor Commented:
Ultimately I would like to 'browse' to the the file named $name in the script in line 55 when it gets selected since the file will reside in the same directory my php file is located so I'm thinking it should just open it I hope
0
hieloCommented:
>> header('Location: index.html');
That will not work because header() is a php function -- it executes on the server-side before the html is sent to the browser.  If you need to do redirection, you need to use client-side rediretion:
window.location.href = "http://site.com/index.html";
1
Pierre KAuthor Commented:
@hielo - Thanks for pointing that out
I'll play around with the suggested option

All the best!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.