如何使用php代码打印输入


How to print input with php code

我刚开始学习php和html,我想做一些东西,用php代码获取输入并打印输出。然而,使用我的代码,它允许我输入,但当我单击提交时不会打印任何内容。为什么?

<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit value="Submit" />
</form>
<?php
 if (isset($_POST['submit'])) { //to check if the form was submitted
 $username= $_POST['username'];
 echo $username;
}
?>
</body>
<html>

在代码的下面一行

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

word name="submit没有结束的双冒号

您的HTML在所在的行中缺少一个引号

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

应该是

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

您错过了按钮名称中的引号

<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit" value="Submit" />// you missed here
</form>
<?php
 if (isset($_POST['submit'])) { //to check if the form was submitted
 $username= $_POST['username'];
 echo $username;
}
?>
</body>
<html>

有一个语法错误,您错过了双引号name="submit"

    <?php
     if (isset($_POST['submit'])) { //to check if the form was submitted
    // For printing whole post data
     echo '<pre>'; print_r($_POST);
     $username= $_POST['username'];
     echo $username;
    }
    ?>

也可以使用var_dump进行打印。希望这对你有帮助。

您确定通过php解释器运行页面吗?在本地,您可以使用xampp或wampp(windows产品)在mac中,您可以使用mampp