mardi 4 août 2015

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>";
                    }
                    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire