When you first access the form page, nothing has been submitted, so $_POST will be empty. You should therefore check that the value exists before trying to use it:
PHP Code:
<input type="text" name="email" id="email" value="<?php
if(isset($_POST['email'])) {
echo htmlentities($_POST['email']); // always filter outputs of external data
}
?>" />
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
The "<?=" tag (as well as the "<?" tag) will only work if the now-deprecated short_open_tags option is enabled. And even if it is, it still does not validate that the array element exists before trying to use it, so it will throw a notice-level warning whenever it is not. That warning may not be seen, depending on your error_reporting and display_errors setting, but it's still sloppy coding, plus it does nothing to filter the data being output, so ugly things can happen depending upon what the user entered in the first place.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks NogDog. I took some training and they said DO NOT USE <?= ?>, but they never said why ... so, I kept on using it. The training also said to always use <?php ?> and NOT to use <? ?>. They never stated why so I still use the <? ?>.
The main reason that the short_open_tags option has become deprecated is the potential for confusion with "<?xml" tags. Therefore PHP has decided in recent versions to make the default configuration have short_open_tags disabled, and it will most likely not be available at all in PHP 6. Therefore for code portability and future version upgrade compatibility, it's best to avoid the "<?" and "<?=" short_open_tags entirely - the "<?php" tag will always work. (Yes, you have to type a few more characters now, but that's a minuscule price to pay now compared to migrating to a new host in the future and finding out that all of your scripts break.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks NogDog. I took some training and they said DO NOT USE <?= ?>, but they never said why ... so, I kept on using it. The training also said to always use <?php ?> and NOT to use <? ?>. They never stated why so I still use the <? ?>.
You should use <?= ?> ... this is like an echo in PHP
if you want to echo some value couz you saved this in your Database, a single <? ?> oder <?php ?> wont show it .. you can try this with firebug (plugin for Firefox) and see !
You should use <?= ?> ... this is like an echo in PHP
if you want to echo some value couz you saved this in your Database, a single <? ?> oder <?php ?> wont show it .. you can try this with firebug (plugin for Firefox) and see !
If you use the shorthand versions "<?=" or "<?" it may not function with all installs of php as for quite some time now the default has been to disable the short tags.
The correct solution has been given, but sometimes you may need a quick-and-dirty answer to the problem. Which is, in this case, to add the following to your code:
The correct solution has been given, but sometimes you may need a quick-and-dirty answer to the problem. Which is, in this case, to add the following to your code:
error_reporting(E_ALL ^ E_NOTICE);
That should suppress the error warning.
Probably should be noted that for this specific problem the above wouldn't do anything either since nothing would be parsed at all.
Bookmarks