0

I have a php page to creates a multi-dimentional array called $results.

I would like to:

  1. catch submit of a form button
  2. override default behavior of the submit using jQuery
  3. copy and process $results on separate php using $.post

I have this which is not currently working and am not sure why?:

<form id='download_to_excel' method="post">
    <input type="image" name="submit" value="submit" id='xls_download_button' src='images/common/buttons/download.png'> 
</form>

<?php 
    $json_results = json_encode($results);
?>

<script type='text/javascript'>
    $(document).ready(function(){ 
        alert($json_results);
        $("#xls_download_button").click(function(e){ 
            alert('clicked');
            e.preventDefault(); 
            download_xls(); 
        }); 

        function download_xls(){
            $.post('./libs/common/export_data_to_excel.php', {json_data : json_results};
        }
    }); 
</script>

When selecting the xls_download_button, the alert() never fires nor does any data get passed to export_data_to_excel.php

The export_data_to_excel.php file has the following:

<?php 
$results = json_decode($_POST['json_data']);
#include the export-xls.class.php file
require_once('export-xls.class.php');
$date = date('Y-m-d');
$filename = "contacts_search_$date.xls"; // The file name you want any resulting file to be called.
#create an instance of the class
$xls = new ExportXLS($filename, $results);
#lets set some headers for top of the spreadsheet
$header = "Searched Contact Results"; // single first col text
$xls->addHeader($header);
#add blank line
$header = null;
$xls->addHeader($header);
$header = null;
$row = null;
foreach($results as $outer){
   // header row
   foreach($outer as $key => $value){
     $header[] = $key; 
   }
  // Data Rows
  foreach($outer as $key => $value){
    $row[] = $value;
  }
  $xls->addRow($header);//add header to xls body
  $header = null;
  $xls->addRow($row); //add data to xls body 
  $row = null;
} 
# You can return the xls as a variable to use with;
# $sheet = $xls->returnSheet();
#
# OR
#
# You can send the sheet directly to the browser as a file 
#
$xls->sendFile();
?>

I do know that the $json_results does display proper JSON encoded values when echoed. But from there are not sure why the rest of the javascript does not run; the alerts never fire nor does the JSON data get passed?

Can you see why this isn't working?

5 Answers 5

1

Your PHP-supplied json is not stored as a javascript variable in your js.

$(document).ready(function(){ 
    var json_results = <?php echo $json_results; ?>;

...
Sign up to request clarification or add additional context in comments.

Comments

1

This code shouldn't run:

function download_xls(){
$.post('./libs/common/export_data_to_excel.php', {json_data : json_results};
}

It is invalid (the ; doesn't belong there). Try this code:

function download_xls(){
  $.post('./libs/common/export_data_to_excel.php', {json_data : json_results});
}

5 Comments

Thanks @Blender, I changed this as you suggested... Looking in firebug the json_data is set however nothing seems to happen when it pushes it to the export_data_to_excel.php. I have checked the file path and it is correct. I tried doing $results = json_decode($_POST['json_data']); and don't get anything? any thoughts?
How can you tell that you aren't getting anything back? Your PHP file doesn't return anything.
Unfortunately, because I am a new user I can't answer with a post but the export_data_to_excel.php is supposed to use the ajax passed data and create an excel file with it; nothing happens though.
var_dump($results) in export_data_to_excel.php doesn't dump the variable to screen, but then again, I don't know if this should being asynchronisly called or not?
I added the full code that is in the export_data_to_excel.php file in the original post; it let me do that at least. This part of the script doesn't seem to run or throw any errors?
1

Right now you are just setting a php variable called $results you need to transfear it to you javascript.

<script type="text/javascript">
// set javascript variable from php
var $results = "<?php echo json_decode($json_data); ?>";
</script>

Comments

0

For sure you have an error in your javascript code (you were not closing the parenthesis after $.post), should be:

$(document).ready(function() {
    alert($json_results);
    $("#xls_download_button").click(function(e) {
        alert('clicked');
        e.preventDefault();
        download_xls();
    });

    function download_xls() {
        $.post('./libs/common/export_data_to_excel.php', {
            json_data: json_results
        });
    }
});

Then you should assign your JSON to a javascript variable inside document.ready

$(document).ready(function() {
    var json_results = <?php   echo($json_results);?>;

3 Comments

I changed it as your recommended and now the alert('clicked'); runs but it doesn't seem that the export_data_to_excel.php runs as the $.post() does not seem to work? Is Viruzzo correct that I can't pass it this way?
@ServerStorm i'm sorry i'm going home, look at what blender wrote. You can pass the variables from php to javascript, no problem
Thank for all the help! Still not working but I am trying my best to debug!
0

You can't pass a PHP variable to the JavaScript like that: there live in totally different worlds. Use Ajax to get the JSON data from JS.

3 Comments

Others seem to contradict your comment see @Nicola Peluchetti comments. Thanks for your response though.
Actually, they don't: $json_results = json_encode($results); is not sufficient because the variable is only present in PHP's scope; with this var json_results = <?php echo($json_results); ?>; you are "exporting" it to JavaScript by printing it in the HTML page (that is, client-side where JS is executed). While you can do this and not use Ajax, it's a worse solution since you are embedding you whole JSON inside the HTML page received by the broser: more bandwith usage (possibly), no caching, may break depending on content.
I see. Sorry for misunderstanding! I get your explanation however am unclear how to do what you suggest. I am looking at many examples on the web and hopefully I will see one that does what you suggest. Regards Steve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.