Jump to content

Undefined Index Error


SalientAnimal

Recommended Posts

Hi All,

 

Can anyone tell me why I am getting an undefined index error message when submitting my form?

 

This is happening only when the tick boxes are not selected, everything is working fine and the information is written to the database as required.

 

Thanks,

 

 

Here is my checkbox code:

 

 <table width="100%" border="0" align="center">
 <tr>
   <td align="right" width="40%"><strong>Additional Visit Reasons :</strong></td>
   <td width="10%"><input type="checkbox" name="accessory" value="Accessory">Accessory</td>
   <td width="10%"><input type="checkbox" name="airtime" value="Airtime">Airtime</td>
   <td width="40%"><input type="checkbox" name="handset" value="Handset">Handset</td>
 </tr>

 </table>

Link to comment
Share on other sites

I never had that part of the code in my script at all.

 

It was suggested that I add

if(isset($_POST['accessory'])}$_POST['accessory'] == 'Accessory')else{$_POST['N/A']};

 

This is the original submit page:

 

<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
mysql_select_db("store", $con);

$sql="INSERT INTO store_traffic
(username
, gender
, ethnic_group
, age_group
, requirement
, visit_detail
, accessory
, airtime
, handset
, postpaid
, prepaid
, handset_make)

VALUES
('$_POST[username]'
,'$_POST[gender]'
,'$_POST[ethnic_group]'
,'$_POST[age_group]'
,'$_POST[requirement]'
,'$_POST[visit_detail]'
,'$_POST[accessory]'
,'$_POST[airtime]'
,'$_POST[handset]'
,'$_POST[postpaid]'
,'$_POST[prepaid]'
,'$_POST[handset_make]'
)";


if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";
include "redirect_store.html";
mysql_close($con)
?>

Link to comment
Share on other sites

The undefined index error message is because form checkboxes (and radio buttons) are only submitted when they are checked. The solution is to have some validation logic in your code to test if a checkbox is set or not and take an appropriate action.

 

if(isset($_POST['accessory'])){
// code for when this checkbox is checked

} else {
// code for when this checkbox is not checked

}

 

Since I don't have the slightest idea what value you want to use when a checkbox is not set, the actual coding in this example is left up to you.

 

P.S. you need to have validation logic for ALL your form inputs and you need to escape string data and cast numerical data to the appropriate numerical data type before putting it into your query statement to prevent sql errors if the data contains sql special characters or to prevent sql injection.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

Thanks, I think I was appling the if(isset incorrectly.

 

So just to be sure, I would prefer to leave it blank if the checkbox is not set can I have it as follows:

 

if(isset($_POST['accessory'])){
//code for when this checkbox is checked

}else{
($_POST[''])
// code for when this checkbox is not checked

 

Then, lastly, where in my submit for do I put this part of code?

Edited by SalientAnimal
Link to comment
Share on other sites

Managed to solve this with a very basic change on my form rather than the submit part of the page.

 

I just did this:

 


<!-- INCLUDED A HIDDEN FIELD TO SET THE DEFAULT FORM VALUE OF THE CHECKBOX TO "EMPTY" -->
<input type="hidden" name="handset" value="">
<input type="checkbox" name="handset" value="Handset">Handset

Edited by SalientAnimal
Link to comment
Share on other sites

lol - um if you want to leave it blank do something like:

 

if(isset($_POST["accessory"])){

//code for if it IS set

}elseif(!isset($_POST["accessory"])){

//code ofr when it is NOT set

}

 

As lily's suggestion states, it is easier and more secure to do this. As far as I see there is no reason why you shouldn't want to do this

Link to comment
Share on other sites

No real need for that last IF test though: Either it's set or it's not.

 

if (!isset ($_POST['accessory'])) {
   // Error condition, handle it.
   // Let's say it's a fatal one, just to showcase exiting early. 
   return false;
}

// Normal operation, as it is set.

Helps to cut down a lot of the nesting if you utilize the principle of exiting early, and that can have quite a bit impact on the readability. The easier the script is to read, the easier it is to maintain. Especially for others, like those of us trying to help. ;)

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.