Php submitting form help

I have created a file called name.html and in this file i created a form which submit my first name and last name to a php file called name.php and in this file also am trying to output the names entered in the form to screen but when i submit the form it print all my php code to the screen without getting the names …
here is the name.php code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $t ?></title>
</head>
<body>
    <?php

      $name1 = $_GET['firstname'];
      $name2 = $_GET['lastname'];

      echo 'Welcome  '. $name1. ' '. $name2. '!';
     ?>
    
</body>
</html>

here is the name.html code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>first</title>
</head>
<body>
    <form action="hellop.php" method="GET">
        <p>
         <label for="firstname">Firts Name: </label>
         <input type="text" name="firtsname" id="firstname">
         </p>
         <p>
         <label for="lastname">Last Name:</label>
         <input type="text" name="lastname"id="lastname">
         </p>
         <input type="submit" value="SUBMIT">
    </form>
</body>
</html>

Iam using XAMPP

Try these tow web-pages instead:

name.html.php

<?php declare(strict_types=1); // this file only
// follwoing two validation checks are hereditary    
error_reporting(-1);
ini_set('display_errors', 'true');

$t = 'Just Testing';    

?><!DOCTYPE html><html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> <?= $t ?> </title>
</head>
<body>
    <h1> <?= $t ?> </h1>

    <form action="name.php" method="get">
        <p>
           <label for="firstname">First Name: 
            <input type="text" name="firstname" id="firstname" required>
           </label> 
         </p>

         <p>
           <label for="lastname"> Last Name: 
            <input type="text" name="lastname" id="lastname" required>
           </label>
         </p>
         <input type="submit" value="SUBMIT">
    </form>
</body>
</html>   

### name.php
<?php declare(strict_types=1);

$t = $t ?? '$t NOT SET ???';

$name1 	= $_GET['firstname']  ?? '$_GET["firstname"] NOT SET ???';
$name2  = $_GET['lastname'] 	?? '$_GET["lastname"]  NOT SET ???';


?><!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $t ?></title>
</head>
<body>
    <h1> <?= $t ?> </h1>

    <?php
      echo '
        <dl>
          <dt>  Welcome   </dt>
          <dd>' .$name1 .'</dd>
          <dd>' .$name2 .'</dd>
        </dl>
      ';

			echo '<pre> $_GET[] ==> '; 
				print_r($_GET); 
			echo '</pre';
     ?>
    
    <h3> 
      <a href="name.html.php"> Form </a>
  </h3>

</body>
</html>
  1. You spelled firstname wrong
  2. You should be using the POST method
  3. Make sure you are calling the files from the server. Like localhost/filename.
    htm
1 Like

Your PHP file is named name.php, but your form is submitting to hellop.php.

Other than that, as @benanamen asked, how do you open the html page? If you open it by clicking it in file explorer, that’s not going to work as it’s just the browser opening the page directly. You need to type the URL in your address bar, something like `http://localhost/name.html" or however you have your server configured.

1 Like

Yes, notice how John uses lowercase for the method value “get”.

1 Like