samedi 8 décembre 2018

Let PHP take data from jQuery (and the opposite)

I am trying to implement a feature into my online console program. I would like to be able to type in a command, have jQuery send it to a PHP file, and have the PHP file return a response back to jQuery (to be outputted by the console). I want to have this communication so that a user can not just pull up the js in the page's source and find all of the secret commands / easter eggs :). I am aware that this probably has to do with AJAX (because I want the user to stay in the console and not be redirected), although I have not been able to find a solution that works at all. Am I going to need a form or a hidden submit button? Or both? Should I require the php file in the html, or only reference it in the jQuery? I feel like I have tried everything. Here is a very simplified version of my code. Any other tips / improvements would be appreciated. Thanks in advance! (BTW : I do not need any help outputting text to the console, I already have that figured out... I just need the communication between the jQuery and PHP).

$(document).ready(function() {
  $(document).keypress(function(e) {
    if (e.which == 13) {
      cmd = $("input").val();
      $('#submitted').append('<span class="green">> </span>' + $('#input').val() + '<br>');
      $('#input').val('');
    }
  });
});
html {
  background-color: #000000;
  font-family: Menlo;
}

.console {
  color: #ffffff;
}

.green {
  color: #39ff14;
}

input {
  background: transparent;
  border: none;
  color: #ffffff;
  font-family: Menlo;
  font-size: 15px;
}

p {
  color: #ffffff;
}
<!DOCTYPE html>
<html>
        <link rel="stylesheet" type="text/css" href="theme.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="console.js"></script>
<div class="console">
  <div id="submitted"></div>
  <span class="green">> </span>
  <input type="input" name="input" id="input" autocomplete="off"><br>
</div>
</html>



Aucun commentaire:

Enregistrer un commentaire