1

My Code:

<html>
<head>
<title>A BASIC HTML FORM</title>

<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
<Input Type = "text" Value ="<?PHP print $username ;?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>

Problem: The PHP code gets displayed in the textbox instead of the actual value

Demonstration

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Binoj
  • 11
  • 1
  • 5

3 Answers3

3

That syntax is correct (albeit vulnerable to XSS).

If it doesn't work, then you aren't using a server that supports PHP for the file you are loading.

PHP, on the WWW, only works when the page is accessed:

  • Through a web server
  • That has PHP installed
  • That knows that the file you are loading contains PHP and should be processed by the PHP engine (this is usually done by giving it a .php file extension).

Update: You have edited your question to include a screen grab. This confirms this answer. You are trying to load the PHP from your file system directly into your browser (with a file:/// URI). You need to access it through a web server (with an http:// URI).


As @CORRUPT points out in a comment. You don't give $username a default value, so you will get an error if the POST data isn't set. I suggest setting it to an empty string before the if (isset($_POST['Submit1'])) { line.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Try Below code:

<html>
<head>
<title>A BASIC HTML FORM</title>

<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
}else{
$username='';
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
<Input Type = "text" Value ="<?PHP print $username ;?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>
Arun Kumar
  • 36
  • 4
0

Write this function in the php block:

function prepopulate($name) { 
 if(isset($_POST[$name])) { 
  return $_POST[$name]; 
 } 
 else { 
  return ""; 
 } 
} 

and then in the HTML part

<FORM NAME ="form1" METHOD ="POST" ACTION = "">
<input name="fname" type="text" value="<?php echo prepopulate('fname');?>">
<input name="lname" type="text" value="<?php echo prepopulate('lname');?>">
</FORM>

You just have to pass the name of the textbox to the function prepopulate to retain it after form submit.

Aragorn108
  • 53
  • 5