Get a variable from a PHP script in jQuery in another file. No echo?

I need help on how to get a variable from another PHP script in jQuery in my index.php file.

On my web page, I have the opportunity to search the database, and this is done by clicking the button that performs the search and displays the results in another div on the page.

This part of the webpage is working fine.

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

My jQuery in index.php looks something like this:

<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> 

How do I use the getData.php file to display my result in another div, I cannot execute echo $ result and use it in JSon ?? because it will also be displayed on my web page, which of course I do not know.

Is it possible somehow?

Thanks for any help. // Ambrose

Here is my code in getData.php if that helps:

  $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>"; } } 
+4
source share
2 answers

So, if I understood correctly, you want to get some data from the database into a txt file to load it, right?

Well, the first thing you want to do is to generate this data file, there are several ways to do it, but since the file can be generated each time with different data, IMHO you need to generate and submit it on the fly, and for this you only need to create a separate php script to get the data and set the appropriate headers for it.

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

After that, just use the printout of all the necessary data in a text file. eg.

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

will create the following.

enter image description here

enter image description here

enter image description here

Now, if you want to use jQuery, you can simply download it, mainly because depending on the browser, the file may get rendered.

So just name it in another window, like.

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

EDIT: I almost forgot ... Consider modifying your php code, its hints for SQL injections, as well as its bad practice for outputting html directly to php, this makes the code confusing, it's better to create an object, array or something also pass it a script to display it somewhere else.

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

 print $_POST["print"]; 

Then create a hidden form on your web page.

 <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, activate this:

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

This will send the form and open it in a new window. PHP will create a file with any text that you pass to it.

enter image description here

+3
source

The more I think about it, the more I think that I would echo to a hidden DIV that you do like this:

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

And then he grabbed him and did something like this with him.

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

All Articles