Ambrose
Ambrose

Reputation: 163

Get a variable from a PHP script to JQuery in another file. Without echo?

I need some help with how to get a variable from another PHP script to a JQuery in my index.php file.

On my webpage I have the ability to search a database and this is done by the click of a button which performs the search and displays the results in another div on the page.

This part of the webpage work fine.

What I would like to do next is for another button to be able to download a text file with the results of my query. The result is calculated in another PHP file, the same one that displays it.

My JQuery in index.php looks something like:

<script>
$(document).ready(function(){
    $("#querySubmitButton").click(function(evt){
        evt.preventDefault(); // stops the submit taking place
        var myQuery = $('#queryText').val(); // get the query value
        $('#container').load('getData.php', { query:myQuery });
    });

    $("#selectButton").click(function(evt){
        evt.preventDefault();
        var selectQuery = $( "#selectBox" ).val();
        $('#container').load('getData.php', { query:selectQuery });
    });

    $("#downloadButton").click(function(evt){
        alert($result); //Somehow alert the $result variable from my getData.php file, to make sure that I can receive it and perform further things on it so that it is downloaded as a document. 
    });
});
</script>

As I use the getData.php file for displaying my result in another div, I can't echo $result and use it in JSon?? because that will also be displayed on my webpage, which I of course do not what.

Is this possible somehow?

Thank you for any help. //Ambrose

Here is my code in getData.php if that is of help:

 $conn = pg_connect("dbname=xxx user=xxxxxxx password=xxxxxxxx");

                    if (!$conn) {
                            die("Error in connection: " . pg_last_error());
                    }

                    $query =$_POST['query'];
                    $result = pg_query($query);
                    if (!$result) {
                            echo "Problem with query " . $query . "<br/>";
                            echo pg_last_error();
                            exit();

                    }
                    else{
                            if(empty($result)){
                                    echo"Tom";
                            }
                            else{
                            echo "<table class='resultTable'><tr>";
                            $i=0;
                            while ($i < pg_num_fields($result))
                            {
                                    $fieldName = pg_field_name($result, $i);
                                    echo '<td>' . $fieldName . '</td>';
                                    $i = $i + 1;
                            }
                            echo '</tr>';

                            while($row = pg_fetch_assoc($result)) {
                                    echo "<tr>";
                                    foreach ($row as $key => $value){
                                            echo "<td>" . $value . "</td>";
                                    }
                                    echo "</tr>";
                            }
                            echo "</table>";
                    }
                    }

Upvotes: 4

Views: 494

Answers (2)

Carlos Arturo Alaniz
Carlos Arturo Alaniz

Reputation: 428

Ok so, if I got this right, you want to get some data from DB into a txt file to download it, right?

Well the first thing you want to do is to generate that file with data, there's several ways to do it but since the file might be generated each time with different data, IMHO you should generated and serve it on the fly, and to do that the only thing you need to do is to create a separate php script to get get the data, and set the proper headers to it.

   header("Content-type: text/plain"); //File type
   header("Content-Disposition: attachment; filename=SomeFileName.txt"); //suggested file name

After this just use print out all the data you want in the text file. for example.

<?php
header("Content-type: text/plain"); //File type
header("Content-Disposition: attachment; filename=SomeFileName.txt");
print "hey I'm a text file!";

would generate the following.

enter image description here

enter image description here

enter image description here

Now if you want to use JQuery, you cant just load it, mainly because depending on the browser, the file might get rendered.

So just call it in a different window like.

$('#foo').attr({target: '_blank', 
                    href  : 'genfile.php'});

EDIT: I almost forgot... Consider changing your php code, its prompt to SQL injections, also, its bad practice to output html directly on php, it makes the code confusing, its better to create an object, array or whatever and pass it to a script to display it somewhere else.

EDIT 2 From the top of my head, create a print_text.php with the correct headers and just

print $_POST["print"];

Then in your webpage make a hidden form.

<form id="to_print" action="print_text.php" method="post" target="_blank">
  <input id="print_data" name="parameter" type="hidden" value="None">
</form>

Then in your button action hook this:

$('#print_data').val($("#where_ever_your_content_is").text());
$('#to_print').submit();

This will submit the form and open this in a new window, the .php will generate a file with whatever text you pass it .

Upvotes: 3

Michael McPherson
Michael McPherson

Reputation: 498

The more I think about this, the more I think I'd go with echoing it to a hidden DIV, which you do like this:

<div id="myHiddenValue" style="display: none;">

And then grabbing it and doing something with it like this

var myValue = $( "#myHiddenValue" ).text();

Upvotes: 1

Related Questions