chaseman Posted May 11, 2011 Share Posted May 11, 2011 I have a long list of variables with form fields inserted into them like this: $page_id = $_POST['exclude']; I just turned on WP_Debug just to see which error messages would come up and to my surprise it wasn't that bad, the most error messages were "Call to Undefined function". The solution to this problem is simple, all I have to do is wrap an isset around all the form field variables, like this: $page_id = isset($_POST['exclude']); My question is, is this really necessary or considered as good practice? Should I now go ahead and wrap to all the variables in that long list an isset around? Or should I just turn WP_Debug off again and leave it as is? I personally would like to adopt to the good practice way, but I don't know how much of an advantage it is to wrap an isset around every single form field variable. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/ Share on other sites More sharing options...
cyberRobot Posted May 11, 2011 Share Posted May 11, 2011 I would imagine the line of code would need to be: $page_id = isset($_POST['exclude']) ? $_POST['exclude'] : ''; ...or if you don't like the short-hand ifs if(isset($_POST['exclude'])) { $page_id = $_POST['exclude']; } else { $page_id = ''; } Note that I don't have an answer for the best-practices part, but I am curious what others have to say. Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1213949 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 In my opinion that's the BEST way of doing it. You should always verify the existence, then sanitize any variables coming from outside of your script. Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1213965 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2011 Share Posted May 11, 2011 For the specific case of a form, you would use isset() to detect if the form was submitted by testing if the submit button name or a hidden field name is set - if(isset($_POST['submit'])){ // the form was submitted, put all the code to validate and process the form data here... } However, after you know the form was submitted, you would not use isset() on all the rest of the form fields, because text, password, hidden, and textarea fields are set even if they are empty. You would only use isset() on checkbox and radio fields as they are not set when they are not selected in the form. Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1213974 Share on other sites More sharing options...
chaseman Posted May 11, 2011 Author Share Posted May 11, 2011 I understand what you guys are saying but it misses my situation a little bit, what I did was as follows: I assigned a short handle to ALL the form fields, to make them easier to work with like this: $page_id = $_POST['exclude']; $hide_pages_save = $_POST['hide_pages_save']; $blurb_submit = $_POST['blurb_submit']; $blurb1 = trim($_POST['blurb1']); $blurb2 = trim($_POST['blurb2']); $blurb3 = trim($_POST['blurb3']); And THEN when I do the validation part I DO use isset as follows: if (isset($hide_pages_save)) { // run code } The issue is that WP_debug still spits out error messages regarding the variables assignments at the very top of the file, but the errors do not occur when I change it to this: $page_id = isset($_POST['exclude']); $hide_pages_save = isset($_POST['hide_pages_save']); I'm thinking to myself the errors must be there for a reason? Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1214002 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 However, after you know the form was submitted, you would not use isset() on all the rest of the form fields, because text, password, hidden, and textarea fields are set even if they are empty. You would only use isset() on checkbox and radio fields as they are not set when they are not selected in the form. Whether the empty variables are sent or not is completely up to the browser. This is why I like to verify all data. It can be slightly redundant, but I just don't trust anything coming from outside of my script. Chaseman - what errors are you getting? $page_id = isset($_POST['exclude']); will set $page_id as either 1 or 0, and not the value of $_POST['exclude'] I don't think this is what you want. Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1214014 Share on other sites More sharing options...
chaseman Posted May 11, 2011 Author Share Posted May 11, 2011 Ok for further clarification, this is the full error I'm getting, more like a NOTICE: Notice: Undefined index: hide_pages_save in -absolute-path-\-file-name-.php on line 4 And I got around 30 of those notices. EDIT: I just noticed that I totally confused UNDEFINED INDEX, with Call to Undefined Function, sorry about that, should have paid more attention! Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1214018 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 Quite simple. $hide_pages_save = isset($_POST['hide_pages_save']) ? $_POST['hide_pages_save'] : NULL; or in long form if( isset($_POST['hide_pages_save']) ) $hide_pages_save = $_POST['hide_pages_save']; else $hide_pages_save = NULL; Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1214026 Share on other sites More sharing options...
chaseman Posted May 11, 2011 Author Share Posted May 11, 2011 Ok now I understand the system behind it, thanks for taking the time, and to my questions, is this necessary or considered as good practice? Or should I rather just leave it as it is? Quote Link to comment https://forums.phpfreaks.com/topic/236121-call-to-undefined-function-isset-necessary-good-practice/#findComment-1214049 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.