user3449212
user3449212

Reputation:

exporting table to xlsx writes entire page content to file

Exporting html table to xlsx file writes every html page content in file.

I have four buttons and other html code on my page. Among 4, one of button results html table and writes it to xlsx file.

But apart from writing the table content, it also writes label content , Name of all four table , and html of

            <h1> Arabic to English </h1>

            <form method="post" enctype="multipart/form-data">

            *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English" />

            </form>

How can stop writing content other then html table to the exported xlsx file?

Here is my code:

    <label style ="margin-left:30%; margin-bottom:10%; color:red">Please reaload the page before each test [would be fixed]</label>     

<div class= "c1">
    <div class="row demo-row">
            <div class="col-xs-3">
            <a id="Bar2en" rel="leanModal" name="ar2en" href="#ar2en" class="btn btn-block btn-lg btn-primary">Arabic to English</a>
        </div>
            <div class="col-xs-3">
            <a id="Ben2ar" rel="leanModal" name="ben2ara" href="#ar2en"  class="btn btn-block btn-lg btn-success">English to Arabic</a>
        </div>
            <div class="col-xs-3">
            <a id="Ben2ar" rel="leanModal" name="ben2ara" href="#xlsar2en"  class="btn btn-block btn-lg btn-info">Export Arabic to English</a>
        </div>
            <div class="col-xs-3">
            <a id="Ben2ar" rel="leanModal" name="ben2ara" href="#xlsen2ar"  class="btn btn-block btn-lg btn-info">Export English to Arabic</a>
        </div>
</div>
</div>
<div id="xlsar2en" style="text-align:center">
        <div class="container">
        <div style="text-align:center">
    <?php
    if (isset($_FILES['file'])) {
        header('Content-Type: text/html; charset=UTF-8');
        require_once "simplexlsx.class.php";
        require '../../Arabic.php';
        $Arabic = new I18N_Arabic('Transliteration');
        $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
        $file="result.xls";
    //echo $_POST['submit1'];
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$file");
        echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
        $eng = array();
        list($cols,) = $xlsx->dimension();  
        foreach( $xlsx->rows() as $k => $r) {
    //      if ($k == 0) continue; // skip first row
            echo '<tr>';
            for( $i = 0; $i < $cols; $i++)
            {
                if ($_POST['submit2'] = 'Arabic to English')
                    $temp =  $Arabic->en2ar($r[$i]);
                else
                    continue;   
                echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
    }
    ?>
        <h1> Arabic to English </h1>
        <form method="post" enctype="multipart/form-data">
        *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English" />
        </form>
    </div>
   </div>
</div>

UPDATE :

<div id="xlsar2en" style="text-align:center">
        <div class="container">
        <div style="text-align:center">
        <!-- PHP script -->
        <h1> Arabic to English </h1>
        <form method="post" action="a2e.php" enctype="multipart/form-data" target="iframe1" >
                *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English"/>
    </form>
    <iframe name="iframe1" id="iframe1" src="" style="display:none" ></iframe>
    </div>
   </div>
</div>

<div id="xlsen2ar" style="text-align:center">
        <div class="container">
        <div style="text-align:center">

        <h1> English to Arabic</h1>
        <form method="post" action="e2a.php" enctype="multipart/form-data" target="iframe2" >
        *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit2" value="Enlish to Arabic" />
        </form>
        <iframe name="iframe2" id="iframe2" src="" style="display:none" ></iframe>
    </div>
   </div>
</div>

e2a.php

    <?php
    if (isset($_FILES['file'])) {
        header('Content-Type: text/html; charset=UTF-8');
        require_once "simplexlsx.class.php";
        require '../../Arabic.php';
        $Arabic = new I18N_Arabic('Transliteration');
        $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
        $file="result.xls";
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$file");
        echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';

        $eng = array();
        list($cols,) = $xlsx->dimension();  
        foreach( $xlsx->rows() as $k => $r) {
    //      if ($k == 0) continue; // skip first row
            echo '<tr>';
            for( $i = 0; $i < $cols; $i++)
            {   
                if ($_POST['submit2'] = 'Enlish to Arabic')
                    $temp =  $Arabic->en2ar($r[$i]);    
            echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
    }
    ?>

Upvotes: 0

Views: 1370

Answers (1)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

output buffer, before your header() call, or place the header at the top, file writing will capture any output, so obviously any html before you do the file writing will be captured, you have 3 choices,

  1. don't output anything before writing the file, ( move that code before any html output ) - possibly it will capture after it,

  2. use ob_start(), ob_get_clean() to cache the output and then echo it after the file,

  3. best choice separate the file output into a separate php file, and load it via an iframe when needed.

you will still have problems forcing download in the same file, best bet is post a form with a target as the iframe, and run the file processing and download bit in the iframe, hide the iframe with css.

Im surprised you don't have headers already sent errors.

so

 <form method="post" action="location of file processing .php file" enctype="multipart/form-data" target="iframe" >
        *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English" />
        </form>

<iframe name="iframe" id="iframe" src="" style="display:none" ></iframe>

put this bit in the file that the form "action" is pointed at.

<?php
    if (isset($_FILES['file'])) {
        header('Content-Type: text/html; charset=UTF-8');
        require_once "simplexlsx.class.php";
        require '../../Arabic.php';
        $Arabic = new I18N_Arabic('Transliteration');
        $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
        $file="result.xls";
    //echo $_POST['submit1'];
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$file");
        echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
        $eng = array();
        list($cols,) = $xlsx->dimension();  
        foreach( $xlsx->rows() as $k => $r) {
    //      if ($k == 0) continue; // skip first row
            echo '<tr>';
            for( $i = 0; $i < $cols; $i++)
            {
                if ($_POST['submit2'] = 'Arabic to English')
                    $temp =  $Arabic->en2ar($r[$i]);
                else
                    continue;   
                echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
    }
    ?>

For your update, I'm not sure of that I was just answering the part about the output error, but I can tell you this is not correct

 if ($_POST['submit2'] = 'Enlish to Arabic')
     $temp =  $Arabic->en2ar($r[$i]);    
     echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';

it implies

 if ($_POST['submit2'] = 'Enlish to Arabic'){
     $temp =  $Arabic->en2ar($r[$i]);    
 }
 echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';

and

$_POST['submit2'] = 'Enlish to Arabic';
if ($_POST['submit2'] == 'Enlish to Arabic'){...

In which case you never set the $temp variable here, and $_POST['submit2'] is always equal to 'Enlish to Arabic'

you'll want this ( your also doing assignment with the single = which is always true )

if ($_POST['submit2'] == 'Enlish to Arabic'){
     $temp =  $Arabic->en2ar($r[$i]);    
     echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
}

Upvotes: 1

Related Questions