-3

I need to make a button which when clicked, will export all data from the HTML form on the page to an excel file. Could anyone help me with this I'm new to PHP and dont know where to begin.

        <div class="left col-md-12 form-group" >


            <form method="post" id="triallesson_form">
                <div class="table">

                <table class="table table-header-rotated table-condensed table-striped table-hover"  style="width: 100%;">
                    <th></th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_name']            ;?></span></div>  </th>

                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_telphone']        ;?></span></div>  </th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['location_name']          ;?></span></div>  </th>
                    <th class="col-xs-3 not-rotate"><div><span><?= text::$string['class_date']           ;?></span></div>  </th>

                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_type']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_email']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span>via</span></div>  </th>

                    <th class="rotate-45"><div><span><?= text::$string['triallesson_called']    ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_confirmd']  ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_show']      ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_sign_up']   ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_cancelled'] ;?></span></div>  </th>
                    <th class="rotate-45" title="<?= Text::$string['triallesson_sms_info'];?>"><div><span><?= Text::$string['triallesson_sms']       ;?></span></div>  </th>
                    <th></th>
                    <?= $content[1]; ?>
                </table>
                    <input type="hidden" name="date1" value="<?=$con->date1?>">
                <input type="hidden" name="date2" value="<?=$con->date2?>"></form>
        </div>
    </div>

Last time I didnt add the code sorry. This is the table where my data will be showed in. My intention is that when I click a button on the same page, it will take all the data from the table and export it into a XLS or CSV file.

If any further information is needed I'll add it, thankyou in advance.

  • What have you tried to do so. This is not the site where we will be giving free services. At least try something and show us the code where you got stuck, So that we can help you. – Sushank Pokharel Dec 21 '17 at 10:54
  • For hint see some jquery method. I use jquery to download data in excel. Try some jquery code to download, and if you got stucked come here with code so that we can help you. – Sushank Pokharel Dec 21 '17 at 10:57
  • php does not provide this type of special buttons. look here https://stackoverflow.com/questions/4249432/export-to-csv-via-php – TarangP Dec 21 '17 at 11:02
  • Could you add a bit more detail to your question? What are you wanting to capture - the stuff that's currently in input/textareas/selects etc? Are you wanting to do this when a 'submit' button is pressed (for example) - or are you wanting to store the information from a form into a database perhaps, and want then to export it from there? – ash Dec 21 '17 at 11:10
  • @doublesidedstickytape Hey, sorry I was a bit unclear. I edited my question. I hope its more clear now. – Giovanni Wijma Dec 21 '17 at 19:17

1 Answers1

0

You're using PHP to create the table but it might be simpler to use JavaScript to create the Excel file from the resultant HTML table as there are a variety of tools available to do this which are not too hard to use, such as: https://github.com/clarketm/TableExport

e.g. Using this as an example: https://tableexport.v3.travismclarke.com/examples/position.html for your table your code could look like this:

<div class="left col-md-12 form-group" >
    <form method="post" id="triallesson_form">
        <div class="table">
            <table id="myDataTable" class="table table-header-rotated table-condensed table-striped table-hover" style="width: 100%;">
                <thead>
                    <th></th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_name']            ;?></span></div>  </th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_telphone']        ;?></span></div>  </th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['location_name']          ;?></span></div>  </th>
                    <th class="col-xs-3 not-rotate"><div><span><?= text::$string['class_date']           ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_type']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_email']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span>via</span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_called']    ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_confirmd']  ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_show']      ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_sign_up']   ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_cancelled'] ;?></span></div>  </th>
                    <th class="rotate-45" title="<?= Text::$string['triallesson_sms_info'];?>"><div><span><?= Text::$string['triallesson_sms']       ;?></span></div>  </th>
                    <th></th>
                </thead>
                <tbody>
                    <?= $content[1]; ?>
                </tbody>
            </table>
            <input type="hidden" name="date1" value="<?=$con->date1?>">
            <input type="hidden" name="date2" value="<?=$con->date2?>">
        </div>
    </form>
</div>

<script type="text/javascript" src="../bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../bower_components/js-xlsx/dist/xlsx.core.min.js"></script>
<script type="text/javascript" src="../bower_components/blobjs/Blob.min.js"></script>
<script type="text/javascript" src="../bower_components/file-saverjs/FileSaver.min.js"></script>
<script type="text/javascript" src="../dist/js/tableexport.min.js"></script>
<script>
    var PositionTable = document.getElementById('myDataTable');
    new TableExport(PositionTable, {
        position: 'top'
    });
</script>

The only necessary change made to your HTML is adding an ID attribute to the table: <table id="myDataTable" ...

That's then referenced again in the JavaScript lower down: var PositionTable = document.getElementById('myDataTable');.

You'll need to update the locations of the included files to fit where you've got them (xlsx.core.min.js, Blob.min.js, FileSaver.min.js, tableexport.min.js, etc.). Remove the jQuery include if unneeded.

You'll then see Excel download buttons appear under your table just like in the example.

Jonathan
  • 454
  • 3
  • 5