Jump to content

[SOLVED] Notice: Use of undefined constant submit


cunoodle2

Recommended Posts

I'm going through all of my pages now doing final work turned on error reporting to see if I was missing anything.

 

I put this code at the top of my pages..

<?php
//print errors to screen...
ini_set ("display_errors", "1");
error_reporting(E_ALL);
?>

 

The produced the error message..

Notice: Use of undefined constant submit - assumed 'submit' in /home/website/public_html/NewUser.php on line 48

 

Line 48 is this..

<?php if (isset($_POST[submit])) ?>

 

Should I have structured this differently?  Is there a way that I could be handling the form submission in a more secure way?

Link to comment
Share on other sites

This $_POST value was probably inside another if statement and may not have been executed (prior if returned false or something). What you may want to do is assign the $_POST value to a static variable in an area of your code that is triggered by any page load and then reference that variable in the line 48 statement.

 

Link to comment
Share on other sites

Array index names are strings -

<?php if (isset($_POST['submit'])) ?>

Since they are stating that "submit" was undefined can I somehow set the index name of the array without overwriting the contents of it or am I just worrying too much about a notice message on the page?

 

This $_POST value was probably inside another if statement and may not have been executed (prior if returned false or something). What you may want to do is assign the $_POST value to a static variable in an area of your code that is triggered by any page load and then reference that variable in the line 48 statement.

 

This actually is the very first "if" statement on the page.  Is there a better more secure way to check to see if a form was submitted?
Link to comment
Share on other sites

When you don't put array index name strings inside quotes, php must first loop up the name in the list of all the defined constants, both the built in and any user defined. When it does not find a match, it must handle the resulting error. Unfortunately, in php's infant day php.net made an poor decision that when an undefined constant is found, that it would "help" the programmer by assuming (i.e. to make an ASS out of U and ME) that he actually meant to put quotes around the name. Php then generates the error message posted and then searches again for the index name with quotes around it this time. If it finds that array index it accesses the value.

 

The fun part of all this is that this whole process takes 8 to 10 times longer to execute the line of code with no quotes around the index name, every time it is executed, than what would take to execute that line if it had the proper quotes around the name string.

Link to comment
Share on other sites

....meaning....you need to specify a key with quotes... like PFMaBiSmAd said the first time around....

 

$_POST is an array, with all your form values....'submit' being a key, and the value would be anything inside "value" attribute or inside the actual input field....

Link to comment
Share on other sites

When you don't put array index name strings inside quotes, php must first loop up the name in the list of all the defined constants, both the built in and any user defined. When it does not find a match, it must handle the resulting error. Unfortunately, in php's infant day php.net made an poor decision that when an undefined constant is found, that it would "help" the programmer by assuming (i.e. to make an ASS out of U and ME) that he actually meant to put quotes around the name. Php then generates the error message posted and then searches again for the index name with quotes around it this time. If it finds that array index it accesses the value.

 

The fun part of all this is that this whole process takes 8 to 10 times longer to execute the line of code with no quotes around the index name, every time it is executed, than what would take to execute that line if it had the proper quotes around the name string.

Thank you for pointing all of that out to me.  I missed the single quotes in your original response at the beginning of the forum.  I had no idea that it took 8-10 times longer and I will say that is probably one of the best things I've learned on this forum in order to help me speed up page processing times a little.  I know we are talking micro seconds but every little bit helps and I GREATLY appreciate it.
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.