Hey there,i just started learning php and i'm having some problem in forms.
This is the code.

<html>

<form>
Enter username<br>
<input type=text name=username><br><br>
Enter password<br>
<input type=password name=pass><br>
<input type=submit value="Submit">
</form>

<?php


printf("Username is %s<br>", $username);

printf("Password is %s",$password);
?>

</html>

The problem in the code is that when i click the submit button,it shows that the two variables username and password dont have any values and have not been declared.
Please let me know about the problem.Thanks.

Hey there,i just started learning php and i'm having some problem in forms.
This is the code.

<html>

<form>
Enter username<br>
<input type=text name=username><br><br>
Enter password<br>
<input type=password name=pass><br>
<input type=submit value="Submit">
</form>

<?php


printf("Username is %s<br>", $username);

printf("Password is %s",$password);
?>

</html>

The problem in the code is that when i click the submit button,it shows that the two variables username and password dont have any values and have not been declared.
Please let me know about the problem.Thanks.

PHP doesn't create any variables by itself. You're trying to print $username and $password but never said what they were. That, and your form doesn't do anything.

<html>
  
       
  
      <form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
  
      Enter username<br>
   
      <input type=text name=username><br><br>
  
      Enter password<br>
  
      <input type=password name=pass><br>
   
      <input type=submit value="Submit">
  
      </form>
  
       
  
      <?php

       if (isset($_POST["submit"])) {

       $username = $_POST['username'];
       $password = $_POST['pass'];

      printf("Username is %s<br>", $username);

       

      printf("Password is %s",$password);
  }
      ?>

       

      </html>

BTW, no offense but that is some ugly coding. You don't have any header or meta information or even the body tag. Your form would never work without the action attribute. You also haven't closed anything besides the form and html tag. You might want to try to fix your coding practices or it will bite you hard later.

No idea about bad coding or good coding,mate. As i mentioned,i'm a total beginner in php.Never worked on anything related to web pages and thanks for the reply and criticism :)

Try this code

<html>

<form action="<?php echo $_SERVER; ?>" method="get">
Enter username<br>
<input type=text name=username><br><br>
Enter password<br>
<input type=password name=pass><br>
<input type=submit value="Submit">
</form>

<?php
//printf("Username is %s<br>", $username);
echo "Username is ".$_GET."<br>";
//printf("Password is %s",$password);
echo "Password is ".$_GET."<br>";
?>

</html>

Could please explain me the work of the line
<form action="<?php echo $_SERVER; ?>" method="get">

i know what form tag does but as i have less knowledge of form action. Explanation of this will be appreciated.Thanks.

Here a little information of the line

<form action="<?php echo $_SERVER; ?>" method="get">

This part of line

action="<?php echo $_SERVER; ?>"

simply tells that the content of the form's input fields will be send to this file


This part of line

method="get"

tell that web's protocol html uses GET-method in order to transfer this form's
data.

I hope this will help you. A good way to learn to use PHP is web site php.net
which is in address http://php.net.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.