0

Code is at the bottom. (if the question is not formulated correctly please tell me)

I want to write to a json file using javascript and PHP to create a multiplayer game of rock paper scissors. I'm very new to coding in js and PHP so I'll try and formulate as well as possible. In the json file there is an empty string, which should be the user's choice. I want my javascript and PHP file to write the user's choice in there but I have no clue on how to do so. If someone is able to help it would be very appreciated.

{
  "player1": {
      "choice" : ""
  },
    "player2": {
        "choice": ""
    }
}
$(function() {
    if ($("#rock").click( function()
        {
            alert('rock clicked'); // make instructions to send to json.
        }
    ));
});

$(function() {
    if ($("#paper").click( function()
        {
            alert('paper clicked');
        }
    ));
});

$(function() {
    if ($("#scissor").click( function()
        {
            alert('scissor clicked');
        }
    ));
});
<?php
$page_title = 'WP21';
$navigation = Array(
    'active' => 'Play',
    'items' => Array(
        'Play' => 'index.php',
        'About' => 'about.php'
    )
);
include __DIR__ . '/tpl/head.php';
include __DIR__ . '/tpl/body_start.php';
?>

<script type="application/javascript" src="main.js"></script>

<div class="pd-40"></div>
<div class="row">
    <div class="col-md-12" id="news_container">
        <button id="rock">Rock</button>
        <button id="paper">Paper</button>
        <button id="scissor">Scissor</button>
    </div>
    <p id="keuze"></p>
</div>


<?php
include __DIR__ . '/tpl/body_end.php';
?>
  • Well, I'm just unsure on how to specifically fill the empty string. – fat mitchel 38 Jul 10 '21 at 12:45
  • 2
    Rule 1 of programming: Break the Problem Down. Forget about JavaScript, forget about HTML, forget about the game you're trying to build; find out how to write a file with PHP, and find out how to create a JSON string with PHP; then think about how to put those together. Then, once you've got the PHP code working, you can find out about how to trigger that PHP code from the browser, and how to start building up more complex logic to make your game. – IMSoP Jul 10 '21 at 13:26

0 Answers0