从另一个文件中获取PHP脚本到JQuery的变量。没有回声?

时间:2015-08-04 14:25:31

标签: php jquery html

我需要一些帮助,如何从我的index.php文件中将另一个PHP脚本的变量转换为JQuery。

在我的网页上,我可以搜索数据库,只需点击执行搜索的按钮即可完成,并在页面上的另一个div中显示结果。

这部分网页工作正常。

接下来我要做的是另一个按钮,可以下载包含查询结果的文本文件。 结果在另一个PHP文件中计算,与显示它的文件相同。

index.php中的我的JQuery看起来像:

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

当我使用getData.php文件在另一个div中显示我的结果时,我无法回显$ result并在JSon中使用它?因为那也会显示在我的网页上,我当然不会这样做。

这有可能吗?

感谢您的帮助。 //安布罗斯

这是我在getData.php中的代码,如果有帮助的话:

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

2 个答案:

答案 0 :(得分:3)

好的,如果我说得对,你想从DB中获取一些数据到txt文件下载,对吗?

您要做的第一件事就是使用数据生成该文件,有几种方法可以做到这一点,但由于每次使用不同的数据生成文件,恕我直言,您应该生成并提供它苍蝇,要做到这一点,你唯一需要做的就是创建一个单独的PHP脚本来获取数据,并为其设置正确的标题。

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

在此之后,只需使用打印出文本文件中所需的所有数据。例如。

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

将生成以下内容。

enter image description here

enter image description here

enter image description here

现在,如果你想使用JQuery,你就不能加载它,主要是因为根据浏览器的不同,文件可能会被渲染。

所以只需在不同的窗口中调用它。

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

修改 我差点忘了...考虑改变你的PHP代码,它提示SQL注入,还有,直接在php输出html的不良做法,它使代码混乱,更好地创建一个对象,数组或其他什么,并将其传递给一个脚本,可以在其他地方显示它。

编辑2 从头顶开始,使用正确的标题创建一个print_text.php,然后只需

print $_POST["print"];

然后在您的网页上制作一个隐藏的表单。

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

然后在你的按钮操作中挂钩:

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

这将提交表单并在新窗口中打开,.php将生成一个文件,其中包含您传递的任何文本。

答案 1 :(得分:1)

我越想到这一点,我就越想回到隐藏的DIV,你会这样做:

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

然后抓住它并像这样做一些事情

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