jmz1 Posted October 20, 2010 Share Posted October 20, 2010 I have a really simple e-mail form that I made a while ago. I'm trying to make a simple modification, but I'm getting a a logical error somewhere. The problem is with the one optional variable ($wholesale) that gets sent to the PHP script. If the user has clicked the box on the form and the $wholesale variable has something in it, the message gets sent without an error. But if the user leaves the box blank, nothing is in the variable and PHP has a problem with that. It'll still send the e-mail, but I get a Notice: Undefined index: wholesale in /file/path/on our/sever.com/send_form_email.php on line 35 error. Here's a part of the code: // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['street_address']) || !isset($_POST['city']) || !isset($_POST['state']) || !isset($_POST['zip']) || !isset($_POST['phone'])) { died('We are sorry, but there appears to be a problem with the form your submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $street_address = $_POST['street_address']; // required $city = $_POST['city']; // required $state = $_POST['state']; // required $zip = $_POST['zip']; // required $phone = $_POST['phone']; // required $wholesale = $_POST['wholesale']; // not required // I don't see why this isn't solving the problem if ( $_POST['wholesale'] == null ) { $wholesale = "."; } ... then the code formats and sends the e-mail out. Any help would be awesome... this is such a simple thing that I honestly can't think of what else to try... Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/ Share on other sites More sharing options...
PaulRyan Posted October 20, 2010 Share Posted October 20, 2010 The error tells you where to look, line 35. Show us which line is line 35 then we can help you remedy your problem Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124512 Share on other sites More sharing options...
schilly Posted October 20, 2010 Share Posted October 20, 2010 turn down your error reporting. it just means there is nothing in that variable. doesn't effect your script at all. in most regular builds of php "Notice" errors aren't shown. Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124517 Share on other sites More sharing options...
PaulRyan Posted October 20, 2010 Share Posted October 20, 2010 Schilly, all errors are extra overhead that needn't be, fixing it is much better than just leaving it Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124519 Share on other sites More sharing options...
awjudd Posted October 20, 2010 Share Posted October 20, 2010 if ( !isset ( $_POST['wholesale'] ) ) { $wholesale = '.'; } That should remove the notice. That said, you shouldn't be ignoring the notices because there is a chance it could cause your script to do something you don't expect. And iirc, the throwing of notices slows the execution of the scripts a bit. ~judda Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124520 Share on other sites More sharing options...
jmz1 Posted October 20, 2010 Author Share Posted October 20, 2010 juddster, that did it. Thanks... so it was my syntax? I just use my other syntax on another site to do a "if this is empty, put this in it" statement and I swear it worked. lol Ah well... I'm good to go now in any case. Thanks a lot everyone. Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124524 Share on other sites More sharing options...
schilly Posted October 20, 2010 Share Posted October 20, 2010 Schilly, all errors are extra overhead that needn't be, fixing it is much better than just leaving it do you have any articles detailing this? i'd like to know what kind of overhead there is. Because really if you have a form with a lot of fields, some of which are allowed be empty, are you really going to add code to check all these fields for empty then assign a value to them before you assign them to a variable? Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124529 Share on other sites More sharing options...
awjudd Posted October 20, 2010 Share Posted October 20, 2010 @jmz - you were doing a hard read of the array which causes the notice. The isset will do a soft read not causing it to do anything / grabbing any information that is potentially not yours. ~judda Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124531 Share on other sites More sharing options...
DavidAM Posted October 20, 2010 Share Posted October 20, 2010 Schilly, all errors are extra overhead that needn't be, fixing it is much better than just leaving it do you have any articles detailing this? i'd like to know what kind of overhead there is. Because really if you have a form with a lot of fields, some of which are allowed be empty, are you really going to add code to check all these fields for empty then assign a value to them before you assign them to a variable? Aside from any overhead that may or may not exist and may or may not be "significant", ignoring notices is a BAD idea. Retrieving a value from a variable that does not exist is, in my book, an ERROR. Taking your example: "if you have a form with a lot of fields, some of which are allowed be empty, are you really going to add code to check all these fields for empty": YES, I am going to check every field. If you do not check every field and you ignore notices, what happens when you misspell or mis-capitalize one of the field (or array element) names. You have notices turned off so the script goes on its merry way, and you do NOT get the correct results. Then you spend hours searching though the logic looking for a flaw when the problem would have been easily spotted if you had displayed AND read the errors (notices). As to assigning a value to the fields that are allowed to be empty, I don't recommend that. I use the ternary operator: $wholesale = (isset($_POST['wholesale']) ? $_POST['wholesale'] : ''); Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124545 Share on other sites More sharing options...
schilly Posted October 20, 2010 Share Posted October 20, 2010 Schilly, all errors are extra overhead that needn't be, fixing it is much better than just leaving it do you have any articles detailing this? i'd like to know what kind of overhead there is. Because really if you have a form with a lot of fields, some of which are allowed be empty, are you really going to add code to check all these fields for empty then assign a value to them before you assign them to a variable? Aside from any overhead that may or may not exist and may or may not be "significant", ignoring notices is a BAD idea. Retrieving a value from a variable that does not exist is, in my book, an ERROR. Taking your example: "if you have a form with a lot of fields, some of which are allowed be empty, are you really going to add code to check all these fields for empty": YES, I am going to check every field. If you do not check every field and you ignore notices, what happens when you misspell or mis-capitalize one of the field (or array element) names. You have notices turned off so the script goes on its merry way, and you do NOT get the correct results. Then you spend hours searching though the logic looking for a flaw when the problem would have been easily spotted if you had displayed AND read the errors (notices). As to assigning a value to the fields that are allowed to be empty, I don't recommend that. I use the ternary operator: $wholesale = (isset($_POST['wholesale']) ? $_POST['wholesale'] : ''); Your method makes more sense using the ternary operator but for any field I'm going to test whether it works or not regardless of whether the value is set. I've never had an issue with this breaking my scripts. I'd be more interested to know if there is significant overhead. Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124559 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 I'd be more interested to know if there is significant overhead. There's probably more overhead when you don't define the string and PHP has to go through the error reporting and tell you about it. This could of course be eliminated by suppressing the error with an @ cymbal or turning error reporting off. Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124575 Share on other sites More sharing options...
awjudd Posted October 20, 2010 Share Posted October 20, 2010 The @ sign and the turning off of error reporting is only suppressing the errors. It tells PHP to not complain to the UI about them, however they are still being thrown. ~judda Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124577 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 The @ sign and the turning off of error reporting is only suppressing the errors. It tells PHP to not complain to the UI about them, however they are still being thrown. ~judda I agree - but suppressing them is less "overhead" than outputting them.. The best way to get rid of all the error reporting would be <? ini_set('display_errors', 0); error_reporting(0); ?> Or do it on the server level via .htaccess or the httpd.conf Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124580 Share on other sites More sharing options...
awjudd Posted October 20, 2010 Share Posted October 20, 2010 Removing the issues is a lot better than just suppressing them in terms of overhead ... ~judda Quote Link to comment https://forums.phpfreaks.com/topic/216397-undefined-index-error/#findComment-1124665 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.