-2

I tried to execute the following html code:

<html>
 <head>
 <title>Listing 9.1 A simple HTML form</title>
 </head>
 <body>
 <form action="listing.php" method="POST">
 <p><strong>Name:</strong><br>
 <input type="text" name="user"></p>
 <p><strong>Address:</strong><br>
 <textarea name="address" rows="5" cols="40"></textarea></p>
 <p><input type="submit" value="send"></p>
 </form>
 </body>
 </html>

And this is the code of the associated php file (listing.php):

     <html>
 <head>
 <title>Listing  Reading input from a form </title>
 </head>
 <body>

 Welcome <?php echo $_POST["user"]; ?><br>
 Your email address is: <?php echo $_POST["address"]; ?>

 </body>
 </html>

I was able to get the form and enter values as shown below: Form Input

But, when I clicked 'Send Message', it displays only: Welcome Your email address is:

Without the values that I entered through the form.

When I tried to run the php file directly from the local host (http://localhost/listing.php), I received these error messages: Welcome Notice: Undefined index: user in C:\xampp\htdocs\listing.php on line 7

Your email address is: Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8

I even modified the php code as follows, but still got the same output:

<html>
 <head>
 <title>Listing  Reading input from a form </title>
 </head>
 <body>

 Welcome <?php 
  if(isset($_POST['submit'];)) {
  session_start(); 
  $user = $_POST['user'];
  echo "$text";}else {echo 'Could not load text!';}?><br>
 Your email address is: <?php echo $_POST["address"]; ?>

 </body>
 </html>

I would really appreciate it if you could give some advice to make it work. Thanks

Faresfox
  • 1
  • 1
  • use this if(isset($_POST['submit'])) instead of if(isset($_POST['submit'];)) – A.A Noman Jul 29 '17 at 18:22
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jason Krs Jul 29 '17 at 18:39
  • Another issue I possibly think is going on here is that if ALL your files are not in the `htdocs` directory of `xampp`, you should use `http://localhost/listing.php` in the form action. Like this `
    `
    – Jason Krs Jul 29 '17 at 18:43

4 Answers4

0

if(isset($_POST['submit'];)) should be changed,so you check if address and user is isset. Furthermore you normally don't want ; in your if statements.

Here i have a optimized version for you. the !empty is added so we also check if the inputs are not empty.

if (isset($_POST["name"] && isset($_POST["address"])) {
  if (!empty($_POST["name"] && !empty($_POST["address"]) {
    // execute code as u would 
  }
}
0

Notice: Undefined index: address in C:\xampp\htdocs\listing.php on line 8

This is caused by the use of the ';' in the if condition. Remove that and you should be good. for now.

Try this code:

<html>
<head>
<title>Listing  Reading input from a form </title>
</head>
<body>

Welcome <?php 
    if(isset($_POST['submit'])) 
  {
      session_start(); 
      $user = $_POST['user'];
      echo $user;
      echo "<br><br>Your Email Address is: ".$_POST['address'];
  }
  else 
  {
     echo 'Could not load text!';
  }
 ?>
 <br>



<form action="" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user"></p>
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea></p>
<p><input type="submit" value="send" name="submit"></p>
</form>
</body>
</html>
Jason X
  • 1
  • 1
  • This code worked, but it displays the entered value along with form itself. I don't need the form to be displayed again with the output. Also, the code works as a combined file. When I split html and php codes into two files, it gives this output: Welcome Your Email Address is: ".$_POST['address']; } else { echo 'Could not load text!'; } ?> --- any more tips would be highly appreciated. – Faresfox Jul 29 '17 at 20:17
0

In your second code block $text isn't defined anywhere. Do you mean to have $user?

<html>
 <head>
   <title>Listing Reading input from a form</title>
 </head>
 <body>

 Welcome 
 <?php 
   if(isset($_POST['submit'])) {
     session_start(); 
     echo isset($_POST['user']) ? $_POST['user'] : "User";
   } else {
     echo "User";
   } 
 ?>
 <br>
 Your email address is: 
 <?php 
    echo isset($_POST["address"]) ? $_POST["address"] : "Address" 
 ?>
 </body>
</html>
Liz Hesser
  • 38
  • 1
  • 5
  • I tried your code, but I got the same results: Welcome Your email address is: ; that's only the comments, without the values that I entered in the form. – Faresfox Jul 29 '17 at 20:23
0

Try this and let me know:

<?php
  session_start();
  if(isset($_POST['submit'])) {
    $user = $_POST['user'];
    echo "Welcome ".$user;
    echo "<br>";
    echo "Your email address is: ".$_POST["address"];
  }else {
    // you may get rid of this block if you like
    echo 'Could not load text!';
  }

?>

    <html>
      <head>
        <title>Listing 9.1 A simple HTML form</title>
      </head>
      <body>
        <form action="" method="POST">
          <p><strong>Name:</strong><br>
          <input type="text" name="user"></p>
          <p><strong>Address:</strong><br>
          <textarea name="address" rows="5" cols="40"></textarea></p>
          <p><input type="submit" name='submit' value="send"></p>
        </form>
      </body>
    </html>
Vagabond
  • 877
  • 1
  • 10
  • 23
  • I tried your code, and this is what I got: "; echo "Your email address is: ".$_POST["address"]; }else { echo 'Could not load text!'; } ?> – Faresfox Jul 29 '17 at 20:19
  • @Faresfox: I have edited my answer. Let me know if it works now. – Vagabond Jul 31 '17 at 02:05