Jump to content

Call to Undefined Function - Isset Necessary / Good Practice?


chaseman

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;

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.