0

I have followed html code:

<html>
    <head>
    </head>
    <body>
        <input type="text" name="username">
        <input type="submit" name="submit">
    </body>
</html>

And my PHP code is:

<?php
    $username = $_GET['username'];
?>

Why I cant get value from input to variable?

Marty1452
  • 430
  • 5
  • 19
Jacob
  • 103
  • 7

2 Answers2

0

You missing form

Add to your code

<form action="#" method="get">
//your input and PHP code goes here
</form>
D3nny
  • 24
  • 3
0

document.forms["myform"].onsubmit=function(){
console.log('submitting');
return false;
};
<html>

<head>
</head>

<body>
  <form id="myform" name="myform">
  </form>
  <input form="myform" type="text" name="username">
  <input form="myform" type="submit" name="submit">
</body>

</html>

If for some reason the OP prefers to place the inputs outside the form that's permissable with HTML5 and should allow the form submission to still occur as long as each input bears a form attribute.

When the form is submitted, you need PHP to check if the form submission is valid by writing code like the following:

<?php
 if ( isset( $_POST["submit"] ) )  {
       // process the form submission
 }

OR:

 if ( isset( $_GET["submit"] ) ) {
       // process the form submission
 }
slevy1
  • 3,797
  • 2
  • 27
  • 33