-2

I have this following code in my php file:

$p_adminType = $_POST['adminType'];
$p_adminUserID = $_POST['userID'];
$p_adminUserPass = $_POST['userPass'];
$p_firstName = $_POST['adminFirstName'];
$p_lastName = $_POST['adminLastName'];

And then when I execute the file by clicking the submit button from an html file, I get a Notice Error saying:

Notice: Undefined index: userID in C:\wamp\www\CaNeCo\CaNeCo_Admin_Reg.php on line 19
Notice: Undefined index: userPass in C:\wamp\www\CaNeCo\CaNeCo_Admin_Reg.php on line 20
Notice: Undefined index: adminFirstName in C:\wamp\www\CaNeCo\CaNeCo_Admin_Reg.php on line 21

wherein you can see from the code where lines 19, 20, and 21 are. I tried adding isset() function to my code just like from what I have read from other sites/forums but when I do that, it doesn't solve my problem. It only hides the error message and the variables are still empty.

How can I resolve that problem? Also, I'm just wondering why is it only happening to 3 of the 5 declarations? $p_adminType = $_POST['adminType']; and $p_lastName = $_POST['adminLastName']; just work fine but not the other 3? Can someone please enlighten me about that?

This is the html file that I have:

<form method="post" action="CaNeCo_Admin_Reg.php">
  <table width="400">
  <tr>
      <td><b>Log-in Information:</b></td>
  </tr>
  <tr>
      <td>&nbsp;</td>
  </tr>
  <tr>
      <td>Administrator Type:</td>
      <td>
        <select name="adminType" id="adminType">
        <option value="0">Please Select</option>
        <option value="1">Super Administrator</option>
        <option value="2">Administrator</option>
        <option value="3">Secretary</option>
        <option value="4">Viewer</option>
        </select>
      </td>
  </tr>
  <tr>
      <td>User ID: </td>
      <td><input type="text" name:"userID" id="adminUserID" placeholder="User I.D." maxlength="20" size="35"/></td>
  </tr>
  <tr>
      <td>Password: </td>
      <td><input type="password" name:"userPass" id="adminUserPass" placeholder="Password" maxlength="25" size="35"/></td>
  </tr>
  <tr>
      <td>Repeat Password:</td>
      <td><input type="password" name="repeatPass" id="adminRepeatPass" placeholder="Repeat Password" maxlength="25" size="35"/></td>
  </tr>
  <tr>
      <td>&nbsp;</td>
   </tr>
   <tr>
      <td colspan="25"><b>Personal Information:</b></td>
   </tr>
   <tr>
      <td>&nbsp;</td>
   </tr>
   <tr>
      <td>First Name: </td>
      <td><input type="text" name:"adminFirstName" id="adminFirstName" placeholder="First Name" maxlength="20" size="35"/></td>
   </tr>
   <tr>
      <td>Last Name: </td>
      <td><input type="text" name="adminLastName" id="adminLastName" placeholder="Last Name" maxlength="25" size="35"/></td>
   </tr>
   <tr>
      <td>&nbsp;</td>
   </tr>
   <tr>
      <td colspan="50"><center><input type="submit" value="Submit"/></center></td>
   </tr>
</table>

Jairo
  • 39
  • 1
  • 9
  • `and the variables are still empty` how would your script know what the value should be if you're not passing it? – Dogbert May 13 '13 at 10:27
  • 1
    `var_dump($_POST);` and check which variables are you getting. – Rikesh May 13 '13 at 10:28
  • First of all you need to make sure whether the name specified for the element on html page is same as the one specified while accessing in php code – dreamweiver May 13 '13 at 10:29
  • @Deval Shah - I've already added my form code. – Jairo May 13 '13 at 10:50
  • @dreamweiver - i've double checked them already so i think there's no problem in that area. – Jairo May 13 '13 at 10:55
  • @Dogbert - it seems that i don't understand what you said. if that's the case, why are the two other lines of codes that are similarly declared didn't have the same problems. I always test my code with dummy data so i'm pretty sure that they are not empty. – Jairo May 13 '13 at 10:56
  • can anyone please try to answer this part of my post? _Also, I'm just wondering why is it only happening to 3 of the 5 declarations? $p_adminType = $_POST['adminType']; and $p_lastName = $_POST['adminLastName']; just work fine but not the other 3? Can someone please enlighten me about that?_ – Jairo May 13 '13 at 11:02
  • `name:"userID" ` you were suppose to use `name="userId"` – dreamweiver May 13 '13 at 11:05

5 Answers5

0

first you check $_POST hold some value or not.

so use print_r($_POST) above your code , it help you to identifies the error,

Rajendra Yadav
  • 645
  • 3
  • 12
0

Undefined index is probably thrown because you don't have defined in your html form those names. Your form must look like this:

<input type='text' name='adminType'/>
<input type='text' name='userID'/>
<input type='text' name='userPass'/>
<input type='text' name='adminFirstName'/>
<input type='text' name='adminLastName'/>

Or whatever input type you are using. You should also post your form code to give more information.

Check that you have exactly those names defined in your html form.

UPDATE

You posted your form code and saw the error. You are using : instead to use = to asign values to your name properties for your inputs. If you take a look, the only 2 elements that you are recieving into the server are sent properly to the server, because you defined their name property like name={value} but the other 3 properties are defined as name:{value}. Assignament with : does not exist in HTML and the form dont know how to send that element with that attribute, so your server never recieves an element with an attribute named userID, because userID is not defined as already said (name={value}).

