Reputation: 37
<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>
The name of this file, as you can probably tell, is "allNewPractice.php" and I use localhost/allNewPractice.php directly through my browser to access it, not through notepad++'s run. it doesn't work whatsoever; its supposed to print the information I typed into the text box to the page but The page does NOTHING.When I click the login button the page only refreshes, shows the original text box and the login button, but doesn't show what i entered. I got the tutorial from http://www.homeandlearn.co.uk/php/php4p6.html what am i doing wrong? Is there something wrong with my computer?
Upvotes: 0
Views: 267
Reputation: 7447
First, you need to add a name
attribute to the input box, as such:
<input type = "text" value ="username" name="username">
When you call $_POST['username']
, it is referencing the name attribute, not the value.
Second, you are setting the value of $username
before anything has been posted. This will result in an error, undefined variable: username
, because $_POST['username']
does not exist when you first load the page. Not until after you submit the form will it have a value. So, you need to check if the form has been submitted first:
if (isset($_POST['submit1'])) {
// Process data from form
}
Here is a complete and working version of your program:
<?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>
Upvotes: 0
Reputation: 1
try this. personally i just dont seem to use the post method but it works
<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>
Upvotes: 0
Reputation: 360712
Learn basic HTML forms:
<input type = "text" value ="username">
Inputs with no name do not submit anything. It should be
<input type="text" name="username" value="somevalue" />
^^^^^^^^^^^^^^^----must be present.
Upvotes: 2