1

I need a solution for on click button call PHP function in the same PHP file, PHP function internally executes a Perl script as well as downloads a file using ftp. (my Perl script is executing and my ftp download works fine) only when I click button it's not calling PHP function). I found many other posting didn't find the solution which I am looking. Is there anything I am doing wrong?

Thank you in advance

below is my sample PHP code

<?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>
1
  • Have you used Ajax before? What about jquery? Also, do you know the difference between server-side and client-side? Commented Oct 6, 2017 at 3:02

1 Answer 1

1

The are a lot of things that you are doing wrong and I am feeling like you are not clear about PHP, jquery and even AJAX.

Since you want to send / retrieve POST data via AJAX on button click and without refreshing the page, you don't need the form element.

Instead, try to understand how Ajax works from the following

<?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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.