0

I am trying to save the input from a PHP form onto the action page.

So for example, the form is on /world/play/create/create.php, and onsubmit leads to /world/play/create/index.php?id=(6DIGIT#) the input of the username from the first URL would be echoed onto the action and saved to the page, so that any user can go back and see it.

However, when I run this code (below), it displays the username on the tab that submitted the form, but other users that go to the link can't see that username when they join; each person can only see their own username.

I'm trying to make it so that each person can see theirs input AND everyone else's, but right now it only shows their own.

Currently this is what I have so far:

The URL:

/world/play/create/index.php?id=M12345 

The form (on first link):

<form id="access" method="post" action="/world/play/create/index.php?id=" onsubmit="this.action=this.action.split('#')[0]+'M'+(Math.floor(Math.random()*(75800-39408))+30938);phpValue();">
<input autocomplete="off" type="text" name="nameP1" id="unm" placeholder="Username" value="<?php echo $nameP1;?>">
<br>
<div style="line-height:10px">&nbsp;</div>
<input type="submit" value="CONTINUE" onclick="return checkInput()">
</form>

The PHP (on second link):

    <?php
$nameErr = "";
$nameP1 = "";$nameP2 = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["nameP1"])) {
    $nameErr = "Name is required";
  } else {
    $nameP1 = test_input($_POST["nameP1"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$nameP1)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["nameP2"])) {
    $nameErr = "Name is required";
  } else {
    $nameP2 = test_input($_POST["nameP2"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$nameP2)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

And finally, the PHP echo (on second link):

 <span id="playerName"><?php echo $nameP1; ?></span>&nbsp;
3
  • unable to understand your question.What you are trying to achieve and most important what problem you are facing and in which code you are facing it? Commented Jan 2, 2018 at 4:54
  • What is the problem. Do you get any errors? Commented Jan 2, 2018 at 4:55
  • Sorry, I forgot to mention it. When I run this code, it displays the username on the tab that submitted the form, but then other users can't see that username when they join. Commented Jan 2, 2018 at 15:20

1 Answer 1

1

If you want to save the variables in the URL you should change the form method to GET instead of POST. Change the $_POST variable in your code on your second php file to $_GET.

More info : http://php.net/manual/en/tutorial.forms.php#37899

Sign up to request clarification or add additional context in comments.

1 Comment

My fault, I was unclear in my question, I've updated it for clarity.

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.