John Smith
John Smith

Reputation: 71

Print php data in new tab

I have a simple php code like this:

    //index.php
    $file  = fil1.txt;
    //open file using fopen...
    echo $file;

What should I do if I need to print my file data in new tab in another php file(e.x. result.php) ?

This is real my example of code:

function elementStart($parser, $name, $attrs) {
global $depth;

echo str_repeat(" ", $depth * 0); 
//echo "<b>$name</b><br>";     

$depth++; 

    foreach ($attrs as $attr => $value) {
        echo str_repeat("&nbsp;", $depth * 0); 

        echo $value.'<br>';
    }
}

function elementStr($parser, $str) {
    if (strlen(trim($str)) > 0) {
        global $depth;

        echo str_repeat("&nbsp;", $depth * 0); 
        echo $str.'<br>'; 
    }
}

function elementEnd($parser, $name) {
    global $depth;

    $depth--;
}

require_once 'connect.php';

if(isset($_POST['submit1']))
{

$db = new database('localhost', 'root', '', 'database1');
$val = mysql_escape_string($_POST['xml1']); 
$db->query("SELECT * FROM xmltable1 WHERE xmlvalue LIKE '%$val%' LIMIT 1");
if($_POST['submit1'])
{



    if($dbase->numRows() == 0){
        echo 'No rows';
    }
    else{
        foreach($db->rows() as $name){
            //echo $name['xmlvalue'];

            define('FOLDER_PATH', 'C:user/folder1/xmld/');
            $full_path = FOLDER_PATH.$name['xmldata'];
            //echo $full_path;
                    $depth = 0;
                    $file  = $full_path;

                    $xml_parser = xml_parser_create();

                    xml_set_element_handler($xml_parser, "elementStart", "elementEnd");
                    xml_set_character_data_handler($xml_parser, "elementStr");


                    while ($data = fgets($fp)) {

                    thexml_parser_free($thexml_parser);

            }

        }
    }

$dbase->disconnect();
}

In my functions there are echo. And of course I get echo in current page, but I need echo in new tab.

Upvotes: 2

Views: 3605

Answers (2)

BBagi
BBagi

Reputation: 2085

The only way to pass data to another PHP file in another window (or tab) is by using sessions.

At the top of both your PHP files, start a session:

session_start();

In your index.php assign the file data to a session variable:

$_SESSION['file_data'] = $file_contents;

In your results.php, print out the data:

echo $_SESSION['file_data'];

Upvotes: 1

Kai
Kai

Reputation: 342

Open an new tab is not PHP should do, you can try using javascripntk to achieve like this:

echo <<<EOF
<script>
var win = window.open('about:blank');
var doc = win.document;
doc.write('{$yourdata}');
</script>
EOF;

Upvotes: 1

Related Questions