无法查看从.html发送到.php的变量

时间:2016-05-30 03:37:39

标签: php html

我试图将表格数据从我的html页面发布到PHP,但我无法看到我发布的数据。请帮助。这是我的代码:

html文件的代码:

<!doctype html>

<html>
  <head>
    <title>
      Intro Page.
    </title>
  </head>

  <body>

    <form action="receive.php" method="POST">


      Name:<input type="text" name="username">
      Password:<input type="text" name="password">
      <input type="submit" value="submit">
  </body>
</html>

我的receive.php文件的代码是:

<?php

$name=$pass="";

$name=$_POST["username"];
$pass=$_POST["password"];



if($_SERVER["REQUEST_METHOD"]=="POST")
{

  $name= test_input($_POST["username"]);
  $pass= test_input($_POST["password"]);
}

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

echo $name;
echo $pass;

?>

4 个答案:

答案 0 :(得分:0)

我首先更改您的表单提交按钮以获得名称:

<input type="submit" name="submit" value="submit>

允许您将PHP修改为:

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

$name=$pass = '';

if(isset($_POST['submit'])) {
    $name = test_input($_POST['name']);
    $pass = test_input($_POST['pass']);
}

echo $name;
echo $pass;

您可能会收到您不了解的错误。在调试/编写代码时,最好通过在PHP脚本的顶部添加以下内容来启用错误报告:

<?php
ini_set('display_errors', 1);
error_reporting(-1); // or E_ALL

答案 1 :(得分:0)

1)检查php安装是否正确

2)知道该文件位于apache根目录下

测试:

直接打开receive.php并检查第一个php是否工作。

<强> HTML

<input type="submit"  name="form1_submit" value="submit">

根据@kunruh提到

You are missing your closing </form>

<强> PHP

<?php

$name=$pass="";

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

//you have to check the value post like because some times more than one form in your html means it make conflict so you have to use name isset .

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

    $name= test_input($_POST["username"]);
    $pass= test_input($_POST["password"]);

   echo $name;
   echo $pass;
  }

 ?>

答案 2 :(得分:0)

确保将receive.php文件放在html文件附近。例如:

如果html文件位于以下路径/var/www/html/YOUR_HTML_FILE.html

然后receive.php文件也应放在同一路径/var/www/html/receive.php中。否则,请在表单操作<form action="/var/www/html/receive.php" method="POST">中提及正确的路径。同时将密码字段从文本更改为密码

Text field - <input type="text" name="password">
<br>
Password field - <input type="password" name="password">

答案 3 :(得分:0)

尝试这样的事情......

Collections.sort(resultList, new Comparator<Item>() {
    @Override
    public int compare(Item lhs, Item rhs) {
        return lhs.getDistance().compareTo(rhs.getDistance());//mungkin valuenya null
    }
});

和“welcome_get.php”如下所示:

   <html>
<body>

<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>