0

I am trying to create a multiplayer game where the host will click a button that generates a unique link. They will then be able to send this link to all of the other players that they want to play with. This will allow the other players to join the same session.

My form looks like this:

        <form action="includes/host.inc.php" method="post">
          <button type="button" onclick="show()" name="Host">Host</button>
        </form>
        <div id="share" style="display:none;">
          <!-- The text field -->
          <input type="text" value="Link..." id="myInput">

          <!-- The button used to copy the text -->
          <button onclick="myFunction()">Copy text</button>
        </div>

You can see the action on the form refers to an external PHP file that is supposed to check whether to see the user has clicked the 'Host' button, and if so generates a random key which will be used as part of the unique link and sent to a database.

At the moment I want it to simply echo the random string generated but I'm not even getting much luck with that.

The code for the php file is below:

<?php

if (isset($_POST['Host'])) {

  function generateKey() {
    $randStr = uniqid();
    return $randStr;
  }

  echo generateKey();

}
else {
  echo "didnt work";
}

After clicking the 'Host' button in the form just nothing happens at all and I'm not sure why?

enter image description here

Here you can see my file structure. I think I'm referring to the external PHP file correctly.

If anyone could help me I'd really appreciate it!

Madamot
  • 81
  • 9
  • 1
    `` where is `show()`, in any case a button will not submit a form unless it's `type="submit"` OR you do it with JS after clicking the button. – ArtisticPhoenix Mar 04 '19 at 23:17
  • @ArtisticPhoenix Wow thank you that pretty much solved it! The only issue I'm having now is I'm being redirected to host.inc.php where it shows me the random string there instead of in my original page? – Madamot Mar 04 '19 at 23:22
  • 1
    That's the expected behavior with this action `action="includes/host.inc.php"` You can save it in the session, do a `header('location: ...'); exit();` redirect to the page, then pull it from the session and unset it. Or use AJAX (to do it without reloading the page) or pass it in the URL etc. Most of these would take some explaining. – ArtisticPhoenix Mar 04 '19 at 23:25
  • 1
    @Madamot because that is exactly what action="includes/host.inc.php" is supposed to do: action sets where the form is sent. – stravanato Mar 04 '19 at 23:25
  • 1
    As a prerequisite, I would suggest reading this https://stackoverflow.com/questions/13200152/why-is-it-said-that-http-is-a-stateless-protocol or anything about HTTP and maintaining state. – ArtisticPhoenix Mar 04 '19 at 23:27

1 Answers1

0

Change

<button type="button" onclick="show()" name="Host">Host</button>

To

<button type="submit" name="Host">Host</button>

Not sure what this onclick="show()" does as it's not included in the question. I guess you could submit the form via Javascript or something there.

A button (type button) is just a UI element, it doesn't submit the form.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38