2017-10-06 186 views
0

嗨我需要一个解决方案,在同一个PHP文件中点击按钮调用PHP函数, php函数实习生执行Perl脚本以及使用ftp下载文件。 (我的Perl脚本执行和我的FTP下载工作正常)只有当我点击按钮它不是调用PHP函数)我发现很多其他帖子没有找到我正在寻找的解决方案。有什么我做错了。 预先感谢您按钮点击呼叫PHP功能

下面是我的样本PHP代码

<?php 
    function getFile(){ 
     exec("/../myperlscript.pl parameters");// 
     //some code for ftp file download(wich 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> 
+0

是如何工作的,你以前使用过阿贾克斯?那么jquery呢?另外,你是否知道服务器端和客户端之间的区别? –

回答

1

的很多东西,你做错了,我的感觉就像你看不清PHP,jQuery和甚至AJAX。

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

相反,尝试了解阿贾克斯从以下

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