Jump to content

Undefined index error


jmz1

Recommended Posts

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

 

Link to comment
Share on other sites

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'] : '');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

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.