<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</head>
<body>
<form name ="form1" method ="POST" action = "allNewPractice.php">
<input type = "text" value ="username">
<input type = "submit" name = "submit1" value = "login">
</form>
</body>
</html>
这个文件的名称,正如您可能看到的,是"allNewPractice.php",我使用localhost/allNewPractice.php直接通过我的浏览器访问它,而不是通过notepad++的运行。它不管用;它应该打印我在文本框中输入的信息到页面,但页面什么也不做。当我单击登录按钮时,页面只刷新,显示原始文本框和登录按钮,但不显示我输入的内容。我从http://www.homeandlearn.co.uk/php/php4p6.html得到了教程我做错了什么?我的电脑出问题了吗?
学习基本的HTML表单:
<input type = "text" value ="username">
没有名称的输入不提交任何内容。应该是
<input type="text" name="username" value="somevalue" />
^^^^^^^^^^^^^^^----must be present.
试试这个。就我个人而言,我似乎不使用post方法,但它的工作
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if ($_GET['username']) {
$user = $_GET['username'];
echo $user;
}else{
echo "no text";
}
?>
</head>
<body>
<form method ="GET" action = "allNewPractice.php">
<input id="username" name="username" type="text" placeholder="username">
<input type = "submit" id = "submit" value = "login">
</form>
</body>
</html>
首先,需要向输入框添加一个name
属性,如下所示:
<input type = "text" value ="username" name="username">
当您调用$_POST['username']
时,它引用的是name属性,而不是值。
第二,您在发布任何内容之前设置$username
的值。这将导致错误undefined variable: username
,因为第一次加载页面时$_POST['username']
不存在。直到提交表单之后它才会有一个值。因此,您需要先检查表单是否已提交:
if (isset($_POST['submit1'])) {
// Process data from form
}
这是你的程序的一个完整的工作版本:
<?php
if (isset($_POST['submit1'])) {
$username = $_POST['username'];
print ($username);
}
?>
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
<form name ="form1" method ="POST" action = "allNewPractice.php">
<input type = "text" value ="username" name="username">
<input type = "submit" name = "submit1" value = "login">
</form>
</body>
</html>