【问题标题】:How to use jquery to handle a $GET method for viewing a xml file?如何使用 jquery 处理 $GET 方法来查看 xml 文件?
【发布时间】:2018-06-04 04:39:26
【问题描述】:

我正在使用这个问题jQuery Ajax POST example with PHP 来了解如何在提交表单之前处理表单中的数据。

我有一个没有 GUI 的应用程序,我围绕它构建了一个数据库和一个 web 应用程序。 此应用程序使用 fopen() 打开 xml 文件。 我正在使用 $GET 方法从数据库的列中获取 xml 文件及其路径。

<?php
$sql = pg_query($conn, "select link, identification from tbl_xml where date='$today';"));
?>
<form id="xmlform" name="xmlform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="GET">
<?php
while ($row = pg_fetch_row($sql)) {
echo "<button type='submit' name='xml' value='$row[0]' class='btn-as-link'>$row[1]</button>"
}
?>
</form>

我们在index.php中。

[...]
ELSE IF( isset($_GET['xml'] )){
    include_once("showXml.php");
    }
[...]

showXml.php

$url = filter_var($_GET['xml'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
[...]
// $url contains a really long path and the file. Each folder of the path creates exception on how to open the xml file, but in the end i simply open like this:
    // Opening the file
    $myfile = fopen("$xml", "r");
    $xml =  fread($myfile,filesize("$xml"));
    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = TRUE;

    // IF the file has XML format we will arrange it, if not we will print it raw
    IF ( $dom->loadXML($xml) ) {
      $dom->formatOutput = TRUE;
      $xml_out = $dom->saveXML();
      echo "<pre><code class='language-markup'>";
      echo htmlspecialchars($xml_out);
      echo "</code></pre>";
    } ELSE {
      $xml = str_replace('\'','\''.PHP_EOL,$xml);
      echo "<pre><code class='language-markup'>";
      echo htmlspecialchars($xml);
      echo "</code></pre>";
    }

    fclose($myfile);

我想使用 $GET 方法,因为 webapp 有一个时钟,页面每 X 分钟刷新一次。如果我使用 $POST,我会看到那个窗口告诉我“重新发送数据”。我不想那样。

我面临的问题是路径在 URL 中很明显,并且因为 webapp 将很快收到更新,它还将打开其他服务器中的 XML 文件,我正在寻找一种方法来维护 @ 987654326@ 但将表单从使用 $GET 方法转换为 jquery/ajax 或任何其他方式以将其 xml_file_path 提供给 showXml.php。

我添加了另一个问题的代码,我可以阅读“万岁,它成功了!”在控制台上。 我的变量$xmlform.php 内。

form.php

if (isset($_POST['xml']){
    $xml = isset($_POST['xml']) ? $_POST['xml'] : null;
}

从这里开始,我不清楚如何触发:

ELSE IF( isset($_GET['xml'] )){
    include_once("showXml.php");
    }

index.php 中,打开变量$xml 不是来自$GET 方法,而是来自$POST 内部使用的form.php

【问题讨论】:

  • 我不知道你怎么能没有 gui 但有一个按钮?
  • 这是一个运行在 Ubuntu 服务器上的 java 应用程序。应用程序本身没有 GUI。它根据谁向我发送了文件来创建文件夹。我建立了一个数据库,并且太多的 bash 脚本无法在数据库中注册所有发送和接收的文件。 webapp 向我展示了这些文件
  • @SergeK.,好吧,不是真的。我使用 include 是因为这是我很久以前对网站进行编程的方式。现在我必须在同一个服务器之外打开 xml 文件,我仍然可以使用 include 和 $GET,但是你能想象这个 URL:index.php?xml=100.100.100.99:5050/rootfolder/basefolder/anotherfolder/evenonemore/dateoftoday/xmlfile.xml 我确定有一个正确的方法,我只是不知道):
  • $.get('100.100.100.99:5050/rootfolder/basefolder/anot‌​herfolder/evenonemor‌​e/dateoftoday/xmlfil‌​e.xml', ... ?

标签: php jquery fopen


【解决方案1】:

我不是 100% 相信我已经掌握了这个问题,但如果您想使用 ajax 发送请求(使用 GET ),也许以下内容可能会提供一些指导。

使用 ajax 时,标准按钮可以正常工作,而不是使用提交按钮 - xml 文件的详细信息保存在按钮 data-value 属性中,并作为 ajax url 的一部分发送。

根据需要使用回调函数处理响应 - 这只会弹出带有响应数据的警报。

<?php
    $sql = pg_query( $conn, "select `link`, `identification` from `tbl_xml` where `date`='$today';"));
    $bttns = array();
    while( $row = pg_fetch_row( $sql ) )$bttns[]="<input type='button' data-value='{$row[0]}' value='{$row[1]}' />";



    echo "<!-- render basic form with all buttons -->
        <form name='xmlform'>
        " . implode( PHP_EOL, $bttns ) . "
        </form>";
?>




<script>
    var url='showxml.php';
    var evtcallback=function(xml){
        alert(xml)
    };

    function ajax( url, callback, payload ){
        var xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                if( this.status==200 && this.readyState==4 )callback.call( this, this.response );
            };
            xhr.open('GET', url + '?xml='+payload, true );
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.send(null);
    }

    var col=Array.prototype.slice.call( document.forms.xmlform.querySelectorAll('input[type="button"]') );
        col.forEach(function( bttn ){
            bttn.addEventListener('click', function(event){
                ajax.call( url, evtcallback, this.dataset.value );
            }.bind( bttn ),false );
        });
</script>

【讨论】:

  • 谢谢!我印象深刻。不确定我是否能够理解所使用的每一段代码。我会试试看。让我先投票,因为我真的很感谢你的回答!
  • Uncaught TypeError: Cannot read property 'querySelectorAll' of undefined at index.php?... 我不能使用$bttns = array();,也不能使用. implode( PHP_EOL, $bttns ) .。不知道是不是这里的问题,如果是的话,请问您是否可以修改代码以匹配我的代码?我知道我的不好,很抱歉 :D 但while() 的内容比我在示例中显示的要多。
  • 有一个错误 - 应该是 document.forms.xmlform ~ 希望可以清除该错误消息。为什么不能使用$bttns=array();等?
  • 因为构建表我使用了 4 个不同的 SQL。 1 是第一个,我发布的那个,它不仅获得“链接”,还获得其他 6 个列,其中一些列与其他 SQL 一起处理,以便在主表的每个 &lt;tr&gt; 内嵌套另一个表. java应用程序发送和接收文件,每个文件有1个或多个回复。 SQL 协同工作以动态构建表。这是一个非常糟糕的环境,但我能做到的最好。我用document.forms.xmlform 修复了但仍然是同样的错误:/
  • 主表(this)有数千条记录(我使用分页来显示它们),"filename; date; link; sender; receiver; status" - 每行都有一个嵌套表,其中包含$SQL3 的值:@987654333 @。我使用xmlform 打开主表中的链接和打开嵌套表中的链接。一个是发送的文件,另一个是接收的。真的很长,我很想解释一下我是如何做到的,因为我不是网络开发人员(我曾经是),但不能。我很容易地转换了你写的内容以匹配我的代码,但错误仍然存​​在
【解决方案2】:

不是最好的方法,但它是有效的。

我将表单转换为$_POST。将$_POST['xml'] 的值分配给$_SESSION['xml'] 并使用它打开文件。打开文件后清空$_SESSION['xml']。网址是干净的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2014-09-04
    • 2012-12-26
    • 1970-01-01
    • 2011-06-03
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多