Jump to content

Undefined Index using $_POST


jsparrow

Recommended Posts

I have been going back over this and can not locate the problem.  I really need someone else's fresh eyes to look at this for me. 

 

I am trying to have an html page send info to a php page using POST.  I keep getting the error Undefined index *** on line **.  I have done this before, but can not locate the problem.

 

Here is the html code for the input page:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Pre-Registration</title>
    <link rel="stylesheet" type="text/css" href="preregistration.css"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  </head>
  <body>
    <h1>Pre-Registration</h1>
    <h3> Please fill out the below pre-registration form. </h3>
    <form method="post" action="reg5.php" enctype="text/plain" onReset="return setDisabled()">
   <table>
     <tr>
      <td class="name">First Name <input class="name" type="text" name="firstname"/> </td>
      <td class="name">Last Name <input class="name" type="text" name="lastname"/> </td>
    </tr>
   </table>
<br/>
      <input type="submit" value="Submit" />
     <input type="button" value="Reset Form" onClick="this.form.reset()" />
    </form>
  </body>
</html>

 

And here is php code that should output: Welcome to our site, firstname lastname

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome Page Test</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<p>
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

echo  'Welcome to our site, ' . 
   htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') .
   htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8'); 
?>
</p>
</body>
</html>

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/224121-undefined-index-using-_post/
Share on other sites

typically, that means you are referencing an index that doesn't exist. without seeing the parts of the error you omitted, i can't say for sure, but i suspect it's caused by referencing those $_POST variables. you should check to see if they are set before using them. one way:

 

if (isset($_POST['firstname'])) {
     $firstname = $_POST['firstname'];
}

if (isset($_POST['lastname'])) {
    $lastname = $_POST['lastname'];
}

The error and text I am getting is:

 

Notice: Undefined index: firstname in C:\Apache2.2\htdocs\formtest\reg5.php on line 12

Notice: Undefined index: lastname in C:\Apache2.2\htdocs\formtest\reg5.php on line 13
Welcome to our site, 

 

I'm really confused since I have another page that is almost similar to this and it works fine.  Is the problem on the html page with some code issue and it not passing the firstname & lastname - or - is it a problem on the php code side not interpreting what was sent correctly?  Thx.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.