简体   繁体   中英

Executing PHP script with button in Javascript

Here's the break down: My problem is two fold, i want to use a button to call to my php script, which in turn calls to my nodejs script running on my little internal test server (this is all done local for now).

Here is my html + script pull:

      <input type="submit" value="Save Sketch" id= "run_system">



  </body>
 <script>
  $('#run_system').on('click', function(){
    $.ajax({
      url : 'exec.php',
      type : "GET"
    }).done(function(data){
      console.log(data);
    });

  });
 </script>

and here is my php script:

 <?php exec('node screenshot.js //place holder url that gets dumped into js file// "); ?>

so my problem is that the previous call to this php is not executing this. Everything i've googled says that this exec("") should run my nodejs script. What am i missing?

I am guessing the type : GET is incorrect, tried not having that line, putting POST in there, nothing seems to work though

Oh one more addition, so when i run the above all it does is print what was in the php file to the console, doesn't actually run the script

One of the issues is that the exec is executing the command, but you're not capturing / returning the output of that command. You can probably add an echo or print before exec to return the output to the AJAX request.

You might also want to bind to the submit event to handle form submissions as well as submit button clicks, but that would be bound to the form itself.

Ultimately though, I would recommend that you contemplate handling this exec by adding a simple HTTP server to your screenshots.js node script (or something that wraps it) and handle the AJAX request with that. Will likely need to add a Access-Control-Allow-Origin exemption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM