4

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>";
                    }
                    }
5
  • you are so vulnerable to sql injection attacks it's not even funny. what's the url for your site? I'd like to search for revoke all privileges from current_user; Commented Aug 4, 2015 at 14:30
  • Well, instead of doing that.. you could tell me how to make it more secure :P Commented Aug 5, 2015 at 6:09
  • or you could click the link I provided and read for yourself... Commented Aug 5, 2015 at 13:57
  • @Ambrose, have either of these answers told you what you want? They both suggest different ways of getting what you asked for. If they don't, you should clarify your question, and if they do, you should accept one of them. Commented Aug 6, 2015 at 12:32
  • I solved it in another way. Actually more or less like Carlos edited answer, so I will accept that one. Commented Aug 7, 2015 at 5:32

2 Answers 2

3

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 .

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

1 Comment

Well. The thing is that I want to be able to get the current value which contains my database results as it can be changed by 2 different sources. For example a person can choose to display a certain query, and once it is being displayed they can should be able to click the Download Results button to download it as a text file. I don't understand how to do this by using the answer you have given.
1

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();

Comments

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.