SalientAnimal Posted October 24, 2012 Share Posted October 24, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/ Share on other sites More sharing options...
White_Lily Posted October 24, 2012 Share Posted October 24, 2012 Its because the boxes are returning a false (or empty or 0) value. Its the same as an undefined variable, it's because that variable is not storing any data. Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387395 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 How do I fix it? I can always rely on a reply from you Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387398 Share on other sites More sharing options...
Beeeeney Posted October 24, 2012 Share Posted October 24, 2012 How do I fix it? I can always rely on a reply from you Use an if statement to make it return a NULL value when nothing is selected. Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387399 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 Ok so I tried using it like this: if(isset($_POST['accessory']) } $_POST['accessory'] == 'Accessory') else { $_POST['N/A'] }; But get this error: Parse error: syntax error, unexpected '}' (T_VARIABLE) in submit_store.php on line 14 Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387415 Share on other sites More sharing options...
MDCode Posted October 24, 2012 Share Posted October 24, 2012 (edited) if(isset($_POST['accessory'])) { $_POST['accessory'] = "Accessory"; } else { // Not sure what you're trying to accomplish here $_POST['N/A']; } Edited October 24, 2012 by ExtremeGaming Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387416 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 Currently when I submit my form I get an undefined index error message. What I am trying to do is to prevent this message from coming up. The error message occurs when the user has't checked the checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387425 Share on other sites More sharing options...
MDCode Posted October 24, 2012 Share Posted October 24, 2012 (edited) $_POST['N/A']; is your problem because it is undefined. If you are trying to mark it as N/A use $_POST['accessory'] = "N/A"; Edited October 24, 2012 by ExtremeGaming Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387427 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387428 Share on other sites More sharing options...
PFMaBiSmAd Posted October 24, 2012 Share Posted October 24, 2012 (edited) 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 October 24, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387431 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) 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 October 24, 2012 by SalientAnimal Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387438 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) 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 October 24, 2012 by SalientAnimal Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387442 Share on other sites More sharing options...
White_Lily Posted October 24, 2012 Share Posted October 24, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387443 Share on other sites More sharing options...
MDCode Posted October 24, 2012 Share Posted October 24, 2012 Using any kind of hidden field is a very bad idea (ie Javascript injection) Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387525 Share on other sites More sharing options...
SalientAnimal Posted October 25, 2012 Author Share Posted October 25, 2012 It was the only solution I could get to work for what I needed to do. Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387631 Share on other sites More sharing options...
MDCode Posted October 25, 2012 Share Posted October 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387650 Share on other sites More sharing options...
Christian F. Posted October 25, 2012 Share Posted October 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269851-undefined-index-error/#findComment-1387783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.