gpt4 book ai didi

php - 如何使用 jquery 处理用于查看 xml 文件的 $GET 方法?

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:12 24 4
gpt4 key购买 nike

我正在使用这个问题 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 文件,我正在寻找一种方法来维护 echo "<button type='submit' name='xml' value='$row[0]' class='btn-as-link'>$row[1]</button>"但是将表单从使用 $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

最佳答案

我不是 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>

关于php - 如何使用 jquery 处理用于查看 xml 文件的 $GET 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47926031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com