gpt4 book ai didi

javascript - 按钮点击调用PHP函数

转载 作者:行者123 更新时间:2023-12-03 03:20:31 25 4
gpt4 key购买 nike

我需要一个解决方案,用于在同一 PHP 文件中单击按钮调用 PHP 函数,PHP 函数在内部执行 Perl 脚本并使用 ftp 下载文件。(我的 Perl 脚本正在执行,我的 ftp 下载工作正常)只有当我单击按钮时,它才不会调用 PHP 函数)。我发现许多其他帖子都没有找到我正在寻找的解决方案。我是不是做错了什么?

提前谢谢

下面是我的示例 PHP 代码

<?php
function getFile(){
exec("/../myperlscript.pl parameters");//
//some code for ftp file download( which is working )
}
<!--
if(isset($_POST['submit'])){
getFile();
}
-->
?>
<script src="https://code.jquery.com/jquery-1.11.2.min.js">
</script>
<script type="text/javascript">
$("form").submit(function() {
$.ajax({

type: "POST",
sucess: getFile
});
});
</script>
<form method="post">
<input type="button" name="getFile" value="getFile">
</form>

最佳答案

你做错了很多事情,我感觉你对 PHP、jquery 甚至 AJAX 都不清楚。

由于您希望在单击按钮时通过 AJAX 发送/检索 POST 数据并且无需刷新页面,因此不需要表单元素。

相反,尝试从以下内容了解 Ajax 的工作原理

<?php
function getFile($filename) {
echo "working, contents of $filename will be displayed here";

//terminate php script to prevent other content to return in ajax data
exit();
}

if (isset($_POST['getfile']) && $_POST['getfile'] === "true") {
getFile($_POST['filename']);
}
?>

<script src="https://code.jquery.com/jquery-1.11.2.min.js">
</script>
<script>
$(document).ready(function(){
$("#getFile").click (function(){
$.post("index.php", // current php file name
{
// post data to be sent
getfile: "true",
filename: "file1"
},
function(data, status){
// callback / function to be executed after the Ajax request
$("#fileContent").text(data);
});
});
});
</script>

<button id="getFile">Get File</button>
<p id="fileContent"></p>

关于javascript - 按钮点击调用PHP函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46597348/

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