drw85135 2018-01-02 04:51
浏览 156

将PHP表单输入保存到网页

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;
  • 写回答

1条回答 默认 最新

  • dongliuxia9495 2018-01-02 06:12
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题