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
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'];
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.