$php_mysql$ Posted July 6, 2011 Share Posted July 6, 2011 could someone see where the error is coming from? it appears only when i submit the form Notice: Undefined index: ads_title in C:\wamp\www\ads\post_ads.php on line 23 <?php include("config.php"); include("functions/db_functions.inc.php"); include("functions/gn_functions.inc.php"); if(count($_POST)){ $postData = $_POST; $postData = cleanData($postData); $error = postErrorCheck($postData); printr($_POST); if(count($error) == 0) { } } ?> <form action="post_ads.php" name="postads" method="POST" enctype="multipart/form-data"/> Ad Title*: <br/> <input type="text" name="ads_title" value="" maxlength="100"/> <?php if($error['ads_title'] !='') ///////ERROR IS ON THIS LINE { echo '<div class="error_msg">'.$error['ads_title'].'</div>'; } ?> <br/> Upload Image: <br/> <input type="file" name="ads_image" value=""/> <br/> Your Name*: <br/> <input type="text" name="postersname" value=""/> <br/> Choose Category*: <br/> <select name="category"/> <option value="0"/>Select Category</option> <option value="Events_Announcements"/>Events/Announcements</option> <option value="Career_Jobs"/>Career/Jobs</option> <option value="Buy_Sell"/>Buy/Sell</option> <option value="Realestate"/>Real Estate</option> <option value="Services"/>Services</option> <option value="Vehicles"/>Vehicles</option> <option value="Software"/>Software</option> <option value="Pets"/>Pets</option> <option value="Music"/>Music</option> <option value="Personals"/>Personals</option> <option value="Other"/>Other</option> </select> <br/> Choose Type*: <br/> <select name="ads_type"/> <option value="0"/>Select Ad Type</option> <option value="Offered"/>Offered</option> <option value="Wanted"/>Wanted</option> <option value="Other"/>Other</option> </select> <br/> Choose State*: <br/> <select name="state"/> <option value="0"/>Select State</option> <option value="Assam"/>Assam</option> <option value="Arunachal Pradesh"/>Arunachal Pradesh</option> <option value="Meghalaya"/>Meghalaya</option> <option value="Mizoram"/>Mizoram</option> <option value="Manipur"/>Manipur</option> <option value="Nagaland"/>Nagaland</option> <option value="Tripura"/>Tripura</option> <option value="Other"/>Other</option> </select> <br/> City/Town*: <br/> <input type="text" name="ads_location" value=""/> <br/> Your E-mail*: <br/> <input type="text" name="email" value=""/> <br/> Your Phone: <br/> <input type="text" name="phone" value=""/> <br/> Ad Description*: <br/> <textarea name="description" cols="40" rows="15"/></textarea> <br/> <input type="submit" name="submit" value="Post Ad"/> </form> Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/ Share on other sites More sharing options...
Network_ninja Posted July 7, 2011 Share Posted July 7, 2011 does it only happens in your ads_title? or it also goes with some other field? ads_image........etc.. Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239359 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 A notice is not an error -- it is a "notice". It's very clear what the notice is telling you. You are attempting to make a boolean evaluation using $error['ads_title'] however, this array element doesn't exist. In a situation like this you can use isset() first to shortcircuit evaluation. if (isset($error['ads_title']) && $error['ads_title'] != '') Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239363 Share on other sites More sharing options...
Network_ninja Posted July 7, 2011 Share Posted July 7, 2011 Yep... exactly.. You cannot encounter that kind of notice when the version of your php is below 5.0 Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239366 Share on other sites More sharing options...
PFMaBiSmAd Posted July 7, 2011 Share Posted July 7, 2011 What does your postErrorCheck() function code do and should it return an array element for each form field? Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239439 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 friends it happening for all fields only when submited the form with value in it, i got the same code working on one of my other script which does not output any error at all, here is my function function postErrorCheck($data){ $errormsg = array(); if($data['ads_title'] == '') { $errormsg['ads_title'] = 'Advertisement title is required!'; }else if(strlen($data['ads_title']) > 100) { $errormsg['ads_title'] = 'Title cannot be more than 100 characters'; } return $errormsg; } Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239442 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 Well the problem exists in the code, whether you happen to stumble upon it in testing or not. If you try and access an element of an array that doesn't exist you'll get a notice, unless you either insure that it exists, or check using isset() to short circuit the access as I showed you. Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239443 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 yeah i did used isset and it did solve my error issue but still was wondering the same code works ok in other script but not in this Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239448 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 Ok, well, as I stated previously it's not an error. You can control the "level" of error reporting your site provides, and furthermore it should be logged and not displayed on a production site. Really these notices exist to help you write really clean code, but in many cases, even if you don't write the cleanest level of code, you will not have issues. See error_reporting Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239451 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 yes i have already notice that when i use error_reporting(1); or even error_reporting(0); i get no Notice message Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239454 Share on other sites More sharing options...
$php_mysql$ Posted July 7, 2011 Author Share Posted July 7, 2011 anyways thanks mate, will just satisfy myself with the error_reporting(0); :-) Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239456 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 The setting you want: error_reporting(E_ALL & ~E_NOTICE); For a production server, you want your php.ini (depending on the php version) to set this either to be off, or to send to stderr. You don't want to have to set this in your scripts. Quote Link to comment https://forums.phpfreaks.com/topic/241267-getting-a-undefined-index-error/#findComment-1239461 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.