1

So here's my full code

    <!DOCTYPE html>
<html>
<body>

<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>

And here's the error I get

Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20

Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?

  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index). It's not an error; it's a [notice](http://www.php.net/manual/en/function.error-reporting.php) which is just the run-time telling you something *might* be wrong. In this case, `$encrypt` will be `null`, when you probably expect it to have a valid value. – Patrick M Apr 19 '14 at 20:56
  • `$encrypt = (isset($_POST['in']) ? $_POST['in'] : '');` – Abhik Chakraborty Apr 19 '14 at 20:57
  • Why do I need to do that? @AbhikChakraborty –  Apr 19 '14 at 20:57
  • Already saw that post, just don't see where the $_POST shows @PatrickM –  Apr 19 '14 at 20:58
  • `$_POST['in']` is not defined. If you assume it is and simply try to read it, you get this Notice. You are not supposed to get value of variables that do not exist. – Havenard Apr 19 '14 at 20:59
  • So I should be good? @PatrickM –  Apr 19 '14 at 20:59
  • @user302975 your php will execute no matter the form is submitted or not, so before submission $_POST['in'] does not exists – Royal Bg Apr 19 '14 at 21:00
  • @user302975 when you load the page the $_POST data is not available, so we need to make sure that the $_POST['in'] is used only if its posted else you get undefined index error. – Abhik Chakraborty Apr 19 '14 at 21:00
  • `$_POST` is an array. The first recommendation of the first answer explicitly mentions the `$_POST` array. – Patrick M Apr 19 '14 at 21:00
  • But my code is at the bottom? @Havenard –  Apr 19 '14 at 21:00
  • So what? It doesn't matter. – Havenard Apr 19 '14 at 21:01
  • @user302975 once more, the php will execute not matter how much HTML you have before it. – Royal Bg Apr 19 '14 at 21:01
  • All the code in the page will execute every time the page is requested unless explicitly halted with `exit()` or `die()`, the fact it is after or before the HTML doesn't mean anything. – Havenard Apr 19 '14 at 21:03
  • Also, your php executes the first time the page is served, before the user has submitted the form. This is a GET request, and therefore won't have an POST variables set. See also: http://stackoverflow.com/questions/504947/ – Patrick M Apr 19 '14 at 21:03

4 Answers4

2

As a general practice for forms in php, always check if the submit button has been clicked.

First name your submit button:

<input type="submit" name="submit">

then further in your php:

if (isset($_POST['submit'])) {
    // do your stuff, eg...
    $encrypt = $_POST['in'];
}

EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).

If you really need 2 forms, name your submit buttons differently and then you can call them separately.

<input type="submit" name="submit-in">

<!-- ... -->

<input type="submit" name="submit-out">

<?php // ...

if (isset($_POST['submit-in'])) {
    // do your stuff, eg...
    $encrypt = $_POST['in'];
}

if (isset($_POST['submit-out'])) {
    // do your stuff, eg...
    $dencrypt = $_POST['out'];
}

EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).

benomatis
  • 5,536
  • 7
  • 36
  • 59
  • It's probably because you haven't specified any value in the `in` input or haven't actually clicked submit, provided you updated your code with my suggestion... – benomatis Apr 19 '14 at 22:06
  • ...nonetheless you seem to have 2 forms and 2 submit buttons. I suggest you keep them on one form, and one submit button, or name your submit buttons differently and then you can call them separately... – benomatis Apr 19 '14 at 22:08
  • think EDIT #2 is the answer to your question ;) – benomatis Apr 19 '14 at 22:19
1

you need to first check if the form has been sent, if it hasn't then $_POST['in'] does not yet exist thus throwing the error

Eeji
  • 1,648
  • 3
  • 17
  • 22
  • 1
    @user302975 it's called `isset()` – Royal Bg Apr 19 '14 at 21:01
  • You can simply check `if ($_POST)` to determine if data was posted to your page using POST method. It doesn't necessarily means that `$_POST['in']` is defined though. You should check every variable individually. – Havenard Apr 19 '14 at 21:05
  • When I use `isset()` It won't echo the string, rather a `1`... @RoyalBg –  Apr 19 '14 at 21:08
  • It's because you are getting the value of `isset()`. This is not what you should do. Use `isset()` to determine if the variable is set before reading its actual value. `$encrypt = (isset($_POST['in']) ? $_POST['in'] : '');` – Havenard Apr 19 '14 at 21:10
-1

May be nothing but you called a php script after closing the form /form, the body /body and then then the HTML /html

-4

replace this code $encrypt = $_POST['in']; by this $encrypt = @$_POST['in']; this is an error on client server when you upload this file on remote server you will not saw this. use @ sign on the client server when you saw this error in future.

Sana Malik
  • 1
  • 1
  • 1
  • 3
  • I'm afraid but @ should be used in some rare, rare cases, and that is not one of them. Avoid using error-suppression operator – Royal Bg Apr 19 '14 at 21:04
  • This is bad practice. `@` will suppress the error, but `$_POST['in']` is still undefined, therefore equivalent to `null` (not an empty string as some would believe). – Havenard Apr 19 '14 at 21:08