DaGLiMiOuX
  • 889
  • 9
  • 28
  • I've already double checked that area. so i'm pretty sure that there's no problem with that. – Jairo May 13 '13 at 10:56
  • @Jairo My bad, I rewrited my answer. I didn't want to reference that. You have an error in your properties, you are using `:` instead an `=` when you asign the name to your inputs. To assign values use `=` and do not use `:` – DaGLiMiOuX May 13 '13 at 10:57
  • Thanks! I didn't see that one. :) One thing that I hate about html codes. It doesn't tell you if there are errors in your code. – Jairo May 13 '13 at 11:04
  • ha ha @Jairo, you need to be more careful with your code next time – dreamweiver May 13 '13 at 11:06
0

your html should be like this :-

   <form method="post" action="CaNeCo_Admin_Reg.php">
   <table width="400">
    <tr>
        <td><b>Log-in Information:</b></td>
    </tr>
    <tr>
       <td>&nbsp;</td>
    </tr>
    <tr>
     <td>Administrator Type:</td>
     <td>
       <select name="adminType" id="adminType">
       <option value="0">Please Select</option>
       <option value="1">Super Administrator</option>
       <option value="2">Administrator</option>
       <option value="3">Secretary</option>
       <option value="4">Viewer</option>
       </select>
    </td>
  </tr>
  <tr>
   <td>User ID: </td>
   <td><input type="text" name="userID" id="adminUserID" placeholder="User I.D."         maxlength="20" size="35"/></td>
 </tr>
  <tr>
  <td>Password: </td>
  <td><input type="password" name="userPass" id="adminUserPass" placeholder="Password" maxlength="25" size="35"/></td>
 </tr>
  <tr>
     <td>Repeat Password:</td>
    <td><input type="password" name="repeatPass" id="adminRepeatPass" placeholder="Repeat Password" maxlength="25" size="35"/></td>
  </tr>
  <tr>
     <td>&nbsp;</td>
   </tr>
    <tr>
    <td colspan="25"><b>Personal Information:</b></td>
   </tr>
    <tr>
  <td>&nbsp;</td>
  </tr>
   <tr>
     <td>First Name: </td>
       <td><input type="text" name="adminFirstName" id="adminFirstName" placeholder="First Name" maxlength="20" size="35"/></td>
     </tr>
   <tr>
   <td>Last Name: </td>
   <td><input type="text" name="adminLastName" id="adminLastName" placeholder="Last Name" maxlength="25" size="35"/></td>
  </tr>
  <tr>
     <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="50"><center><input type="submit" value="Submit"/></center></td>
  </tr>
</table>
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
-1

For cheking index existing you may use php function like array_key_exists or isset, if index not found you must init variable as empty (null or empty string):

if (array_key_exists('adminType', $_POST)) {
    $p_adminType = $_POST['adminType'];
} else {
    $p_adminType = '';
}
if (array_key_exists('userID', $_POST)) {
    $p_adminUserID = $_POST['userID'];
else {
    $p_adminUserID = '';
}
if (array_key_exists('userPass', $_POST)) {
    $p_adminUserPass = $_POST['userPass'];
else {
    $p_adminUserPass = '';
}
if (array_key_exists('adminFirstName', $_POST)) {
    $p_firstName = $_POST['adminFirstName'];
else {
    $p_firstName = '';
}
if (array_key_exists('adminLastName', $_POST)) {
    $p_lastName = $_POST['adminLastName'];
else {
    $p_lastName = '';
}
Eugene
  • 1,899
  • 1
  • 13
  • 10
  • `array_key_exists` works slower than `isset`. – mpyw May 13 '13 at 10:40
  • For example, `$p_adminType = isset($_POST['adminType']) ? $_POST['adminType'] : '';` is the best. – mpyw May 13 '13 at 10:47
  • Yes, also, isset for $_POST['adminType'] = null will be false. I use current example for more simple view – Eugene May 13 '13 at 10:48
  • There's no possibility that `$_POST` contains **NULL** . Only **string** (including empty string, `""`) or **array** . – mpyw May 13 '13 at 11:28
-1

By the way, these names seem to be very confusing.

I think you should rewrite these statement:

$p_adminType = $_POST['adminType'];
$p_adminUserID = $_POST['userID'];
$p_adminUserPass = $_POST['userPass'];
$p_firstName = $_POST['adminFirstName'];
$p_lastName = $_POST['adminLastName'];

into:

$p_adminType = $_POST['type'];
$p_adminUserID = $_POST['userID'];
$p_adminUserPass = $_POST['userPass'];
$p_adminFirstName = $_POST['firstName'];
$p_adminLastName = $_POST['lastName'];

or:

$p_type = $_POST['adminType'];
$p_userID = $_POST['adminUserID'];
$p_userPass = $_POST['adminUserPass'];
$p_firstName = $_POST['adminFirstName'];
$p_lastName = $_POST['adminLastName'];

or:

$p_type = $_POST['type'];
$p_userID = $_POST['userID'];
$p_userPass = $_POST['userPass'];
$p_firstName = $_POST['firstName'];
$p_lastName = $_POST['lastName'];

or:

$p_adminType = $_POST['adminType'];
$p_adminUserID = $_POST['adminUserID'];
$p_adminUserPass = $_POST['adminUserPass'];
$p_adminFirstName = $_POST['adminFirstName'];
$p_adminLastName = $_POST['adminLastName'];
mpyw
  • 5,526
  • 4
  • 30
  • 36
  • The reason why my naming scheme is like that is because I find it easier in referencing that way. I tend to have difficulty in remembering variables when they are differently named especially when I already have a lot of variables in my code. – Jairo May 13 '13 at 10:49